serde_html/traits/
mod.rs

1use alloc::borrow::Cow;
2use alloc::vec;
3use alloc::vec::Vec;
4use core::fmt::{Display, Formatter};
5use core::fmt::Debug;
6use core::ops::AddAssign;
7
8use indexmap::{IndexMap, IndexSet};
9
10use crate::values::AttributeValue;
11
12mod edit;
13mod show;
14
15
16/// The trait of all html elements
17#[allow(unused_variables)]
18pub trait Element {
19    /// Construct html elements
20    fn build(self) -> HtmlElement;
21
22    /// Get the tag of the element, all html elements must have tag
23    fn get_tag(&self) -> &str;
24
25    /// Change the tag of the element and return whether the change is successful
26    fn set_tag<S>(&mut self, tag: S) -> bool where S: Into<Cow<'static, str>> {
27        false
28    }
29    /// Get the html attribute based on the given name
30    fn get_attribute(&self, name: &str) -> Option<&AttributeValue> {
31        None
32    }
33    /// Get the mutable html attribute based on the given name
34    fn mut_attribute(&mut self, name: &str) -> Option<&mut AttributeValue> {
35        None
36    }
37    /// According to the given name, set the html attribute and return whether it is successful.
38    fn set_attribute<S>(&mut self, name: S, value: AttributeValue) -> bool where S: Into<Cow<'static, str>> {
39        false
40    }
41    /// Get the legal html id.
42    ///
43    /// Note that Some will be returned only when the id exists and the value is a string.
44    fn get_id(&self) -> Option<&str> {
45        match self.get_attribute("id")? {
46            AttributeValue::Constant(s) => { Some(s) }
47            AttributeValue::String(s) => { Some(s) }
48            _ => None
49        }
50    }
51    /// Set html id and return whether successful
52    fn set_id<S>(&mut self, id: S) -> bool where S: Into<Cow<'static, str>> {
53        match id.into() {
54            Cow::Borrowed(s) => {
55                self.set_attribute("id", AttributeValue::Constant(s))
56            }
57            Cow::Owned(s) => {
58                self.set_attribute("id", AttributeValue::String(s))
59            }
60        }
61    }
62    /// Get the legal html class.
63    fn get_classes(&self) -> impl Iterator<Item=&str> {
64        [].into_iter()
65    }
66    /// Set html class and return whether successful
67    fn set_classes<I>(&mut self, classes: I) -> bool where I: Iterator<Item=Cow<'static, str>> {
68        false
69    }
70    /// Add html class and return whether successful
71    fn add_class<S>(&mut self, class: S) -> bool where S: Into<Cow<'static, str>> {
72        false
73    }
74    /// Remove html class and return whether successful
75    fn remove_class<S>(&mut self, class: &str) -> bool {
76        false
77    }
78    /// Get the child element based on the given index
79    fn get_child(&self, index: usize) -> Option<&HtmlNode> {
80        self.get_children().nth(index)
81    }
82    /// Get the mutable child element based on the given index
83    fn mut_child(&mut self, index: usize) -> Option<&mut HtmlNode> {
84        self.mut_children().nth(index)
85    }
86    /// Set the child element based on the given index and return whether successful
87    fn set_child<T>(&mut self, index: usize, child: T) -> bool where T: Into<HtmlNode> {
88        match self.mut_children().nth(index) {
89            Some(s) => {
90                *s = child.into();
91                true
92            }
93            None => {
94                false
95            }
96        }
97    }
98    /// Add a child element and return whether successful
99    fn add_child<T>(&mut self, child: T) -> bool where T: Into<HtmlNode> {
100        false
101    }
102    /// Remove the child element based on the given index and return whether successful
103    fn get_children(&self) -> impl Iterator<Item=&HtmlNode> {
104        [].into_iter()
105    }
106    /// Get the mutable child element based on the given index
107    fn mut_children(&mut self) -> impl Iterator<Item=&mut HtmlNode> {
108        [].into_iter()
109    }
110    /// Set the child element based on the given index and return whether successful
111    fn set_children<I>(&mut self, children: I) -> bool where I: Iterator<Item=HtmlNode> {
112        false
113    }
114}
115
116///
117#[derive(Clone, Debug)]
118pub struct HtmlElement {
119    tag: Cow<'static, str>,
120    classes: IndexSet<Cow<'static, str>>,
121    attributes: IndexMap<Cow<'static, str>, AttributeValue>,
122    children: Vec<HtmlNode>,
123}
124
125#[derive(Clone, Debug)]
126pub enum HtmlNode {
127    Text(Cow<'static, str>),
128    Comment(Cow<'static, str>),
129    Standard(HtmlElement),
130}
131