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
use std::{
    fmt::Display,
    str::FromStr,
};

#[derive(Clone, Copy, PartialEq, Debug, Hash)]
pub enum TagName {
    Root,
    Rect,
    Paragraph,
    Label,
    Text,
    Image,
    Svg,
}

impl TagName {
    pub fn has_intrinsic_layout(&self) -> bool {
        *self != Self::Text
    }

    pub fn has_children_with_intrinsic_layout(&self) -> bool {
        *self != Self::Paragraph && *self != Self::Label
    }

    pub fn contains_text(&self) -> bool {
        matches!(self, Self::Paragraph | Self::Label | Self::Text)
    }
}

impl FromStr for TagName {
    type Err = ();

    fn from_str(txt: &str) -> Result<Self, Self::Err> {
        match txt {
            "rect" => Ok(TagName::Rect),
            "paragraph" => Ok(TagName::Paragraph),
            "label" => Ok(TagName::Label),
            "text" => Ok(TagName::Text),
            "image" => Ok(TagName::Image),
            "svg" => Ok(TagName::Svg),
            _ => Err(()),
        }
    }
}

impl Display for TagName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TagName::Root => f.write_str("root"),
            TagName::Rect => f.write_str("rect"),
            TagName::Paragraph => f.write_str("p"),
            TagName::Label => f.write_str("label"),
            TagName::Text => f.write_str("text"),
            TagName::Image => f.write_str("img"),
            TagName::Svg => f.write_str("svg"),
        }
    }
}