Skip to main content

weave_content/
lib.rs

1#![deny(unsafe_code)]
2#![deny(clippy::unwrap_used)]
3#![deny(clippy::expect_used)]
4#![allow(clippy::missing_errors_doc)]
5
6pub mod cache;
7pub mod entity;
8pub mod nulid_gen;
9pub mod output;
10pub mod parser;
11pub mod registry;
12pub mod relationship;
13pub mod timeline;
14pub mod verifier;
15pub mod writeback;
16
17use crate::entity::Entity;
18use crate::parser::{ParseError, ParsedCase, SectionKind};
19use crate::relationship::Rel;
20
21/// Parse a case file fully: front matter, entities, relationships, timeline.
22/// Returns the parsed case, inline entities, and relationships (including NEXT from timeline).
23///
24/// When a registry is provided, relationship and timeline names are resolved
25/// against both inline events AND the global entity registry.
26pub fn parse_full(
27    content: &str,
28    reg: Option<&registry::EntityRegistry>,
29) -> Result<(ParsedCase, Vec<Entity>, Vec<Rel>), Vec<ParseError>> {
30    let case = parser::parse(content)?;
31    let mut errors = Vec::new();
32
33    let mut all_entities = Vec::new();
34    for section in &case.sections {
35        if section.kind == SectionKind::Events {
36            let entities =
37                entity::parse_entities(&section.body, section.kind, section.line, &mut errors);
38            all_entities.extend(entities);
39        }
40    }
41
42    // Build combined name list: inline events + registry entities
43    let mut entity_names: Vec<&str> = all_entities.iter().map(|e| e.name.as_str()).collect();
44    if let Some(registry) = reg {
45        for name in registry.names() {
46            if !entity_names.contains(&name) {
47                entity_names.push(name);
48            }
49        }
50    }
51
52    let event_names: Vec<&str> = all_entities
53        .iter()
54        .filter(|e| e.label == entity::Label::PublicRecord)
55        .map(|e| e.name.as_str())
56        .collect();
57
58    let mut all_rels = Vec::new();
59    for section in &case.sections {
60        if section.kind == SectionKind::Relationships {
61            let rels = relationship::parse_relationships(
62                &section.body,
63                section.line,
64                &entity_names,
65                &case.sources,
66                &mut errors,
67            );
68            all_rels.extend(rels);
69        }
70    }
71
72    for section in &case.sections {
73        if section.kind == SectionKind::Timeline {
74            let rels =
75                timeline::parse_timeline(&section.body, section.line, &event_names, &mut errors);
76            all_rels.extend(rels);
77        }
78    }
79
80    if errors.is_empty() {
81        Ok((case, all_entities, all_rels))
82    } else {
83        Err(errors)
84    }
85}
86
87/// Collect registry entities referenced by relationships in this case.
88pub fn collect_referenced_registry_entities(
89    rels: &[Rel],
90    inline_entities: &[Entity],
91    reg: &registry::EntityRegistry,
92) -> Vec<Entity> {
93    let inline_names: Vec<&str> = inline_entities.iter().map(|e| e.name.as_str()).collect();
94    let mut referenced = Vec::new();
95    let mut seen_names: Vec<String> = Vec::new();
96
97    for rel in rels {
98        for name in [&rel.source_name, &rel.target_name] {
99            if !inline_names.contains(&name.as_str())
100                && !seen_names.contains(name)
101                && let Some(entry) = reg.get_by_name(name)
102            {
103                referenced.push(entry.entity.clone());
104                seen_names.push(name.clone());
105            }
106        }
107    }
108
109    referenced
110}
111
112/// Build a `CaseOutput` from a case file path.
113/// Handles parsing and ID writeback.
114pub fn build_case_output(
115    path: &str,
116    reg: &registry::EntityRegistry,
117) -> Result<output::CaseOutput, i32> {
118    let content = match std::fs::read_to_string(path) {
119        Ok(c) => c,
120        Err(e) => {
121            eprintln!("{path}: error reading file: {e}");
122            return Err(2);
123        }
124    };
125
126    let (case, entities, rels) = match parse_full(&content, Some(reg)) {
127        Ok(result) => result,
128        Err(errors) => {
129            for err in &errors {
130                eprintln!("{path}:{err}");
131            }
132            return Err(1);
133        }
134    };
135
136    let referenced_entities = collect_referenced_registry_entities(&rels, &entities, reg);
137
138    let build_result = match output::build_output(
139        &case.id,
140        &case.title,
141        &case.summary,
142        &case.sources,
143        &entities,
144        &rels,
145        &referenced_entities,
146    ) {
147        Ok(out) => out,
148        Err(errors) => {
149            for err in &errors {
150                eprintln!("{path}:{err}");
151            }
152            return Err(1);
153        }
154    };
155
156    let case_output = build_result.output;
157
158    // Write back generated IDs to source case file
159    if !build_result.case_pending.is_empty() {
160        let mut pending = build_result.case_pending;
161        if let Some(modified) = writeback::apply_writebacks(&content, &mut pending) {
162            if let Err(e) = writeback::write_file(std::path::Path::new(path), &modified) {
163                eprintln!("{e}");
164                return Err(2);
165            }
166            let count = pending.len();
167            eprintln!("{path}: wrote {count} generated ID(s) back to file");
168        }
169    }
170
171    // Write back generated IDs to entity files
172    if let Some(code) = writeback_registry_entities(&build_result.registry_pending, reg) {
173        return Err(code);
174    }
175
176    eprintln!(
177        "{path}: built ({} nodes, {} relationships)",
178        case_output.nodes.len(),
179        case_output.relationships.len()
180    );
181    Ok(case_output)
182}
183
184/// Write back generated IDs to registry entity files.
185/// Returns `Some(exit_code)` on error, `None` on success.
186fn writeback_registry_entities(
187    pending: &[(String, writeback::PendingId)],
188    reg: &registry::EntityRegistry,
189) -> Option<i32> {
190    for (entity_name, pending_id) in pending {
191        let Some(entry) = reg.get_by_name(entity_name) else {
192            continue;
193        };
194        let entity_path = &entry.path;
195        let entity_content = match std::fs::read_to_string(entity_path) {
196            Ok(c) => c,
197            Err(e) => {
198                eprintln!("{}: error reading file: {e}", entity_path.display());
199                return Some(2);
200            }
201        };
202        let fm_end = writeback::find_front_matter_end(&entity_content);
203        let mut ids = vec![writeback::PendingId {
204            line: fm_end.unwrap_or(2),
205            id: pending_id.id.clone(),
206            kind: writeback::WriteBackKind::EntityFrontMatter,
207        }];
208        if let Some(modified) = writeback::apply_writebacks(&entity_content, &mut ids) {
209            if let Err(e) = writeback::write_file(entity_path, &modified) {
210                eprintln!("{e}");
211                return Some(2);
212            }
213            eprintln!("{}: wrote generated ID back to file", entity_path.display());
214        }
215    }
216    None
217}
218
219/// Resolve the content root directory.
220///
221/// Priority: explicit `--root` flag > parent of given path > current directory.
222pub fn resolve_content_root(path: Option<&str>, root: Option<&str>) -> std::path::PathBuf {
223    if let Some(r) = root {
224        return std::path::PathBuf::from(r);
225    }
226    if let Some(p) = path {
227        let p = std::path::Path::new(p);
228        if p.is_file() {
229            if let Some(parent) = p.parent() {
230                for ancestor in parent.ancestors() {
231                    if ancestor.join("cases").is_dir()
232                        || ancestor.join("actors").is_dir()
233                        || ancestor.join("institutions").is_dir()
234                    {
235                        return ancestor.to_path_buf();
236                    }
237                }
238                return parent.to_path_buf();
239            }
240        } else if p.is_dir() {
241            return p.to_path_buf();
242        }
243    }
244    std::path::PathBuf::from(".")
245}
246
247/// Load entity registry from content root. Returns empty registry if no entity dirs exist.
248pub fn load_registry(content_root: &std::path::Path) -> Result<registry::EntityRegistry, i32> {
249    match registry::EntityRegistry::load(content_root) {
250        Ok(reg) => Ok(reg),
251        Err(errors) => {
252            for err in &errors {
253                eprintln!("registry: {err}");
254            }
255            Err(1)
256        }
257    }
258}
259
260/// Resolve case file paths from path argument.
261/// If path is a file, returns just that file.
262/// If path is a directory (or None), auto-discovers `cases/**/*.md`.
263pub fn resolve_case_files(
264    path: Option<&str>,
265    content_root: &std::path::Path,
266) -> Result<Vec<String>, i32> {
267    if let Some(p) = path {
268        let p_path = std::path::Path::new(p);
269        if p_path.is_file() {
270            return Ok(vec![p.to_string()]);
271        }
272        if !p_path.is_dir() {
273            eprintln!("{p}: not a file or directory");
274            return Err(2);
275        }
276    }
277
278    let cases_dir = content_root.join("cases");
279    if !cases_dir.is_dir() {
280        return Ok(Vec::new());
281    }
282
283    let mut files = Vec::new();
284    discover_md_files(&cases_dir, &mut files, 0);
285    files.sort();
286    Ok(files)
287}
288
289/// Recursively discover .md files in a directory (max 3 levels deep for cases/year/topic/).
290fn discover_md_files(dir: &std::path::Path, files: &mut Vec<String>, depth: usize) {
291    const MAX_DEPTH: usize = 3;
292    if depth > MAX_DEPTH {
293        return;
294    }
295
296    let Ok(entries) = std::fs::read_dir(dir) else {
297        return;
298    };
299
300    let mut entries: Vec<_> = entries.filter_map(Result::ok).collect();
301    entries.sort_by_key(std::fs::DirEntry::file_name);
302
303    for entry in entries {
304        let path = entry.path();
305        if path.is_dir() {
306            discover_md_files(&path, files, depth + 1);
307        } else if path.extension().and_then(|e| e.to_str()) == Some("md")
308            && let Some(s) = path.to_str()
309        {
310            files.push(s.to_string());
311        }
312    }
313}