use crate::graph::Graph;
use std::collections::BTreeMap;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ResolvedFlow {
pub resource: String,
pub from: String,
pub to: String,
pub quantity: String,
pub namespace: String,
pub annotations: BTreeMap<String, serde_json::Value>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FlowError {
DanglingReference { flow_id: String },
MultiNamespace { namespaces: Vec<String> },
}
impl std::fmt::Display for FlowError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::DanglingReference { flow_id } => write!(
f,
"flow {flow_id} references an unknown entity or resource (dangling reference)"
),
Self::MultiNamespace { namespaces } => write!(
f,
"model spans multiple namespaces ({}); the projection targets require a single \
namespace. Namespaces found: {}",
namespaces.len(),
namespaces.join(", ")
),
}
}
}
impl std::error::Error for FlowError {}
impl From<FlowError> for String {
fn from(e: FlowError) -> String {
e.to_string()
}
}
pub fn collect_flows(graph: &Graph) -> Result<Vec<ResolvedFlow>, FlowError> {
let entity_name: BTreeMap<String, String> = graph
.all_entities()
.iter()
.map(|e| (e.id().to_string(), e.name().to_string()))
.collect();
let resource_name: BTreeMap<String, String> = graph
.all_resources()
.iter()
.map(|r| (r.id().to_string(), r.name().to_string()))
.collect();
let mut flows: Vec<ResolvedFlow> = Vec::new();
for f in graph.all_flows() {
let (Some(from), Some(to), Some(resource)) = (
entity_name.get(&f.from_id().to_string()),
entity_name.get(&f.to_id().to_string()),
resource_name.get(&f.resource_id().to_string()),
) else {
return Err(FlowError::DanglingReference {
flow_id: f.id().to_string(),
});
};
flows.push(ResolvedFlow {
resource: resource.clone(),
from: from.clone(),
to: to.clone(),
quantity: f.quantity().to_string(),
namespace: f.namespace().to_string(),
annotations: f
.attributes()
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
});
}
flows.sort_by(|a, b| {
a.resource
.cmp(&b.resource)
.then(a.from.cmp(&b.from))
.then(a.to.cmp(&b.to))
.then(a.quantity.cmp(&b.quantity))
});
Ok(flows)
}
pub fn model_namespace(graph: &Graph) -> Result<String, FlowError> {
let mut namespaces: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
for e in graph.all_entities() {
namespaces.insert(e.namespace().to_string());
}
for r in graph.all_resources() {
namespaces.insert(r.namespace().to_string());
}
match namespaces.len() {
0 => Ok("default".to_string()),
1 => Ok(namespaces.iter().next().unwrap().clone()),
_ => Err(FlowError::MultiNamespace {
namespaces: namespaces.into_iter().collect(),
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::parse_to_graph;
#[test]
fn collect_flows_resolves_and_sorts() {
let src = r#"
@namespace "proc"
Entity "A" in proc
Entity "B" in proc
Resource "R" units in proc
Flow "R" from "B" to "A" quantity 2
Flow "R" from "A" to "B" quantity 1
"#;
let g = parse_to_graph(src).unwrap();
let flows = collect_flows(&g).unwrap();
assert_eq!(flows.len(), 2);
assert_eq!(flows[0].from, "A");
assert_eq!(flows[1].from, "B");
}
#[test]
fn collect_flows_preserves_flow_annotations() {
let src = r#"
@namespace "proc"
Entity "A" in proc
Entity "B" in proc
Resource "R" units in proc
Flow "R" @cqrs { "kind": "command" } from "A" to "B" quantity 1
"#;
let g = parse_to_graph(src).unwrap();
let flows = collect_flows(&g).unwrap();
assert_eq!(flows.len(), 1);
assert_eq!(flows[0].annotations["cqrs"]["kind"], "command");
}
#[test]
fn collect_flows_errors_on_dangling_reference() {
let src = r#"
@namespace "proc"
Entity "A" in proc
Entity "B" in proc
Resource "R" units in proc
Flow "R" from "A" to "B" quantity 1
"#;
let g = parse_to_graph(src).unwrap();
let mut value = serde_json::to_value(&g).unwrap();
let flow = value["flows"]
.as_object_mut()
.unwrap()
.values_mut()
.next()
.unwrap();
flow["resource_id"] = serde_json::json!(uuid::Uuid::new_v4().to_string());
let dangling: Graph = serde_json::from_value(value).unwrap();
let err = collect_flows(&dangling).unwrap_err();
assert!(matches!(err, FlowError::DanglingReference { .. }));
assert!(err.to_string().contains("dangling reference"), "{err}");
}
#[test]
fn model_namespace_single() {
let src = r#"
@namespace "procurement"
Entity "Buyer" in procurement
Resource "PO" units in procurement
"#;
let g = parse_to_graph(src).unwrap();
assert_eq!(model_namespace(&g).unwrap(), "procurement");
}
#[test]
fn model_namespace_errors_on_multiple() {
let src = r#"
@namespace "a"
Entity "A" in a
Entity "B" in b
"#;
let g = parse_to_graph(src).unwrap();
let err = model_namespace(&g).unwrap_err();
assert!(matches!(err, FlowError::MultiNamespace { .. }));
assert!(err.to_string().contains("multiple namespaces"), "{err}");
}
#[test]
fn model_namespace_default_when_empty() {
let g = parse_to_graph("@namespace \"empty\"\n").unwrap();
assert_eq!(model_namespace(&g).unwrap(), "default");
}
}