pub mod comment;
pub mod tag;
pub mod text;
pub use comment::Comment;
pub use tag::Tag;
pub use text::Text;
#[derive(Debug)]
pub enum DomType {
Tag,
Text,
Comment,
}
#[derive(Debug)]
pub struct Dom {
pub dom_type: DomType,
tag: Option<Tag>,
text: Option<Text>,
comment: Option<Comment>,
children: Option<Vec<Box<Dom>>>,
}
impl Dom {
pub fn new(dom_type: DomType) -> Dom {
Dom {
dom_type,
tag: None,
text: None,
comment: None,
children: None,
}
}
fn domtype_str(&self) -> String {
match self.dom_type {
DomType::Tag => return String::from("Tag"),
DomType::Text => return String::from("Text"),
DomType::Comment => return String::from("Comment"),
}
}
pub fn set_tag(&mut self, tag: Tag) {
match self.dom_type {
DomType::Tag => self.tag = Some(tag),
_ => panic!("invalid DomType. expect Tag but {}", self.domtype_str()),
}
}
pub fn get_tag(&self) -> Option<&Tag> {
self.tag.as_ref()
}
pub fn set_text(&mut self, text: Text) {
match self.dom_type {
DomType::Text => self.text = Some(text),
_ => panic!("invalid DomType. expect Text but {}", self.domtype_str()),
}
}
pub fn get_text(&self) -> Option<&Text> {
self.text.as_ref()
}
pub fn set_comment(&mut self, comment: Comment) {
match self.dom_type {
DomType::Comment => self.comment = Some(comment),
_ => panic!("invalid DomType. expect Comment but {}", self.domtype_str()),
}
}
pub fn get_comment(&self) -> Option<&Comment> {
self.comment.as_ref()
}
pub fn add_child(&mut self, dom: Dom) {
let dom = Box::new(dom);
match &mut self.children {
Some(children) => {
children.push(dom);
}
None => {
let mut children = Vec::new();
children.push(dom);
self.children = Some(children);
}
}
}
pub fn get_children(&self) -> Option<&Vec<Box<Dom>>> {
self.children.as_ref()
}
}