Skip to main content

collections/
collections.rs

1//! Collection management operations - fully self-contained.
2//!
3//! Run: `cargo run --example collections`
4
5use anyhow::Result;
6use qmd::{Store, is_virtual_path, parse_virtual_path};
7
8const SAMPLE_DOCS: &[(&str, &str)] = &[
9    ("rust-basics.md", include_str!("data/rust-basics.md")),
10    ("error-handling.md", include_str!("data/error-handling.md")),
11    ("async-await.md", include_str!("data/async-await.md")),
12];
13
14fn main() -> Result<()> {
15    let db_path = std::env::temp_dir().join("qmd_collections.db");
16    let _ = std::fs::remove_file(&db_path);
17    let store = Store::open(&db_path)?;
18
19    let now = chrono::Utc::now().to_rfc3339();
20    for (name, content) in SAMPLE_DOCS {
21        let hash = Store::hash_content(content);
22        let title = Store::extract_title(content);
23        store.insert_content(&hash, content, &now)?;
24        store.insert_document("samples", name, &title, &hash, &now, &now)?;
25    }
26
27    // List files
28    println!("Files in 'samples':");
29    for (path, title, _, size) in store.list_files("samples", None)? {
30        println!("  {} - {} ({} bytes)", path, title, size);
31    }
32
33    // Virtual path parsing
34    println!("\nVirtual path parsing:");
35    for path in [
36        "qmd://samples/rust-basics.md",
37        "/local/file.md",
38        "relative.md",
39    ] {
40        if is_virtual_path(path) {
41            if let Some((coll, file)) = parse_virtual_path(path) {
42                println!("  {} -> [{}] {}", path, coll, file);
43            }
44        } else {
45            println!("  {} -> local", path);
46        }
47    }
48
49    let _ = std::fs::remove_file(&db_path);
50    Ok(())
51}