use std::collections::BTreeMap;
use std::sync::Arc;
use corpora_core::subscriber::ek;
use corpora_core::{Context, DocPath, Event, Graph, Interest, Record, Subscriber};
#[derive(Default)]
pub struct GraphBuilder {
records: BTreeMap<DocPath, Arc<Record>>,
}
impl GraphBuilder {
pub fn new() -> Self {
GraphBuilder::default()
}
}
impl Subscriber for GraphBuilder {
fn name(&self) -> &'static str {
"graph-builder"
}
fn interest(&self) -> Interest {
Interest::only(ek::PARSED | ek::SETTLED)
}
fn handle(&mut self, ev: &Event, cx: &mut Context<'_>) {
match ev {
Event::Parsed(r) => {
self.records.insert(r.path.clone(), r.clone());
}
Event::Settled => {
let records: Vec<Arc<Record>> = self.records.values().cloned().collect();
let (graph, diags) = Graph::build(records);
for d in diags {
cx.error(d);
}
cx.emit(Event::GraphBuilt(Arc::new(graph)));
}
_ => {}
}
}
}