use super::diagnostics::DiagnosticId;
use super::graph::GraphNode;
const SHAPE_ALIASES: [(&str, &str); 7] = [
("task", "roundedRect"),
("tool", "subroutine"),
("input", "lean-right"),
("decision", "diamond"),
("refdoc", "lin-doc"),
("action", "hexagon"),
("round", "rect"),
];
const REMOVED_SHAPES: [&str; 27] = [
"doc",
"stadium",
"terminal",
"circle",
"trapezoid",
"inv_trapezoid",
"inv-trapezoid",
"doublecircle",
"double-circle",
"typeDeclaration",
"procs",
"lean_left",
"lean-left",
"in-out",
"cylinder",
"ellipse",
"odd",
"tag-rect",
"tagged-rectangle",
"delay",
"half-rounded-rectangle",
"lin-rect",
"lined-rectangle",
"win-pane",
"window-pane",
"curv-trap",
"curved-trapezoid",
];
const ALLOWED_SHAPES: [&str; 13] = [
"roundedRect",
"subroutine",
"subprocess",
"subproc",
"framed-rectangle",
"lean-right",
"diamond",
"lin-doc",
"lined-document",
"hexagon",
"hex",
"connector",
"collapsedGroup",
];
const DEFAULT_SHAPE: &str = "roundedRect";
pub(crate) fn resolve_shape_alias(shape: Option<&str>) -> Option<&str> {
match shape {
Some(s) if !s.is_empty() => Some(
SHAPE_ALIASES
.iter()
.find(|(alias, _)| *alias == s)
.map_or(s, |(_, shape)| shape),
),
_ => shape,
}
}
pub(crate) fn is_valid_shape(name: &str) -> bool {
crate::diagrams::flowchart::is_valid_shape(name) || name == "constructor"
}
pub(crate) struct ShapeFinding {
pub(crate) id: DiagnosticId,
pub(crate) error: bool,
pub(crate) message: String,
pub(crate) node_id: String,
}
pub(crate) fn normalise_node_shapes(nodes: &mut [GraphNode]) -> Vec<ShapeFinding> {
let mut found = Vec::new();
for node in nodes {
if node.is_group {
continue;
}
if matches!(node.shape.as_str(), "" | "squareRect" | "rect") {
node.shape = DEFAULT_SHAPE.into();
}
let (id, error, message) = if REMOVED_SHAPES.contains(&node.shape.as_str()) {
let message = format!(
"shape \"{}\" was removed in v0.8.1, using \"{DEFAULT_SHAPE}\"",
node.shape
);
(DiagnosticId::ShapeRemoved, true, message)
} else if !ALLOWED_SHAPES.contains(&node.shape.as_str()) {
let message = format!(
"shape \"{}\" is not supported, using \"{DEFAULT_SHAPE}\"",
node.shape
);
(DiagnosticId::ShapeUnsupported, false, message)
} else {
continue;
};
found.push(ShapeFinding {
id,
error,
message,
node_id: node.id.clone(),
});
node.shape = DEFAULT_SHAPE.into();
}
found
}