tawasuyu

tawasuyu/minga

Sovereign P2P content sharing with a content-addressed FUSE mount, in Rust.

⎇ 85795c42cbd49259b2fef73a15989c73ac319141 03_ukupacha/minga/card-discovery/src/registry.rs Rust · 46 lines · 1.4 KB History Who touched what raw

feat: minga standalone — compartición P2P soberana con montaje FUSE content-addressed (front-door, git-dep al monorepo) 85795c42c · Sergio ·

1//! Registro local: escaneo de directorios con Cards en disco.
2
3use crate::index::CardIndex;
4use std::path::Path;
5
6/// Escanea `dir` (no recursivo) cargando toda Card `*.json` válida.
7/// Los archivos que no parsean como Card se saltan en silencio.
8pub fn scan_dir(dir: &Path) -> std::io::Result<CardIndex> {
9    let mut index = CardIndex::new();
10    for entry in std::fs::read_dir(dir)? {
11        let path = entry?.path();
12        if path.extension().and_then(|e| e.to_str()) == Some("json") {
13            if let Ok(card) = cards::load_card_file(&path) {
14                index.insert(card);
15            }
16        }
17    }
18    Ok(index)
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24    use card_core::Card;
25
26    #[test]
27    fn scans_only_valid_json_cards() {
28        let dir = tempfile::tempdir().unwrap();
29        for name in ["alpha", "beta"] {
30            let card = Card::new(name);
31            let json = serde_json::to_string(&card).unwrap();
32            std::fs::write(dir.path().join(format!("{name}.json")), json).unwrap();
33        }
34        // Ruido que debe ignorarse.
35        std::fs::write(dir.path().join("readme.txt"), "no soy una card").unwrap();
36        std::fs::write(dir.path().join("roto.json"), "{ no json }").unwrap();
37
38        let ix = scan_dir(dir.path()).unwrap();
39        assert_eq!(ix.len(), 2);
40    }
41
42    #[test]
43    fn missing_dir_is_an_error() {
44        assert!(scan_dir(Path::new("/no/existe/jamas")).is_err());
45    }
46}