mirror of
https://github.com/Astatin3/IntroToWebAuthoring.git
synced 2026-06-09 00:28:00 -06:00
Get constraint layout work with xml
This commit is contained in:
+51
-8
@@ -1,20 +1,63 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::views::View;
|
||||
use crate::{
|
||||
log,
|
||||
views::{Bounds, BoxView, ColorRectView, ConstraintLayout, View},
|
||||
};
|
||||
|
||||
mod interpreter;
|
||||
mod parser;
|
||||
|
||||
pub const TEST_XML: &'static str = include_str!("../../pages/main.xml");
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Tag {
|
||||
name: String,
|
||||
children: Vec<Tag>,
|
||||
attributes: HashMap<String, String>,
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(xml: &str) -> Box<dyn View> {
|
||||
let tag = parser::parse_xml(xml);
|
||||
interpreter::interpret_tag(tag)
|
||||
parser::parse_xml(xml).parse()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user