2025-10-30 14:44:41 -06:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
2025-10-31 12:55:51 -06:00
|
|
|
use crate::{
|
|
|
|
|
log,
|
|
|
|
|
views::{Bounds, BoxView, ColorRectView, ConstraintLayout, View},
|
|
|
|
|
};
|
2025-10-30 14:44:41 -06:00
|
|
|
|
|
|
|
|
mod parser;
|
|
|
|
|
|
|
|
|
|
pub const TEST_XML: &'static str = include_str!("../../pages/main.xml");
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2025-10-31 12:55:51 -06:00
|
|
|
pub struct Tag {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub children: Vec<Tag>,
|
|
|
|
|
pub attributes: HashMap<String, String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Tag {
|
|
|
|
|
pub fn parse(&self) -> Box<dyn View> {
|
|
|
|
|
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<String, String>) -> (Option<Bounds>, Option<Bounds>) {
|
|
|
|
|
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::<i32>() {
|
|
|
|
|
int as f32
|
|
|
|
|
} else if let Ok(float) = str.parse::<f32>() {
|
|
|
|
|
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)
|
|
|
|
|
}
|
2025-10-30 14:44:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn parse(xml: &str) -> Box<dyn View> {
|
2025-10-31 12:55:51 -06:00
|
|
|
parser::parse_xml(xml).parse()
|
2025-10-30 14:44:41 -06:00
|
|
|
}
|