summaryrefslogtreecommitdiff
path: root/src/dot.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/dot.rs')
-rw-r--r--src/dot.rs59
1 files changed, 15 insertions, 44 deletions
diff --git a/src/dot.rs b/src/dot.rs
index f328e1a..5e96223 100644
--- a/src/dot.rs
+++ b/src/dot.rs
@@ -53,7 +53,7 @@ impl<'a> Dot<'a> {
write!(w, "{}{}", strict, &graph.graph_type())?;
if let Some(id) = &graph.id {
- write!(w, " {}", format_id(id))?;
+ write!(w, " {}", &id.dot_string())?;
}
writeln!(w, " {{")?;
@@ -187,9 +187,9 @@ impl<'a> Dot<'a> {
w,
r#"{}{} {} {}"#,
get_indentation(indentation_level),
- format_id(&edge_source),
+ &edge_source,
edge_op,
- format_id(&edge_target)
+ &edge_target
)?;
write!(w, "{}", fmt_attributes(&edge.attributes))?;
writeln!(w, ";")
@@ -224,7 +224,7 @@ pub enum RenderOption {
#[derive(Clone, Debug)]
pub struct Graph<'a> {
- pub id: Option<String>,
+ pub id: Option<AttributeText<'a>>,
pub is_directed: bool,
@@ -248,7 +248,7 @@ pub struct Graph<'a> {
impl<'a> Graph<'a> {
pub fn new(
- id: Option<String>,
+ id: Option<AttributeText<'a>>,
is_directed: bool,
strict: bool,
comment: Option<String>,
@@ -291,7 +291,7 @@ impl<'a> Graph<'a> {
}
pub struct GraphBuilder<'a> {
- id: Option<String>,
+ id: Option<AttributeText<'a>>,
is_directed: bool,
@@ -314,13 +314,13 @@ pub struct GraphBuilder<'a> {
errors: Vec<ValidationError>,
}
-// TODO: id should be an escString
+// TODO: id should be an escString. Nah! It should be an AttributeText. Everything should be.
impl<'a> GraphBuilder<'a> {
pub fn new_directed() -> Self {
Self::new(None, true)
}
- pub fn new_named_directed<S: Into<String>>(id: S) -> Self {
+ pub fn new_named_directed<S: Into<AttributeText<'a>>>(id: S) -> Self {
Self::new(Some(id.into()), true)
}
@@ -328,11 +328,11 @@ impl<'a> GraphBuilder<'a> {
Self::new(None, false)
}
- pub fn new_named_undirected<S: Into<String>>(id: S) -> Self {
+ pub fn new_named_undirected<S: Into<AttributeText<'a>>>(id: S) -> Self {
Self::new(Some(id.into()), false)
}
- fn new(id: Option<String>, is_directed: bool) -> Self {
+ fn new(id: Option<AttributeText<'a>>, is_directed: bool) -> Self {
Self {
id,
is_directed,
@@ -433,7 +433,7 @@ impl<'a> GraphBuilder<'a> {
pub fn build_ignore_validation(&self) -> Graph<'a> {
Graph {
- id: self.id.to_owned(),
+ id: self.id.to_owned().into(),
is_directed: self.is_directed,
strict: self.strict,
comment: self.comment.clone(), // TODO: is clone the only option here?
@@ -622,12 +622,12 @@ impl<'a> SubGraphBuilder<'a> {
#[derive(Clone, Debug)]
pub struct Node<'a> {
- pub id: String,
+ pub id: AttributeText<'a>,
pub attributes: IndexMap<String, AttributeText<'a>>,
}
impl<'a> Node<'a> {
- pub fn new<S: Into<String>>(id: S) -> Node<'a> {
+ pub fn new<S: Into<AttributeText<'a>>>(id: S) -> Node<'a> {
// TODO: constrain id
Node {
id: id.into(),
@@ -638,7 +638,7 @@ impl<'a> Node<'a> {
impl<'a> DotString<'a> for Node<'a> {
fn dot_string(&self) -> Cow<'a, str> {
- let mut dot_string = format!("{}", format_id(&self.id));
+ let mut dot_string = format!("{}", &self.id.dot_string());
dot_string.push_str(fmt_attributes(&self.attributes).as_str());
dot_string.push_str(";");
dot_string.into()
@@ -701,7 +701,7 @@ impl<'a> NodeBuilder<'a> {
pub fn build_ignore_validation(&self) -> Node<'a> {
Node {
// TODO: are these to_owned and clones necessary?
- id: self.id.to_owned(),
+ id: self.id.to_owned().into(),
attributes: self.attributes.clone(),
}
}
@@ -965,32 +965,3 @@ impl<'a> EdgeAttributeStatementBuilder<'a> {
fn get_indentation(indentation_level: usize) -> String {
INDENT.repeat(indentation_level)
}
-
-fn is_alphanum(val: &String) -> bool {
- for byte in val.bytes() {
- if !((byte >= b'a' && byte <= b'z') || (byte >= b'A' && byte <= b'Z') ||
- (byte >= b'0' && byte <= b'9') || byte == b'_' || byte >= 128)
- {
- return false;
- }
- }
- true
-}
-
-// According to https://graphviz.org/doc/info/lang.html we should wrap any strings containing
-// non-alphanumerical characters, as escape double quotes within those strings.
-// This probably needs to be more robust but I think for now it fixes a but around double-quoted
-// strings
-fn format_id(val: &String) -> String {
- if is_alphanum(val) {
- val.to_string()
- } else {
- format!("\"{}\"", val.chars().map(
- |c| if c == '"' {
- format!("\\{}", c)
- } else {
- format!("{}", c)
- }).collect::<String>()
- )
- }
-}