use std::collections::HashMap; use crate::{ log, views::{Bounds, BoxView, ColorRectView, ConstraintLayout, View}, }; mod parser; pub const TEST_XML: &'static str = include_str!("../../pages/main.xml"); #[derive(Debug)] pub struct Tag { pub name: String, pub children: Vec, pub attributes: HashMap, } impl Tag { pub fn parse(&self) -> Box { match self.name.as_str() { "ColoredRectView" => ColorRectView::from_tag(&self.attributes, &self.children), "ConstraintLayout" => ConstraintLayout::from_tag(&self.attributes, &self.children), "BoxView" => BoxView::from_tag(&self.attributes, &self.children), _ => panic!("Unknown tag: {}", self.name), } } pub fn parse_bounds(attributes: &HashMap) -> (Option, Option) { let (mut width, mut height) = (None, None); let parse_bound = |str: &str| -> Bounds { match str { "parent" => Bounds::MatchParent, _ => { let bound = if let Ok(int) = str.parse::() { int as f32 } else if let Ok(float) = str.parse::() { float } else { panic!("Invalid String"); }; Bounds::Pixels(bound) } } }; for (key, value) in attributes { match key.as_str() { "width" => width = Some(parse_bound(value)), "height" => height = Some(parse_bound(value)), _ => {} } } (width, height) } } pub fn parse(xml: &str) -> Box { parser::parse_xml(xml).parse() }