Skip to content

Commit e7b42aa

Browse files
committed
fix(analysis): properly escape when exporting to graphviz
1 parent 877a6b8 commit e7b42aa

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

modules/analysis/src/service/render/graphviz.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
use crate::service::Visitor;
1+
use crate::{
2+
model::graph::{self, Node},
3+
service::Visitor,
4+
};
5+
use std::fmt::Write;
26
use trustify_entity::relationship::Relationship;
37

8+
/// Escape a string to be used as dot string value. The result
9+
/// must be placed within double quotes.
10+
///
11+
/// Double quotes and backspace are prefixed with another backspace.
12+
///
13+
/// Newlines are replaced with `\n`. CR, form-feed, and backspace are
14+
/// just dropped.
415
fn escape(id: &str) -> String {
516
let mut escaped = String::with_capacity(id.len());
617

718
for ch in id.chars() {
819
match ch {
9-
'"' => {
20+
'"' | '\\' => {
1021
escaped.push('\\');
1122
escaped.push(ch);
1223
}
1324
'\n' => {
1425
escaped.push_str("\\n");
1526
}
27+
'\r' | '\x08' | '\x0C' => {
28+
// just drop
29+
}
1630
_ => escaped.push(ch),
1731
}
1832
}
1933

2034
escaped
2135
}
2236

23-
use crate::model::graph;
24-
use crate::model::graph::Node;
25-
use std::fmt::Write;
26-
2737
pub struct Renderer {
2838
data: String,
2939
}
@@ -104,9 +114,9 @@ impl Visitor for Renderer {
104114

105115
#[cfg(test)]
106116
mod test {
107-
108117
#[test]
109118
fn escape() {
110119
assert_eq!(super::escape("foo\"bar\nbaz"), r#"foo\"bar\nbaz"#);
120+
assert_eq!(super::escape("foo\\\"bar\rbaz"), r#"foo\\\"barbaz"#);
111121
}
112122
}

0 commit comments

Comments
 (0)