Skip to main content

corpora_engine/
graph_builder.rs

1//! Accumulates `Parsed` records; on `Settled` builds the immutable graph and emits
2//! `GraphBuilt`. On `Changed` it replaces one entry — that's the whole incremental story
3//! (full rebuild for now; see plan §7).
4
5use std::collections::BTreeMap;
6use std::sync::Arc;
7
8use corpora_core::subscriber::ek;
9use corpora_core::{Context, DocPath, Event, Graph, Interest, Record, Subscriber};
10
11#[derive(Default)]
12pub struct GraphBuilder {
13    records: BTreeMap<DocPath, Arc<Record>>,
14}
15
16impl GraphBuilder {
17    pub fn new() -> Self {
18        GraphBuilder::default()
19    }
20}
21
22impl Subscriber for GraphBuilder {
23    fn name(&self) -> &'static str {
24        "graph-builder"
25    }
26
27    fn interest(&self) -> Interest {
28        Interest::only(ek::PARSED | ek::SETTLED)
29    }
30
31    fn handle(&mut self, ev: &Event, cx: &mut Context<'_>) {
32        match ev {
33            Event::Parsed(r) => {
34                self.records.insert(r.path.clone(), r.clone());
35            }
36            Event::Settled => {
37                let records: Vec<Arc<Record>> = self.records.values().cloned().collect();
38                let (graph, diags) = Graph::build(records);
39                for d in diags {
40                    cx.error(d);
41                }
42                cx.emit(Event::GraphBuilt(Arc::new(graph)));
43            }
44            _ => {}
45        }
46    }
47}