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
use html_escape::encode_double_quoted_attribute_to_string;
pub use html_escape::encode_text_minimal_to_string as escape_inner;

pub trait Child: std::fmt::Display {}

impl<T: 'static> Child for T where T: std::fmt::Display {}

pub struct Tag {
    pub name: &'static str,
    pub attrs: Vec<(&'static str, Box<dyn std::fmt::Display>)>,
    pub children: Vec<Box<dyn Child>>,
}

impl Tag {
    pub fn with_children<V: Child + 'static, T: IntoIterator<Item = V>>(
        mut self,
        children: T,
    ) -> Self {
        self.children.clear();
        self.add_children(children)
    }

    pub fn add_children<V: Child + 'static, T: IntoIterator<Item = V>>(
        mut self,
        children: T,
    ) -> Self {
        self.children.extend(
            children
                .into_iter()
                .map(|v| -> Box<dyn Child> { Box::new(v) }),
        );
        self
    }
}

impl std::fmt::Display for Tag {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        use std::fmt::Write;

        write!(f, "<{}", self.name)?;
        let mut buf = String::new();
        let mut enbuf = String::new();
        for (k, v) in self.attrs.iter() {
            buf.clear();
            write!(&mut buf, "{}", v)?;
            if buf.is_empty() {
                continue;
            }
            enbuf.clear();
            encode_double_quoted_attribute_to_string(buf.as_str(), &mut enbuf);
            write!(f, " {}=\"{}\"", k, enbuf)?;
        }
        write!(f, ">")?;
        for child in self.children.iter() {
            // TODO:
            //  escape properly
            //    - ashkan, Wed 11 Nov 2020 01:05:34 AM JST
            write!(f, "{}", child)?;
        }
        write!(f, "</{}>", self.name)
        // match &self.children[..] {
        //     // [] => write!(f, "/>"),
        //     // [v] => {
        //     //     buf.clear();
        //     //     write!(&mut buf, "{}", v)?;
        //     //     if buf.is_empty() {
        //     //         return write!(f, "/>");
        //     //     }
        //     //     write!(f, ">{}</{}>", buf, self.name)
        //     // }
        //     children => {
        //         // TODO:
        //         //  use itertools .format()?
        //         //    - ashkan, Wed 07 Oct 2020 11:24:22 AM JST
        //         write!(f, ">")?;
        //         for child in children {
        //             // TODO:
        //             //  escape properly
        //             //    - ashkan, Wed 11 Nov 2020 01:05:34 AM JST
        //             write!(f, "{}", child)?;
        //         }
        //         write!(f, "</{}>", self.name)
        //     }
        // }
    }
}

#[macro_export]
macro_rules! tag {
  ($name:literal[$($k:literal=$v:expr)+] $($children:expr)*) => {
      $crate::Tag {
          name: $name,
          attrs: vec![$(($k, Box::new($v))),*],
          children: vec![$(Box::new($children)),*],
      }
  };
  ($name:literal $($children:expr)*) => {
      $crate::Tag {
          name: $name,
          attrs: vec![],
          children: vec![$(Box::new($children)),*],
      }
  };
}

#[macro_export]
macro_rules! _maybe {
    ($v:literal) => {
        Some($v)
    };
    ($v:expr) => {
        $v
    };
}

#[macro_export]
macro_rules! class {
  ($($v:expr),*) => {
      (&[$($crate::_maybe!($v)),*]).into_iter().flatten().join(" ")
  };
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(
            tag!("html" tag!("body" 123)).to_string().as_str(),
            "<html><body>123</body></html>"
        );
        assert_eq!(
            tag!("html" tag!("body" 123).add_children(vec![1,2,3].into_iter().map(|c| tag!("div" c))))
                .to_string()
                .as_str(),
            "<html><body>123<div>1</div><div>2</div><div>3</div></body></html>"
        );
    }
}