svg/node/
mod.rs

1//! The nodes.
2
3use std::collections::hash_map::DefaultHasher;
4use std::collections::HashMap;
5use std::fmt;
6
7mod blob;
8mod comment;
9mod text;
10mod value;
11
12pub use self::blob::Blob;
13pub use self::comment::Comment;
14pub use self::text::Text;
15pub use self::value::Value;
16
17/// Attributes.
18pub type Attributes = HashMap<String, Value>;
19
20/// Child nodes.
21pub type Children = Vec<Box<dyn Node>>;
22
23/// A node.
24pub trait Node:
25    'static + fmt::Debug + fmt::Display + NodeClone + NodeDefaultHash + Send + Sync
26{
27    /// Append a child node.
28    #[inline]
29    fn append<T>(&mut self, _: T)
30    where
31        Self: Sized,
32        T: Into<Box<dyn Node>>,
33    {
34    }
35
36    /// Assign an attribute.
37    #[inline]
38    fn assign<T, U>(&mut self, _: T, _: U)
39    where
40        Self: Sized,
41        T: Into<String>,
42        U: Into<Value>,
43    {
44    }
45
46    /// Return the name.
47    fn get_name(&self) -> &str;
48
49    /// Return the attributes.
50    #[inline]
51    fn get_attributes(&self) -> Option<&Attributes> {
52        None
53    }
54
55    /// Return the attributes as mutable.
56    fn get_attributes_mut(&mut self) -> Option<&mut Attributes> {
57        None
58    }
59
60    /// Return the children.
61    fn get_children(&self) -> Option<&Children> {
62        None
63    }
64
65    /// Return the children as mutable.
66    fn get_children_mut(&mut self) -> Option<&mut Children> {
67        None
68    }
69
70    #[doc(hidden)]
71    fn is_bare(&self) -> bool {
72        false
73    }
74
75    #[doc(hidden)]
76    fn is_bareable(&self) -> bool {
77        false
78    }
79}
80
81#[doc(hidden)]
82pub trait NodeClone {
83    fn clone(&self) -> Box<dyn Node>;
84}
85
86#[doc(hidden)]
87pub trait NodeDefaultHash {
88    fn default_hash(&self, state: &mut DefaultHasher);
89}
90
91impl<T> NodeClone for T
92where
93    T: Node + Clone,
94{
95    #[inline]
96    fn clone(&self) -> Box<dyn Node> {
97        Box::new(Clone::clone(self))
98    }
99}
100
101impl Clone for Box<dyn Node> {
102    #[inline]
103    fn clone(&self) -> Self {
104        NodeClone::clone(&**self)
105    }
106}
107
108impl<T> From<T> for Box<dyn Node>
109where
110    T: Node,
111{
112    #[inline]
113    fn from(node: T) -> Box<dyn Node> {
114        Box::new(node)
115    }
116}
117
118impl NodeDefaultHash for Box<dyn Node> {
119    #[inline]
120    fn default_hash(&self, state: &mut DefaultHasher) {
121        NodeDefaultHash::default_hash(&**self, state)
122    }
123}
124
125pub mod element;