use crate::{
ast::{ArrowType, Flowchart, NodeShape},
builder::ir::{
self,
common::{
ArrowKind, ArrowSpec, EdgePriority, LabelSpec, LineKind, PortHint, PortSet, SizeHint,
StyleRef,
},
shape::ShapeKind,
unigraph::{EdgeKind, UGEdge, UGNode, Unigraph},
},
};
pub fn map_shape(shape: &Option<NodeShape>) -> ShapeKind {
match shape {
None => ShapeKind::Rectangle,
Some(NodeShape::Rectangle) => ShapeKind::Rectangle,
Some(NodeShape::Rounded) => ShapeKind::Rounded,
Some(NodeShape::Stadium) => ShapeKind::Stadium,
Some(NodeShape::Subroutine) => ShapeKind::Subroutine,
Some(NodeShape::Diamond) => ShapeKind::Diamond,
Some(NodeShape::Hexagon) => ShapeKind::Hexagon,
Some(NodeShape::Circle) => ShapeKind::Circle,
Some(NodeShape::DoubleCircle) => ShapeKind::DoubleCircle,
Some(NodeShape::Cylinder) => ShapeKind::Cylinder,
Some(NodeShape::Asymmetric) => ShapeKind::Asymmetric,
Some(NodeShape::Parallelogram) | Some(NodeShape::ParallelogramAlt) => {
ShapeKind::Parallelogram
}
Some(NodeShape::Trapezoid) | Some(NodeShape::TrapezoidAlt) => ShapeKind::Trapezoid,
}
}
pub fn map_arrow(arrow: &ArrowType) -> ArrowSpec {
let (start, end) = match arrow {
ArrowType::Solid | ArrowType::Thick | ArrowType::Dotted => {
(ArrowKind::None, ArrowKind::Arrow)
}
ArrowType::Labeled(_) => (ArrowKind::None, ArrowKind::Arrow),
ArrowType::NoArrow => (ArrowKind::None, ArrowKind::None),
ArrowType::Invisible => (ArrowKind::None, ArrowKind::None),
ArrowType::Both => (ArrowKind::Arrow, ArrowKind::Arrow),
ArrowType::Circle => (ArrowKind::None, ArrowKind::Circle),
ArrowType::Cross => (ArrowKind::None, ArrowKind::Cross),
ArrowType::MultiCircle => (ArrowKind::Circle, ArrowKind::Circle),
ArrowType::MultiCross => (ArrowKind::Cross, ArrowKind::Cross),
};
ArrowSpec { start, end }
}
pub fn map_line(arrow: &ArrowType) -> LineKind {
match arrow {
ArrowType::Solid
| ArrowType::NoArrow
| ArrowType::Both
| ArrowType::Circle
| ArrowType::Cross
| ArrowType::MultiCircle
| ArrowType::MultiCross
| ArrowType::Labeled(_) => LineKind::Solid,
ArrowType::Dotted => LineKind::Dotted,
ArrowType::Thick => LineKind::Thick,
ArrowType::Invisible => LineKind::Invisible,
}
}
pub fn extract_flowchart(fc: &Flowchart) -> Unigraph {
use std::collections::HashMap;
let mut shape_of: HashMap<&str, &Option<NodeShape>> = HashMap::new();
let mut text_of: HashMap<&str, &Option<String>> = HashMap::new();
for sg in &fc.subgraphs {
for n in &sg.nodes {
shape_of.entry(n.id.as_str()).or_insert(&n.shape);
text_of.entry(n.id.as_str()).or_insert(&n.text);
}
}
for n in &fc.nodes {
shape_of.entry(n.id.as_str()).or_insert(&n.shape);
text_of.entry(n.id.as_str()).or_insert(&n.text);
}
let mut nodes = Vec::new();
let mut seen = std::collections::HashSet::new();
let mut emit_node = |nodes: &mut Vec<UGNode>,
id: &str,
shape_of: &HashMap<&str, &Option<NodeShape>>,
text_of: &HashMap<&str, &Option<String>>| {
if seen.contains(id) {
return;
}
seen.insert(id.to_string());
let shape = map_shape(shape_of.get(id).copied().unwrap_or(&None));
let label = LabelSpec {
text: text_of
.get(id)
.and_then(|o| o.as_ref())
.cloned()
.unwrap_or_else(|| id.to_string()),
spans: Vec::new(), };
nodes.push(UGNode {
id: id.to_string(),
kind: ir::common::NodeKind::Atom,
role: ir::common::NodeRole::Atom,
shape,
label: ir::common::LabelOrMeasured::Spec(label),
ports: PortSet::default(),
size_hint: SizeHint::ByText,
style_ref: StyleRef::NodeDefault,
constraint: ir::common::NodeConstraint::Free,
detail: ir::common::NodeDetail::None,
});
};
for n in &fc.nodes {
emit_node(&mut nodes, &n.id, &shape_of, &text_of);
}
for sg in &fc.subgraphs {
for n in &sg.nodes {
emit_node(&mut nodes, &n.id, &shape_of, &text_of);
}
}
let mut edges = Vec::new();
for (i, e) in fc.edges.iter().enumerate() {
let (src_port, tgt_port) = match fc.direction {
Some(crate::ast::Direction::LR) | Some(crate::ast::Direction::RL) => {
(PortHint::Right, PortHint::Left)
}
_ => (PortHint::Bottom, PortHint::Top),
};
edges.push(UGEdge {
id: format!("e{}", i),
source: e.source.clone(),
target: e.target.clone(),
source_port: src_port,
target_port: tgt_port,
kind: EdgeKind::Flow,
label_text: e.label.clone(),
label: None,
priority: EdgePriority::Primary,
routing_hint: ir::common::RoutingHint::Spline,
arrow: map_arrow(&e.arrow_type),
line_kind: map_line(&e.arrow_type),
repulsion: 1.0,
cardinality: (None, None),
cardinality_text: (None, None),
});
}
let subgraphs: Vec<ir::unigraph::UGSubgraph> = fc
.subgraphs
.iter()
.enumerate()
.map(|(i, sg)| ir::unigraph::UGSubgraph {
id: format!("sub{}", i),
title: sg.title.clone(),
member_ids: sg.nodes.iter().map(|n| n.id.clone()).collect(),
kind: ir::common::ContainerKind::Subgraph,
})
.collect();
Unigraph {
family: ir::unigraph::GraphFamily::Directed,
direction: fc.direction.unwrap_or(crate::ast::Direction::TB),
nodes,
edges,
subgraphs,
sequence_rows: None,
meta: ir::common::DiagramMeta {
title: None,
show_data: false,
},
}
}