diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d39c8ed --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "NoteApp" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..51cd6e9 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "NoteApp" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/main b/main new file mode 100755 index 0000000..911b5b5 Binary files /dev/null and b/main differ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..85caa18 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,117 @@ +use std::collections::HashMap; +use std::env; +use std::fs; +use std::io; +use std::path::{Path, PathBuf}; + +#[derive(Debug, Clone, Copy, PartialEq)] +enum NoteKind { + Txt, + Md, + Typ, +} + +impl NoteKind { + fn from_ext(ext: &str) -> Option { + match ext { + "txt" => Some(Self::Txt), + "md" => Some(Self::Md), + "typ" => Some(Self::Typ), + _ => None, + } + } +} + + +#[derive(Debug)] +struct Note { + path: PathBuf, + kind: NoteKind, + tags: Vec, // folder names above this note +} + +#[derive(Debug, Default)] +struct Vault { + notes: Vec, // nodes + tags: HashMap>, // tag -> indices into `notes` (the edges) + images: Vec, // files found in ".images" folders +} + +fn scan_dir(root: &Path) -> io::Result { + let mut vault = Vault::default(); + walk(root, root, &mut vault)?; + Ok(vault) +} + +fn walk(root: &Path, dir: &Path, vault: &mut Vault) -> io::Result<()> { + let in_images = dir.file_name().and_then(|n| n.to_str()) == Some(".images"); + + for entry in fs::read_dir(dir)? { + let path = entry?.path(); + + if path.is_dir() { + // skip hidden folders, except ".images" + let name = path.file_name().and_then(|n| n.to_str()).unwrap_or(""); + if name.starts_with('.') && name != ".images" { + continue; + } + walk(root, &path, vault)?; + continue; + } + + let ext = path + .extension() + .and_then(|e| e.to_str()) + .unwrap_or("") + .to_lowercase(); + + if in_images { + if matches!(ext.as_str(), "svg" | "webp") { + vault.images.push(path); + } + continue; + } + + if let Some(kind) = NoteKind::from_ext(&ext) { + // folders between root and the file become tags + let tags: Vec = path + .strip_prefix(root) + .ok() + .and_then(|rel| rel.parent()) + .map(|p| { + p.components() + .map(|c| c.as_os_str().to_string_lossy().into_owned()) + .collect() + }) + .unwrap_or_default(); + + let idx = vault.notes.len(); + for tag in &tags { + vault.tags.entry(tag.clone()).or_default().push(idx); + } + vault.notes.push(Note { path, kind, tags }); + } + } + Ok(()) +} + +fn main() -> io::Result<()> { + let dir = env::args().nth(1).unwrap_or_else(|| ".".to_string()); + let root = Path::new(&dir); + + if !root.is_dir() { + eprintln!("'{}' is not a directory", root.display()); + std::process::exit(1); + } + + let vault = scan_dir(root)?; + + for (tag, ids) in &vault.tags { + println!("#{tag}"); + for &i in ids { + println!(" {}", vault.notes[i].path.display()); + } + } + println!("{} notes, {} images", vault.notes.len(), vault.images.len()); + Ok(()) +} \ No newline at end of file