use crate::types::{AcbResult, CodeUnit, Edge};
use super::code_graph::CodeGraph;
pub struct GraphBuilder {
graph: CodeGraph,
pending_edges: Vec<Edge>,
errors: Vec<String>,
}
impl GraphBuilder {
pub fn new(dimension: usize) -> Self {
Self {
graph: CodeGraph::new(dimension),
pending_edges: Vec::new(),
errors: Vec::new(),
}
}
pub fn with_default_dimension() -> Self {
Self::new(crate::types::DEFAULT_DIMENSION)
}
pub fn add_unit(mut self, unit: CodeUnit) -> Self {
self.graph.add_unit(unit);
self
}
pub fn add_edge(mut self, edge: Edge) -> Self {
self.pending_edges.push(edge);
self
}
pub fn build(mut self) -> AcbResult<CodeGraph> {
for edge in self.pending_edges {
self.graph.add_edge(edge)?;
}
Ok(self.graph)
}
pub fn build_lenient(mut self) -> CodeGraph {
for edge in self.pending_edges {
if let Err(e) = self.graph.add_edge(edge) {
tracing::warn!("Skipping invalid edge: {}", e);
self.errors.push(format!("{}", e));
}
}
self.graph
}
pub fn errors(&self) -> &[String] {
&self.errors
}
}