cercis_html/
attribute.rs

1use std::borrow::Cow;
2
3use html_escape::encode_safe;
4
5#[derive(Clone)]
6pub struct VAttribute<'a> {
7    name: Cow<'a, str>,
8    value: Option<Cow<'a, str>>,
9    is_raw: Option<bool>,
10}
11
12impl<'a> VAttribute<'a> {
13    pub fn new(name: impl Into<Cow<'a, str>>) -> Self {
14        Self {
15            name: name.into(),
16            value: None,
17            is_raw: None,
18        }
19    }
20
21    pub fn value(mut self, value: impl Into<Cow<'a, str>>) -> Self {
22        self.value = Some(value.into());
23
24        self
25    }
26
27    pub fn raw(mut self, value: bool) -> Self {
28        self.is_raw = Some(value);
29
30        self
31    }
32
33    pub fn render(&self) -> String {
34        match self.value.as_ref() {
35            Some(value) => match self.is_raw {
36                Some(val) if val => format!(" {}='{}'", self.name, value),
37                _ => format!(" {}='{}'", self.name, encode_safe(&value)),
38            },
39            None => format!(" {}", self.name),
40        }
41    }
42}