1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::collections;
use std::fmt::Write;
use htmlescape;

// TODO should this be pub at all? We should do the encoding all ourselves

/// HTML entity-encode a string.
pub fn encode(s: &str) -> String {
    htmlescape::encode_minimal(s)
}

/// HTML entity-encodes a string for use in attributes values.
pub fn encode_attribute(s: &str) -> String {
    htmlescape::encode_attribute(s)
}

/// A shorthand to draw rounded corners, see `PathData::arc`
#[derive(Clone, Copy)]
pub enum Arc {
    EastToNorth,
    EastToSouth,
    NorthToEast,
    NorthToWest,
    SouthToEast,
    SouthToWest,
    WestToNorth,
    WestToSouth,
}

/// A builder-struct for SVG-Paths
pub struct PathData {
    text: String
}

impl PathData {
    /// Construct a empty `PathData`
    pub fn new() -> Self {
        PathData { text: String::new() }
    }

    /// Convert to a `Element` of type `path` and fill it's data-attribute
    pub fn into_path(self) -> Element {
        Element::new("path")
            .set("d", self)
    }

    /// Move the cursor to this absolute position without drawing anything
    pub fn move_to(mut self, x: i64, y: i64) -> Self {
        write!(self.text, " M {} {}", x, y).unwrap();
        self
    }

    /// Move the cursor relative to the current position without drawing anything
    pub fn move_rel(mut self, x: i64, y: i64) -> Self {
        write!(self.text, " m {} {}", x, y).unwrap();
        self
    }

    /// Draw a line from the cursor's current location to the given relative position
    pub fn line_rel(mut self, x: i64, y: i64) -> Self {
        write!(self.text, " l {} {}", x, y).unwrap();
        self
    }

    /// Draw a horizontal section from the cursor's current position
    pub fn horizontal(mut self, h: i64) -> Self {
        write!(self.text, " h {}", h).unwrap();
        if h > 50 {
            self.move_rel(-(h / 2 - 3), 0)
                .line_rel(-5, -5)
                .move_rel(0, 10)
                .line_rel(5, -5)
                .move_rel(h / 2 - 3, 0)
        } else if h < -50 {
            self.move_rel(-(h / 2 - 3), 0)
                .line_rel(5, -5)
                .move_rel(0, 10)
                .line_rel(-5, -5)
                .move_rel(h / 2 - 3, 0)
        } else {
            self
        }
    }

    /// Draw a vertical section from the cursor's current position
    pub fn vertical(mut self, h: i64) -> Self {
        write!(self.text, " v {}", h).unwrap();
        if h > 50 {
            self.move_rel(0, -(h / 2 - 3))
                .line_rel(-5, -5)
                .move_rel(10, 0)
                .line_rel(-5, 5)
                .move_rel(0, h / 2 - 3)
        } else if h < - 50 {
            self.move_rel(0, -(h / 2 - 3))
                .line_rel(-5, 5)
                .move_rel(10, 0)
                .line_rel(-5, -5)
                .move_rel(0, h / 2 - 3)
        } else {
            self
        }
    }

    /// Draw a rounded corner using the given radius and direction
    pub fn arc(mut self, radius: i64, kind: Arc) -> Self {
        let (sweep, x, y) = match kind {
            Arc::EastToNorth => (1, -radius, -radius),
            Arc::EastToSouth => (0, -radius, radius),
            Arc::NorthToEast => (0, radius, radius),
            Arc::NorthToWest => (1, -radius, radius),
            Arc::SouthToEast => (1, radius, -radius),
            Arc::SouthToWest => (0, -radius, -radius),
            Arc::WestToNorth => (0, radius, -radius),
            Arc::WestToSouth => (1, radius, radius),
        };
        write!(self.text, " a {0} {0} 0 0 {1} {2} {3}", radius, sweep, x, y).unwrap();
        self
    }
}

impl ::std::fmt::Display for PathData {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        write!(f, "{}", self.text)
    }
}

/// A pseudo-SVG Element
///
/// ```
/// use railroad::notactuallysvg as svg;
/// let e = svg::Element::new("g")
///         .add(svg::Element::new("rect")
///                 .set("class", "important")
///                 .set("x", 15))
///         .add(svg::PathData::new()
///                  .move_to(5, 5)
///                  .line_rel(10, 20)
///                  .into_path());
/// let serialized = e.to_string();
/// assert_eq!(serialized, "<g>\n<rect class=\"important\" x=\"15\"/>\n<path d=\" M 5 5 l 10 20\"/>\n</g>\n");
/// ```
#[derive(Clone, Debug)]
pub struct Element {
    name: String,
    attributes: collections::HashMap<String, String>,
    text: Option<String>,
    children: Vec<Element>,
    siblings: Vec<Element>,
}

impl Element {
    /// Construct a new `Element` of type `name`.
    pub fn new(name: impl Into<String>) -> Self {
        Element { name: name.into(),
                    attributes: collections::HashMap::new(),
                    text: None,
                    children: Vec::new(),
                    siblings: Vec::new() }
    }

    /// Set this Element's attribute `key` to `value`
    pub fn set(mut self, key: impl Into<String>, value: impl ToString) -> Self {
        self.attributes.insert(key.into(), value.to_string());
        self
    }

    /// Set all attributes via these `key`:`value`-pairs
    pub fn set_all<'a>(mut self, iter: impl IntoIterator<Item=(&'a String, &'a String)>) -> Self {
        self.attributes.extend(iter.into_iter().map(|(k, v)| (k.to_owned(), v.to_owned())));
        self
    }

    /// Set the text within the opening and closing tag of this Element.
    ///
    /// The text is automatically HTML-escaped. It is written before any children.
    pub fn text(mut self, text: &str) -> Self {
        self.text = Some(htmlescape::encode_minimal(text));
        self
    }

    /// Set the text within the opening and closing tag of this Element.
    ///
    /// The text is NOT automatically HTML-escaped.
    pub fn raw_text(mut self, text: impl Into<String>) -> Self {
        self.text = Some(text.into());
        self
    }

    /// Add a child to this Element
    ///
    /// Children is written within the opening and closing tag of this Element.
    pub fn add(mut self, e: Element) -> Self {
        self.children.push(e);
        self
    }

    /// Add a child to this Element
    ///
    /// Children is written within the opening and closing tag of this Element.
    pub fn push(&mut self, e: Element) -> &mut Self {
        self.children.push(e);
        self
    }

    /// Add a sibling to this Element
    ///
    /// Siblings is written after the closing tag of this Element.
    pub fn append(mut self, e: Element) -> Self {
        self.siblings.push(e);
        self
    }

    #[cfg(not(feature="visual-debug"))]
    #[allow(unused_variables)]
    #[doc(hidden)]
    pub fn debug(self, name: &str, x: i64, y: i64, n: &super::RailroadNode) -> Self {
        self
    }

    /// Adds some basic textual and visual debugging information to this Element
    #[cfg(feature="visual-debug")]
    pub fn debug(self, name: &str, x: i64, y: i64, n: &super::RailroadNode) -> Self {
        self.set("railroad:type", name)
            .set("railroad:x", x)
            .set("railroad:y", y)
            .set("railroad:entry_height", n.entry_height())
            .set("railroad:exit_height", n.exit_height())
            .set("railroad:height", n.height())
            .set("railroad:width", n.width())
            .add(Element::new("title")
                        .text(name))
            .append(Element::new("path")
                    .set("d", PathData::new()
                            .move_to(x, y)
                            .horizontal(n.width())
                            .vertical(5)
                            .move_rel(-n.width(), -5)
                            .vertical(n.height())
                            .horizontal(5)
                            .move_rel(-5, -n.height())
                            .move_rel(0, n.entry_height())
                            .horizontal(10))
                    .set("class", "debug"))
    }
}

impl ::std::fmt::Display for Element {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        write!(f, "<{}", self.name)?;
        let mut attrs = self.attributes.iter().collect::<Vec<_>>();
        attrs.sort_by_key(|(k, _)| *k);
        for (k, v) in attrs {
            write!(f, " {}=\"{}\"", k, v)?;
        }
        if self.text.is_none() && self.children.is_empty() {
            f.write_str("/>\n")?;
        } else {
            f.write_str(">\n")?;
        }
        if let Some(t) = &self.text {
            f.write_str(t)?;
        }
        for child in &self.children {
            write!(f, "{}", child)?;
        }

        if self.text.is_some() || !self.children.is_empty() {
            writeln!(f, "</{}>", self.name)?;
        }
        for sibling in &self.siblings {
            write!(f, "{}", sibling)?;
        }
        Ok(())
    }
}