diff --git a/output_multiple_nodes.svg b/output_multiple_nodes.svg new file mode 100644 index 0000000..ef9e6e0 --- /dev/null +++ b/output_multiple_nodes.svg @@ -0,0 +1,15 @@ +Node 1Node 1 \ No newline at end of file diff --git a/src/render/svglib/mod.rs b/src/render/svglib/mod.rs new file mode 100644 index 0000000..ab86762 --- /dev/null +++ b/src/render/svglib/mod.rs @@ -0,0 +1,134 @@ +pub mod circle; +pub mod div; +pub mod ellipse; +pub mod foreign_object; +pub mod group; +pub mod image; +pub mod line; +pub mod link; +pub mod path; +pub mod rect; +pub mod svg; +pub mod text; + +#[derive(Debug)] +pub struct Value(String); + +impl From<&str> for Value { + fn from(s: &str) -> Self { + Self(s.to_string()) + } +} + +impl Value { + pub fn to_string(&self) -> String { + self.0.clone() + } +} + +impl From for Value { + fn from(s: usize) -> Self { + Self(s.to_string()) + } +} + +impl From for Value { + fn from(s: u32) -> Self { + Self(s.to_string()) + } +} + +impl From for Value { + fn from(s: i32) -> Self { + Self(s.to_string()) + } +} + +impl From for Value { + fn from(s: f32) -> Self { + Self(s.to_string()) + } +} + +impl From for Value { + fn from(s: String) -> Self { + Self(s) + } +} + +impl From<&String> for Value { + fn from(s: &String) -> Self { + Self(s.to_string()) + } +} + +pub enum ElementType { + Circle, + Ellipse, + ForeignObject, + Group(Vec), + Image, + Line, + Link, + Path, + Rect, +} + +pub trait Element { + fn get_type(&self) -> ElementType; + fn to_string(&self) -> String; + fn atts(&self) -> &[Att]; +} + +pub trait Shape { + fn fill(self, value: V) -> Self + where + V: Into; + + fn stroke(self, value: V) -> Self + where + V: Into; + + fn transform(self, value: V) -> Self + where + V: Into; +} + +#[derive(Debug)] +pub struct Att { + name: String, + value: Value, +} + +impl Att { + pub fn new(name: &str, value: Value) -> Self { + Self { + name: name.to_string(), + value, + } + } +} + +fn att(name: &str, value: V) -> Att +where + V: Into, +{ + Att::new(name, value.into()) +} + +fn att_str(att: &Option) -> String { + att.as_ref() + .map(|a| format!(r#" {}="{}""#, a.name, a.value.to_string())) + .unwrap_or("".to_string()) +} + +fn att_str2(att_name: &str, att_val: &Option) -> String { + att_val + .as_ref() + .map(|val| format!(r#" {}="{}""#, att_name, val)) + .unwrap_or("".to_string()) +} + +fn att_str3(att: &Att) -> String { + format!(r#" {}="{}""#, att.name, att.value.to_string()) +}