corpora-engine 0.1.0

Bus orchestrator and IO edges (parser, fs walk, writer, git oracle) for corpora.
Documentation
//! Accumulates `Parsed` records; on `Settled` builds the immutable graph and emits
//! `GraphBuilt`. On `Changed` it replaces one entry — that's the whole incremental story
//! (full rebuild for now; see plan §7).

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)));
            }
            _ => {}
        }
    }
}