Rename ParsedLayoutNode to LayoutParsedNode

This commit is contained in:
Keavon Chambers 2020-05-25 22:24:54 -07:00
parent f8025b15ea
commit 870ce74743
4 changed files with 23 additions and 19 deletions

View File

@ -1,6 +1,6 @@
use std::fs; use std::fs;
use std::io; use std::io;
use crate::parsed_layout_node::*; use crate::layout_parsed_node::*;
pub struct ComponentLayout { pub struct ComponentLayout {
@ -9,19 +9,17 @@ pub struct ComponentLayout {
impl ComponentLayout { impl ComponentLayout {
pub fn new() -> ComponentLayout { pub fn new() -> ComponentLayout {
let parsed_layout_tree = Self::parse_xml_file("gui/window/main.xml").unwrap(); let parsed_layout_tree = Self::parse_xml_file("gui/window/main.xml").unwrap();
for node in parsed_layout_tree.descendants() { Self::interpret_abstract_syntax_tree(parsed_layout_tree);
println!("{:?}", node);
}
Self {} Self {}
} }
pub fn parse_xml_file(path: &str) -> io::Result<rctree::Node<ParsedLayoutNode>> { pub fn parse_xml_file(path: &str) -> io::Result<rctree::Node<LayoutParsedNode>> {
let source = fs::read_to_string(path)?; let source = fs::read_to_string(path)?;
let parsed = xmlparser::Tokenizer::from(&source[..]); let parsed = xmlparser::Tokenizer::from(&source[..]);
let mut stack: Vec<rctree::Node<ParsedLayoutNode>> = Vec::new(); let mut stack: Vec<rctree::Node<LayoutParsedNode>> = Vec::new();
let mut current: Option<rctree::Node<ParsedLayoutNode>> = None; let mut current: Option<rctree::Node<LayoutParsedNode>> = None;
let mut result: Option<rctree::Node<ParsedLayoutNode>> = None; let mut result: Option<rctree::Node<LayoutParsedNode>> = None;
for token in parsed { for token in parsed {
match token.unwrap() { match token.unwrap() {
@ -29,7 +27,7 @@ impl ComponentLayout {
let namespace = String::from(prefix.as_str()); let namespace = String::from(prefix.as_str());
let tag_name = String::from(local.as_str()); let tag_name = String::from(local.as_str());
let new_parsed_layout_node = ParsedLayoutNode::new_tag(namespace, tag_name); let new_parsed_layout_node = LayoutParsedNode::new_tag(namespace, tag_name);
let new_node = rctree::Node::new(new_parsed_layout_node); let new_node = rctree::Node::new(new_parsed_layout_node);
current = Some(new_node); current = Some(new_node);
@ -49,11 +47,11 @@ impl ComponentLayout {
match &mut current { match &mut current {
Some(current_node) => { Some(current_node) => {
match &mut *current_node.borrow_mut() { match &mut *current_node.borrow_mut() {
ParsedLayoutNode::Tag(tag) => { LayoutParsedNode::Tag(tag) => {
// Add this attribute to the current node that has not yet reached its closing angle bracket // Add this attribute to the current node that has not yet reached its closing angle bracket
tag.add_attribute(attribute); tag.add_attribute(attribute);
} }
ParsedLayoutNode::Text(_) => { LayoutParsedNode::Text(_) => {
panic!("Error adding attribute to tag when parsing XML layout in file: {}", path); panic!("Error adding attribute to tag when parsing XML layout in file: {}", path);
} }
} }
@ -99,7 +97,7 @@ impl ComponentLayout {
let text_string = String::from(text.as_str()); let text_string = String::from(text.as_str());
if !text_string.trim().is_empty() { if !text_string.trim().is_empty() {
let text_node = ParsedLayoutNode::new_text(text_string); let text_node = LayoutParsedNode::new_text(text_string);
let new_node = rctree::Node::new(text_node); let new_node = rctree::Node::new(text_node);
parent_node.append(new_node); parent_node.append(new_node);
} }
@ -113,4 +111,10 @@ impl ComponentLayout {
Some(tree) => Ok(tree) Some(tree) => Ok(tree)
} }
} }
pub fn interpret_abstract_syntax_tree(root: rctree::Node<LayoutParsedNode>) {
for node in root.descendants() {
println!("{:?}", node);
}
}
} }

View File

View File

@ -1,12 +1,12 @@
#[derive(Debug)] #[derive(Debug)]
pub enum ParsedLayoutNode { pub enum LayoutParsedNode {
Tag(ParsedLayoutTag), Tag(LayoutParsedTag),
Text(String), Text(String),
} }
impl ParsedLayoutNode { impl LayoutParsedNode {
pub fn new_tag(namespace: String, tag: String) -> Self { pub fn new_tag(namespace: String, tag: String) -> Self {
Self::Tag(ParsedLayoutTag::new(namespace, tag)) Self::Tag(LayoutParsedTag::new(namespace, tag))
} }
pub fn new_text(text: String) -> Self { pub fn new_text(text: String) -> Self {
@ -15,13 +15,13 @@ impl ParsedLayoutNode {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct ParsedLayoutTag { pub struct LayoutParsedTag {
pub namespace: Option<String>, pub namespace: Option<String>,
pub tag: String, pub tag: String,
pub attributes: Vec<(String, String)>, pub attributes: Vec<(String, String)>,
} }
impl ParsedLayoutTag { impl LayoutParsedTag {
pub fn new(namespace: String, tag: String) -> Self { pub fn new(namespace: String, tag: String) -> Self {
let namespace = if namespace.is_empty() { None } else { Some(namespace) }; let namespace = if namespace.is_empty() { None } else { Some(namespace) };

View File

@ -10,7 +10,7 @@ mod gui_node;
mod gui_attributes; mod gui_attributes;
mod window_events; mod window_events;
mod component_layout; mod component_layout;
mod parsed_layout_node; mod layout_parsed_node;
use application::Application; use application::Application;
use winit::event_loop::EventLoop; use winit::event_loop::EventLoop;