Initial impl
This commit is contained in:
commit
4b729c5d32
133 files changed
+20475
No files matched your search
@@ -0,0 +1,139 @@
|
||||
//! A collection is a folder of `.http` files. Scanning builds the sidebar
|
||||
//! tree (folder → file → requests) and parses every file.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::httpfile::HttpFile;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Node {
|
||||
Folder {
|
||||
name: String,
|
||||
path: PathBuf,
|
||||
children: Vec<Node>,
|
||||
},
|
||||
File {
|
||||
name: String,
|
||||
path: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub fn name(&self) -> &str {
|
||||
match self {
|
||||
Node::Folder { name, .. } | Node::File { name, .. } => name,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn path(&self) -> &Path {
|
||||
match self {
|
||||
Node::Folder { path, .. } | Node::File { path, .. } => path,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Collection {
|
||||
pub root: PathBuf,
|
||||
pub tree: Node,
|
||||
/// Parsed files by absolute path.
|
||||
pub files: BTreeMap<PathBuf, HttpFile>,
|
||||
}
|
||||
|
||||
pub fn is_http_file(path: &Path) -> bool {
|
||||
matches!(path.extension().and_then(|e| e.to_str()), Some("http" | "rest"))
|
||||
}
|
||||
|
||||
fn skip_dir(name: &str) -> bool {
|
||||
name.starts_with('.') || matches!(name, "node_modules" | "target")
|
||||
}
|
||||
|
||||
/// Scans `root` recursively. Folders without `.http` files are left out.
|
||||
pub fn scan(root: &Path) -> std::io::Result<Collection> {
|
||||
let mut files = BTreeMap::new();
|
||||
let tree = scan_dir(root, &mut files)?.unwrap_or_else(|| Node::Folder {
|
||||
name: dir_name(root),
|
||||
path: root.to_path_buf(),
|
||||
children: Vec::new(),
|
||||
});
|
||||
Ok(Collection {
|
||||
root: root.to_path_buf(),
|
||||
tree,
|
||||
files,
|
||||
})
|
||||
}
|
||||
|
||||
fn dir_name(p: &Path) -> String {
|
||||
p.file_name()
|
||||
.map(|n| n.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| p.display().to_string())
|
||||
}
|
||||
|
||||
fn scan_dir(dir: &Path, files: &mut BTreeMap<PathBuf, HttpFile>) -> std::io::Result<Option<Node>> {
|
||||
let mut entries: Vec<_> = std::fs::read_dir(dir)?.filter_map(Result::ok).collect();
|
||||
entries.sort_by_key(|e| e.file_name().to_string_lossy().to_lowercase());
|
||||
let mut folders = Vec::new();
|
||||
let mut leaves = Vec::new();
|
||||
for e in entries {
|
||||
let path = e.path();
|
||||
let name = e.file_name().to_string_lossy().into_owned();
|
||||
let Ok(ft) = e.file_type() else { continue };
|
||||
if ft.is_dir() {
|
||||
if skip_dir(&name) {
|
||||
continue;
|
||||
}
|
||||
if let Some(n) = scan_dir(&path, files)? {
|
||||
folders.push(n);
|
||||
}
|
||||
} else if is_http_file(&path) {
|
||||
let text = std::fs::read_to_string(&path)?;
|
||||
files.insert(path.clone(), HttpFile::parse(text));
|
||||
leaves.push(Node::File { name, path });
|
||||
}
|
||||
}
|
||||
if folders.is_empty() && leaves.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
folders.extend(leaves);
|
||||
Ok(Some(Node::Folder {
|
||||
name: dir_name(dir),
|
||||
path: dir.to_path_buf(),
|
||||
children: folders,
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn scans_folders_files_and_requests() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
std::fs::write(
|
||||
root.join("users.http"),
|
||||
"### List\nGET /users\n\n### Get\nGET /users/1\n",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::create_dir_all(root.join("orders/payloads")).unwrap();
|
||||
std::fs::write(root.join("orders/create.http"), "POST /orders\n").unwrap();
|
||||
std::fs::write(root.join("orders/payloads/order.json"), "{}").unwrap();
|
||||
std::fs::create_dir(root.join(".git")).unwrap();
|
||||
std::fs::write(root.join(".git/x.http"), "GET /").unwrap();
|
||||
std::fs::create_dir(root.join("empty")).unwrap();
|
||||
|
||||
let c = scan(root).unwrap();
|
||||
let Node::Folder { children, .. } = &c.tree else {
|
||||
panic!()
|
||||
};
|
||||
let names: Vec<&str> = children.iter().map(Node::name).collect();
|
||||
assert_eq!(names, vec!["orders", "users.http"]);
|
||||
let Node::Folder { children: orders, .. } = &children[0] else {
|
||||
panic!()
|
||||
};
|
||||
assert_eq!(orders.len(), 1);
|
||||
assert_eq!(c.files.len(), 2);
|
||||
assert_eq!(c.files[&root.join("users.http")].request_count(), 2);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user