diff --git a/src/main.rs b/src/main.rs index 6d1dc37..7cc37fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,6 @@ use std::borrow::Borrow; #[grammar = "../pest/qml.pest"] struct QmlParser; - #[derive(Debug, Clone)] enum Value { QmlString(String), @@ -76,15 +75,16 @@ fn parse_qml(qml: Pairs) -> QML { } _ => {} } - }, - Rule::function => { - let mut tokens = pair.into_inner(); - let ident = tokens.next().unwrap().as_str(); - let value = tokens.concat(); - qmldoc - .properties - .insert(String::from(ident), QmlString(String::from(value.clone().trim()))); - } + } + Rule::function => { + let mut tokens = pair.into_inner(); + let ident = tokens.next().unwrap().as_str(); + let value = tokens.concat(); + qmldoc.properties.insert( + String::from(ident), + QmlString(String::from(value.clone().trim())), + ); + } _ => return parse_qml(pair.into_inner()), } } @@ -99,17 +99,11 @@ fn main() { let qml_doc = parse_qml(qml_tokens); let top_level = qml_doc.children.clone(); - let width = match top_level[0].1 - .properties - .get("width") - { + let width = match top_level[0].1.properties.get("width") { Some(QmlNumber(num)) => num.clone(), _ => 600.0, }; - let height = match top_level[0].1 - .properties - .get("height") - { + let height = match top_level[0].1.properties.get("height") { Some(QmlNumber(num)) => num.clone(), _ => 600.0, }; @@ -119,7 +113,7 @@ fn main() { .position((100.0, 100.0)) .resizeable(true) .size(width, height) - .child(render_ctx(ctx, &top_level, 0,0,&HashMap::new()).unwrap()) + .child(render_ctx(ctx, &top_level, 0, 0, &HashMap::new()).unwrap()) .build(ctx) }) .run(); @@ -128,17 +122,21 @@ fn main() { fn parse_number(val: Option<&Value>, env: &HashMap) -> f64 { match val { Some(QmlNumber(num)) => num.clone(), - Some(QmlIdent(ident)) => { - match env.get(ident) { - Some(QmlNumber(num)) => num.clone(), - _ => 0.0 - } - } + Some(QmlIdent(ident)) => match env.get(ident) { + Some(QmlNumber(num)) => num.clone(), + _ => 0.0, + }, _ => 0.0, } } -fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u32, env: &HashMap) -> Option { +fn render_ctx( + ctx: &mut BuildContext, + qml: &Vec<(String, QML)>, + row: u32, + col: u32, + env: &HashMap, +) -> Option { for (ident, child) in qml.iter() { match ident.as_str() { "Grid" => { @@ -160,27 +158,25 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u } grid = grid.columns(grid_cols.build()); - let mut grow = 0u32; let mut gcol = 0u32; //rect = rect.attach(Grid::column(gcol as usize)); - for (i,s) in child.children.iter() { - - match render_ctx(ctx, &vec![(i.clone(),s.clone())],grow,gcol, &env) { + for (i, s) in child.children.iter() { + match render_ctx(ctx, &vec![(i.clone(), s.clone())], grow, gcol, &env) { Some(entity) => { grid = grid.child(entity); - grow+=1; + grow += 1; if grow as u32 == rows { grow = 0; - gcol+=1; + gcol += 1; } - }, + } _ => {} } } return Some(grid.build(ctx)); - }, + } "Rectangle" => { let width = parse_number(child.properties.get("width"), &env); let height = parse_number(child.properties.get("height"), &env); @@ -194,21 +190,22 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u _ => Color::rgb(0xff, 0xff, 0xff), }; let mut rect = Container::create() - // .width(width) - // .height(height) + // .width(width) + // .height(height) .background(color); rect = rect.attach(Grid::row(row as usize)); rect = rect.attach(Grid::column(col as usize)); - let mut env = env.clone(); env.insert(String::from("parent.width"), QmlNumber(width)); env.insert(String::from("parent.height"), QmlNumber(height)); - for (i,s) in child.children.iter() { - match render_ctx(ctx, &vec![(i.clone(),s.clone())],0,0, &env) { - Some(entity) => { rect = rect.child(entity); }, + for (i, s) in child.children.iter() { + match render_ctx(ctx, &vec![(i.clone(), s.clone())], 0, 0, &env) { + Some(entity) => { + rect = rect.child(entity); + } _ => {} } } @@ -222,14 +219,12 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u let code = match child.properties.get("onclick").unwrap() { QmlString(code) => code.clone(), - _ => String::new() + _ => String::new(), }; - button = button.on_click(move - |x,y| -> bool { + button = button.on_click(move |x, y| -> bool { rhai_script(code.clone()); - return true; - } - ); + return true; + }); let text = match child.properties.get("text").unwrap() { QmlString(text) => text.clone(), @@ -261,7 +256,7 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u tt = tt.attach(Grid::column(col as usize)); match child.properties.get("anchors.centerIn") { - Some(QmlIdent(str)) => { + Some(QmlIdent(str)) => { if str.eq("parent") { tt = tt.vertical_alignment(Alignment::Center); tt = tt.horizontal_alignment(Alignment::Center); @@ -281,4 +276,4 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u fn rhai_script(run: String) { let mut engine = Engine::new(); engine.eval::<()>(run.as_str()); -} \ No newline at end of file +}