mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Add a bounded Mermaid text renderer (#45817)
## What changed Add `codex-mermaid`, a standalone crate that renders supported subsets of flowchart, sequence, state, class, and ER diagrams as Unicode text. Expose `render` for plain text and `render_spans` for semantic node, edge, and text spans that callers can style. Enforce source, diagram, canvas, and display-width limits. Return errors for unsupported input or exceeded limits without producing partial diagrams, leaving source fallback to callers. Include documentation and a stdin rendering example. ## Testing Add snapshots for each diagram family, checks for relationship endpoints, Unicode labels, semantic spans, truncated input, and size limits, plus edge reconstruction for all 512 directed three-node graphs in all four layout directions. GitOrigin-RevId: 5b4e65d9152abce5d833e4e6b12113a5a21f700e
This commit is contained in:
1 parent
7f501cd334
commit
7c709f0ffd
21 files changed
+2259
No files matched your search
@@ -0,0 +1,225 @@
|
||||
//! Class and ER grammars, preserving members, endpoint cardinalities, and relationship kinds.
|
||||
|
||||
use super::Direction;
|
||||
use super::Edge;
|
||||
use super::Graph;
|
||||
use super::MAX_EDGES;
|
||||
use super::RenderError;
|
||||
use super::parse::check_label;
|
||||
use super::parse::identifier;
|
||||
|
||||
pub(super) fn parse(header: &str, body: &[&str]) -> Result<Graph, RenderError> {
|
||||
if matches!(header, "stateDiagram" | "stateDiagram-v2") {
|
||||
return super::state::parse(body);
|
||||
}
|
||||
let er = header == "erDiagram";
|
||||
let mut graph = Graph::default();
|
||||
let mut block = None;
|
||||
let mut direction = false;
|
||||
for &line in body {
|
||||
if let Some(index) = block {
|
||||
if line == "}" {
|
||||
block = None;
|
||||
} else {
|
||||
let member = if er {
|
||||
attribute(line)?
|
||||
} else {
|
||||
check_label(line)?;
|
||||
line.to_owned()
|
||||
};
|
||||
let node: &mut super::Node = &mut graph.nodes[index];
|
||||
if node.members.len() == 16 {
|
||||
return Err(RenderError::Limit);
|
||||
}
|
||||
node.members.push(member);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(value) = line.strip_prefix("direction ") {
|
||||
if direction {
|
||||
return Err(RenderError::Unsupported);
|
||||
}
|
||||
graph.direction = Direction::parse(value.trim())?;
|
||||
direction = true;
|
||||
continue;
|
||||
}
|
||||
let declaration = !er && line.starts_with("class ");
|
||||
let mut rest = line.strip_prefix("class ").filter(|_| !er).unwrap_or(line);
|
||||
let id = identifier(&mut rest)?;
|
||||
rest = rest.trim_start();
|
||||
let title = id == "accTitle" || er && id.eq_ignore_ascii_case("accTitle");
|
||||
let description = id == "accDescr" || er && id.eq_ignore_ascii_case("accDescr");
|
||||
if !declaration
|
||||
&& (title && rest.starts_with(':') || description && rest.starts_with([':', '{']))
|
||||
{
|
||||
return Err(RenderError::Unsupported);
|
||||
}
|
||||
let from = graph.node(id)?;
|
||||
if rest == "{" && (er || declaration) {
|
||||
block = Some(from);
|
||||
continue;
|
||||
}
|
||||
if declaration || (er && rest.is_empty()) {
|
||||
if !rest.is_empty() {
|
||||
return Err(RenderError::Unsupported);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !er
|
||||
&& let Some(member) = rest
|
||||
.strip_prefix(':')
|
||||
.filter(|text| !text.starts_with("::"))
|
||||
{
|
||||
let member = member.trim();
|
||||
check_label(member)?;
|
||||
if graph.nodes[from].members.len() == 16 {
|
||||
return Err(RenderError::Limit);
|
||||
}
|
||||
graph.nodes[from].members.push(member.to_owned());
|
||||
continue;
|
||||
}
|
||||
let source_card = if er {
|
||||
String::new()
|
||||
} else {
|
||||
cardinality(&mut rest)?
|
||||
};
|
||||
rest = rest.trim_start();
|
||||
let split = rest
|
||||
.find("--")
|
||||
.into_iter()
|
||||
.chain(rest.find(".."))
|
||||
.min()
|
||||
.ok_or(RenderError::Unsupported)?;
|
||||
if split > 2 {
|
||||
return Err(RenderError::Unsupported);
|
||||
}
|
||||
let left = &rest[..split];
|
||||
let dashed = &rest[split..split + 2] == "..";
|
||||
rest = &rest[split + 2..];
|
||||
let (source_tip, target_tip, source_card, target_card) = if er {
|
||||
let source_card = match left {
|
||||
"||" => "1",
|
||||
"|o" => "0..1",
|
||||
"}|" => "1..many",
|
||||
"}o" => "0..many",
|
||||
_ => return Err(RenderError::Unsupported),
|
||||
}
|
||||
.to_owned();
|
||||
let right = rest.get(..2).ok_or(RenderError::Unsupported)?;
|
||||
let target_card = match right {
|
||||
"||" => "1",
|
||||
"o|" => "0..1",
|
||||
"|{" => "1..many",
|
||||
"o{" => "0..many",
|
||||
_ => return Err(RenderError::Unsupported),
|
||||
}
|
||||
.to_owned();
|
||||
rest = &rest[2..];
|
||||
('─', '─', source_card, target_card)
|
||||
} else {
|
||||
let source_tip = match left {
|
||||
"" => '─',
|
||||
"<" => '◄',
|
||||
"<|" => '◁',
|
||||
"*" => '◆',
|
||||
"o" => '◇',
|
||||
_ => return Err(RenderError::Unsupported),
|
||||
};
|
||||
let mut target_tip = '─';
|
||||
for (token, tip) in [("|>", '◁'), (">", '◄'), ("*", '◆'), ("o", '◇')] {
|
||||
if let Some(after) = rest.strip_prefix(token) {
|
||||
target_tip = tip;
|
||||
rest = after;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let target_card = cardinality(&mut rest)?;
|
||||
(source_tip, target_tip, source_card, target_card)
|
||||
};
|
||||
let to = graph.node(identifier(&mut rest)?)?;
|
||||
rest = rest.trim();
|
||||
let label = if let Some(label) = rest
|
||||
.strip_prefix(':')
|
||||
.filter(|text| !text.starts_with("::"))
|
||||
{
|
||||
let label = label.trim();
|
||||
check_label(label)?;
|
||||
label.to_owned()
|
||||
} else if !rest.is_empty() || er {
|
||||
return Err(RenderError::Unsupported);
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
if graph.edges.len() == MAX_EDGES {
|
||||
return Err(RenderError::Limit);
|
||||
}
|
||||
let label = match (source_card.is_empty(), label.is_empty()) {
|
||||
(true, _) => label,
|
||||
(false, true) => format!("({source_card})"),
|
||||
(false, false) => format!("({source_card}) {label}"),
|
||||
};
|
||||
graph.edges.push(Edge {
|
||||
from,
|
||||
to,
|
||||
label,
|
||||
source_tip,
|
||||
target_tip,
|
||||
dashed,
|
||||
target_label: if target_card.is_empty() {
|
||||
target_card
|
||||
} else {
|
||||
format!("({target_card})")
|
||||
},
|
||||
});
|
||||
}
|
||||
if block.is_some() || graph.nodes.is_empty() {
|
||||
return Err(RenderError::Unsupported);
|
||||
}
|
||||
Ok(graph)
|
||||
}
|
||||
|
||||
fn cardinality(rest: &mut &str) -> Result<String, RenderError> {
|
||||
*rest = rest.trim_start();
|
||||
if let Some(after) = rest.strip_prefix('"') {
|
||||
let (value, after) = after.split_once('"').ok_or(RenderError::Unsupported)?;
|
||||
check_label(value)?;
|
||||
*rest = after;
|
||||
Ok(value.to_owned())
|
||||
} else {
|
||||
Ok(String::new())
|
||||
}
|
||||
}
|
||||
|
||||
fn attribute(line: &str) -> Result<String, RenderError> {
|
||||
let mut rest = line;
|
||||
let data_type = identifier(&mut rest)?;
|
||||
if !rest.starts_with(char::is_whitespace) {
|
||||
return Err(RenderError::Unsupported);
|
||||
}
|
||||
let name = identifier(&mut rest)?;
|
||||
let (keys, comment) = if let Some((keys, comment)) = rest.trim().split_once('"') {
|
||||
let comment = comment.strip_suffix('"').ok_or(RenderError::Unsupported)?;
|
||||
check_label(comment)?;
|
||||
(keys.trim(), Some(comment))
|
||||
} else {
|
||||
(rest.trim(), None)
|
||||
};
|
||||
if !keys.is_empty()
|
||||
&& !keys
|
||||
.split(',')
|
||||
.all(|key| matches!(key.trim(), "PK" | "FK" | "UK"))
|
||||
{
|
||||
return Err(RenderError::Unsupported);
|
||||
}
|
||||
let mut result = format!("{data_type} {name}");
|
||||
if !keys.is_empty() {
|
||||
result.push(' ');
|
||||
result.push_str(keys);
|
||||
}
|
||||
if let Some(comment) = comment {
|
||||
result.push_str(" — ");
|
||||
result.push_str(comment);
|
||||
}
|
||||
check_label(&result)?;
|
||||
Ok(result)
|
||||
}
|
||||
Reference in new issue
Block a user