initial
This commit is contained in:
73
src/dom.rs
Normal file
73
src/dom.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
||||
struct Attr {
|
||||
attrs: HashMap<String, String>,
|
||||
}
|
||||
|
||||
struct EData {
|
||||
name: String,
|
||||
attr: Attr,
|
||||
}
|
||||
|
||||
enum NType {
|
||||
Text(String),
|
||||
Comment(String),
|
||||
Element(EData),
|
||||
}
|
||||
|
||||
|
||||
struct Node {
|
||||
children: Vec<Node>,
|
||||
ntype: NType,
|
||||
}
|
||||
|
||||
fn text(d: String) -> Node {
|
||||
Node { children: Vec::new(), ntype: NType::Text(d) }
|
||||
}
|
||||
|
||||
fn comment(d: String) -> Node {
|
||||
Node { children: Vec::new(), ntype: NType::Comment(d) }
|
||||
}
|
||||
|
||||
fn elem(name: String, attr: Attr, children: Vec<Node>) -> Node {
|
||||
Node {
|
||||
children: children,
|
||||
ntype: NType::Element(EData {
|
||||
name: name,
|
||||
attr: attr,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Node {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self.ntype {
|
||||
NType::Text(ref s) => write!(f, "{}", s),
|
||||
NType::Comment(ref s) => write!(f, "<!--{}-->", s),
|
||||
NType::Element(ref d) => {
|
||||
write!(f, "<{}", d.name);
|
||||
write!(f, "{}", d.attr);
|
||||
write!(f, ">");
|
||||
for child in self.children.iter() {
|
||||
write!(f, "{}", child);
|
||||
}
|
||||
write!(f, "</{}>", d.name)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Attr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
for (k, v) in self.attrs.iter() {
|
||||
write!(f, " {}=\"{}\"", k, v);
|
||||
}
|
||||
return Result::Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/*fn main() {
|
||||
let e = elem("html".to_string(), Attr{attrs:"ab".chars().map(|c| (c.to_string(), c.to_string())).collect::<HashMap<_, _>>()}, vec![elem("body".to_string(), Attr{attrs:HashMap::new()}, vec![text("hi".to_string())])]);
|
||||
println!("{}", e)
|
||||
}*/
|
Reference in New Issue
Block a user