diff --git a/src/build/mod.rs b/src/build/mod.rs index 149919e..690181b 100644 --- a/src/build/mod.rs +++ b/src/build/mod.rs @@ -47,7 +47,7 @@ impl MacroDef { root: PathBuf, parent_builder: &FileBuilder, mut args: Vec>, - ) -> Result, Rc)>, Box> { + ) -> Result, Rc)>, Box> { // Error conditions. If the args don't match the length and types of the argdefs then this is // macro call error. if args.len() > self.argdefs.len() { @@ -83,7 +83,7 @@ impl MacroDef { } /// The result of a build. -type BuildResult = Result<(), Box>; +type BuildResult = Result<(), Box>; /// AssertCollector collects the results of assertions in the UCG AST. pub struct AssertCollector { @@ -204,7 +204,7 @@ impl<'a> FileBuilder<'a> { &mut self, fields: &Vec<(Token, Expression)>, scope: &Scope, - ) -> Result, Box> { + ) -> Result, Box> { let mut new_fields = Vec::<(PositionedItem, Rc)>::new(); for &(ref name, ref expr) in fields.iter() { let val = self.eval_expr(expr, scope)?; @@ -213,7 +213,7 @@ impl<'a> FileBuilder<'a> { Ok(Rc::new(Val::Tuple(new_fields))) } - fn eval_list(&mut self, def: &ListDef, scope: &Scope) -> Result, Box> { + fn eval_list(&mut self, def: &ListDef, scope: &Scope) -> Result, Box> { let mut vals = Vec::new(); for expr in def.elems.iter() { vals.push(self.eval_expr(expr, scope)?); @@ -221,7 +221,7 @@ impl<'a> FileBuilder<'a> { Ok(Rc::new(Val::List(vals))) } - fn eval_value(&mut self, v: &Value, scope: &Scope) -> Result, Box> { + fn eval_value(&mut self, v: &Value, scope: &Scope) -> Result, Box> { match v { &Value::Empty(_) => Ok(Rc::new(Val::Empty)), &Value::Boolean(ref b) => Ok(Rc::new(Val::Boolean(b.val))), @@ -267,7 +267,7 @@ impl<'a> FileBuilder<'a> { Ok(()) } - fn eval_input(&mut self, input: OffsetStrIter) -> Result, Box> { + fn eval_input(&mut self, input: OffsetStrIter) -> Result, Box> { match parse(input.clone()) { Ok(stmts) => { //panic!("Successfully parsed {}", input); @@ -289,7 +289,7 @@ impl<'a> FileBuilder<'a> { } /// Evaluate an input string as UCG. - pub fn eval_string(&mut self, input: &str) -> Result, Box> { + pub fn eval_string(&mut self, input: &str) -> Result, Box> { self.eval_input(OffsetStrIter::new(input)) } @@ -338,7 +338,7 @@ impl<'a> FileBuilder<'a> { &mut self, path: P, use_import_path: bool, - ) -> Result> { + ) -> Result> { // Try a relative path first. let path = path.into(); let mut normalized = self.file.parent().unwrap().to_path_buf(); @@ -361,7 +361,7 @@ impl<'a> FileBuilder<'a> { Ok(normalized.canonicalize()?) } - fn eval_import(&mut self, def: &ImportDef) -> Result, Box> { + fn eval_import(&mut self, def: &ImportDef) -> Result, Box> { let sym = &def.name; if Self::check_reserved_word(&sym.fragment) { return Err(Box::new(error::BuildError::new( @@ -412,7 +412,7 @@ impl<'a> FileBuilder<'a> { return Ok(result); } - fn eval_let(&mut self, def: &LetDef) -> Result, Box> { + fn eval_let(&mut self, def: &LetDef) -> Result, Box> { let child_scope = self.scope.clone(); let val = self.eval_expr(&def.value, &child_scope)?; let name = &def.name; @@ -443,7 +443,7 @@ impl<'a> FileBuilder<'a> { Ok(val) } - fn eval_stmt(&mut self, stmt: &Statement) -> Result, Box> { + fn eval_stmt(&mut self, stmt: &Statement) -> Result, Box> { let child_scope = self.scope.clone(); match stmt { &Statement::Assert(ref expr) => self.build_assert(&expr), @@ -473,7 +473,7 @@ impl<'a> FileBuilder<'a> { pos: &Position, left: Rc, right: Rc, - ) -> Result, Box> { + ) -> Result, Box> { match *left { Val::Int(i) => { eval_binary_expr!(&Val::Int(ii), pos, right, Val::Int(i + ii), "Integer") @@ -535,7 +535,7 @@ impl<'a> FileBuilder<'a> { pos: &Position, left: Rc, right: Rc, - ) -> Result, Box> { + ) -> Result, Box> { match *left { Val::Int(i) => { eval_binary_expr!(&Val::Int(ii), pos, right, Val::Int(i - ii), "Integer") @@ -558,7 +558,7 @@ impl<'a> FileBuilder<'a> { pos: &Position, left: Rc, right: Rc, - ) -> Result, Box> { + ) -> Result, Box> { match *left { Val::Int(i) => { eval_binary_expr!(&Val::Int(ii), pos, right, Val::Int(i * ii), "Integer") @@ -581,7 +581,7 @@ impl<'a> FileBuilder<'a> { pos: &Position, left: Rc, right: Rc, - ) -> Result, Box> { + ) -> Result, Box> { match *left { Val::Int(i) => { eval_binary_expr!(&Val::Int(ii), pos, right, Val::Int(i / ii), "Integer") @@ -604,7 +604,7 @@ impl<'a> FileBuilder<'a> { pos: &Position, left: Rc, right: Rc, - ) -> Result, Box> { + ) -> Result, Box> { Ok(Rc::new(Val::Boolean( left.equal(right.as_ref(), pos.clone())?, ))) @@ -615,13 +615,18 @@ impl<'a> FileBuilder<'a> { pos: &Position, left: Rc, right: Rc, - ) -> Result, Box> { + ) -> Result, Box> { Ok(Rc::new(Val::Boolean( !left.equal(right.as_ref(), pos.clone())?, ))) } - fn do_gt(&self, pos: &Position, left: Rc, right: Rc) -> Result, Box> { + fn do_gt( + &self, + pos: &Position, + left: Rc, + right: Rc, + ) -> Result, Box> { // first ensure that left and right are numeric vals of the same type. if let &Val::Int(ref l) = left.as_ref() { if let &Val::Int(ref r) = right.as_ref() { @@ -644,7 +649,12 @@ impl<'a> FileBuilder<'a> { ))) } - fn do_lt(&self, pos: &Position, left: Rc, right: Rc) -> Result, Box> { + fn do_lt( + &self, + pos: &Position, + left: Rc, + right: Rc, + ) -> Result, Box> { // first ensure that left and right are numeric vals of the same type. if let &Val::Int(ref l) = left.as_ref() { if let &Val::Int(ref r) = right.as_ref() { @@ -672,7 +682,7 @@ impl<'a> FileBuilder<'a> { pos: &Position, left: Rc, right: Rc, - ) -> Result, Box> { + ) -> Result, Box> { if let &Val::Int(ref l) = left.as_ref() { if let &Val::Int(ref r) = right.as_ref() { return Ok(Rc::new(Val::Boolean(l <= r))); @@ -699,7 +709,7 @@ impl<'a> FileBuilder<'a> { pos: &Position, left: Rc, right: Rc, - ) -> Result, Box> { + ) -> Result, Box> { if let &Val::Int(ref l) = left.as_ref() { if let &Val::Int(ref r) = right.as_ref() { return Ok(Rc::new(Val::Boolean(l >= r))); @@ -721,7 +731,11 @@ impl<'a> FileBuilder<'a> { ))) } - fn do_dot_lookup(&mut self, right: &Expression, scope: &Scope) -> Result, Box> { + fn do_dot_lookup( + &mut self, + right: &Expression, + scope: &Scope, + ) -> Result, Box> { match right { Expression::Copy(_) => return self.eval_expr(right, scope), Expression::Call(_) => return self.eval_expr(right, scope), @@ -743,7 +757,7 @@ impl<'a> FileBuilder<'a> { left: &Expression, right: &Expression, scope: &Scope, - ) -> Result, Box> { + ) -> Result, Box> { // First we evaluate our right hand side so we have a something to search // inside for our left hand expression. let right_pos = right.pos().clone(); @@ -785,7 +799,7 @@ impl<'a> FileBuilder<'a> { } } - fn eval_binary(&mut self, def: &BinaryOpDef, scope: &Scope) -> Result, Box> { + fn eval_binary(&mut self, def: &BinaryOpDef, scope: &Scope) -> Result, Box> { let kind = &def.kind; if let &BinaryExprType::IN = kind { return self.do_element_check(&def.left, &def.right, scope); @@ -830,7 +844,7 @@ impl<'a> FileBuilder<'a> { src_fields: &Vec<(PositionedItem, Rc)>, overrides: &Vec<(Token, Expression)>, scope: &Scope, - ) -> Result, Box> { + ) -> Result, Box> { let mut m = HashMap::, (i32, Rc)>::new(); // loop through fields and build up a hashmap let mut count = 0; @@ -903,7 +917,7 @@ impl<'a> FileBuilder<'a> { ))); } - fn eval_copy(&mut self, def: &CopyDef, scope: &Scope) -> Result, Box> { + fn eval_copy(&mut self, def: &CopyDef, scope: &Scope) -> Result, Box> { let v = self.eval_value(&def.selector, scope)?; if let &Val::Tuple(ref src_fields) = v.as_ref() { let mut child_scope = scope.spawn_child(); @@ -966,7 +980,7 @@ impl<'a> FileBuilder<'a> { ))) } - fn eval_format(&mut self, def: &FormatDef, scope: &Scope) -> Result, Box> { + fn eval_format(&mut self, def: &FormatDef, scope: &Scope) -> Result, Box> { let tmpl = &def.template; let args = &def.args; let mut vals = Vec::new(); @@ -978,7 +992,7 @@ impl<'a> FileBuilder<'a> { Ok(Rc::new(Val::Str(formatter.render(&def.pos)?))) } - fn eval_call(&mut self, def: &CallDef, scope: &Scope) -> Result, Box> { + fn eval_call(&mut self, def: &CallDef, scope: &Scope) -> Result, Box> { let args = &def.arglist; let v = self.eval_value(&def.macroref, scope)?; if let &Val::Macro(ref m) = v.deref() { @@ -998,7 +1012,7 @@ impl<'a> FileBuilder<'a> { ))) } - fn eval_macro_def(&self, def: &MacroDef) -> Result, Box> { + fn eval_macro_def(&self, def: &MacroDef) -> Result, Box> { match def.validate_symbols() { Ok(()) => Ok(Rc::new(Val::Macro(def.clone()))), Err(set) => Err(Box::new(error::BuildError::new( @@ -1023,7 +1037,11 @@ impl<'a> FileBuilder<'a> { }; } - fn eval_module_def(&mut self, def: &ModuleDef, scope: &Scope) -> Result, Box> { + fn eval_module_def( + &mut self, + def: &ModuleDef, + scope: &Scope, + ) -> Result, Box> { let root = self.file_dir(); // Always work on a copy. The original should not be modified. let mut def = def.clone(); @@ -1035,7 +1053,7 @@ impl<'a> FileBuilder<'a> { Ok(Rc::new(Val::Module(def))) } - fn eval_select(&mut self, def: &SelectDef, scope: &Scope) -> Result, Box> { + fn eval_select(&mut self, def: &SelectDef, scope: &Scope) -> Result, Box> { let target = &def.val; let def_expr = &def.default; let fields = &def.tuple; @@ -1076,7 +1094,7 @@ impl<'a> FileBuilder<'a> { } } - fn eval_list_op(&mut self, def: &ListOpDef, scope: &Scope) -> Result, Box> { + fn eval_list_op(&mut self, def: &ListOpDef, scope: &Scope) -> Result, Box> { let maybe_list = self.eval_expr(&def.target, scope)?; let l = match maybe_list.as_ref() { &Val::List(ref elems) => elems, @@ -1121,7 +1139,7 @@ impl<'a> FileBuilder<'a> { ))); } - fn build_assert(&mut self, tok: &Token) -> Result, Box> { + fn build_assert(&mut self, tok: &Token) -> Result, Box> { if !self.validate_mode { // we are not in validate_mode then build_asserts are noops. return Ok(Rc::new(Val::Empty)); @@ -1176,7 +1194,7 @@ impl<'a> FileBuilder<'a> { Ok(ok) } - pub fn eval_include(&mut self, def: &IncludeDef) -> Result, Box> { + pub fn eval_include(&mut self, def: &IncludeDef) -> Result, Box> { return if def.typ.fragment == "str" { let normalized = match self.find_file(&def.path.fragment, false) { Ok(p) => p, @@ -1214,7 +1232,11 @@ impl<'a> FileBuilder<'a> { // Evals a single Expression in the context of a running Builder. // It does not mutate the builders collected state at all. - pub fn eval_expr(&mut self, expr: &Expression, scope: &Scope) -> Result, Box> { + pub fn eval_expr( + &mut self, + expr: &Expression, + scope: &Scope, + ) -> Result, Box> { match expr { &Expression::Simple(ref val) => self.eval_value(val, scope), &Expression::Binary(ref def) => self.eval_binary(def, scope), diff --git a/src/build/scope.rs b/src/build/scope.rs index b31913c..a407f08 100644 --- a/src/build/scope.rs +++ b/src/build/scope.rs @@ -97,7 +97,7 @@ impl Scope { } /// Lookup up a list index in the current value - pub fn lookup_idx(&self, pos: &Position, idx: &Val) -> Result, Box> { + pub fn lookup_idx(&self, pos: &Position, idx: &Val) -> Result, Box> { if self.search_curr_val && self.curr_val.is_some() { if let &Val::List(ref fs) = self.curr_val.as_ref().unwrap().as_ref() { return Self::lookup_in_list(pos, idx, fs); @@ -151,7 +151,7 @@ impl Scope { pos: &Position, field: &str, fs: &Vec<(PositionedItem, Rc)>, - ) -> Result, Box> { + ) -> Result, Box> { if let Some(vv) = find_in_fieldlist(&field, fs) { Ok(vv) } else { @@ -167,7 +167,7 @@ impl Scope { pos: &Position, field: &Val, elems: &Vec>, - ) -> Result, Box> { + ) -> Result, Box> { let idx = match field { &Val::Int(i) => i as usize, &Val::Str(ref s) => s.parse::()?, diff --git a/src/convert/traits.rs b/src/convert/traits.rs index 733a1f7..caef4fb 100644 --- a/src/convert/traits.rs +++ b/src/convert/traits.rs @@ -20,7 +20,7 @@ use std::result; use crate::build::Val; -pub type Result = result::Result<(), Box>; +pub type Result = result::Result<(), Box>; /// The trait that Converters from Val to different output formats for the /// final conversion stage of the ucg compiler. diff --git a/src/convert/xml.rs b/src/convert/xml.rs index bcc4bc7..4619747 100644 --- a/src/convert/xml.rs +++ b/src/convert/xml.rs @@ -31,7 +31,7 @@ use xml::EmitterConfig; pub struct XmlConverter {} impl XmlConverter { - fn get_str_val(v: &Val) -> std::result::Result<&str, Box> { + fn get_str_val(v: &Val) -> std::result::Result<&str, Box> { if let Val::Str(ref s) = v { Ok(s) } else { @@ -45,7 +45,7 @@ impl XmlConverter { fn get_tuple_val( v: &Val, - ) -> std::result::Result<&Vec<(PositionedItem, Rc)>, Box> { + ) -> std::result::Result<&Vec<(PositionedItem, Rc)>, Box> { if let Val::Tuple(ref fs) = v { Ok(fs) } else { @@ -57,7 +57,7 @@ impl XmlConverter { } } - fn get_list_val(v: &Val) -> std::result::Result<&Vec>, Box> { + fn get_list_val(v: &Val) -> std::result::Result<&Vec>, Box> { if let Val::List(ref fs) = v { Ok(fs) } else { diff --git a/src/format.rs b/src/format.rs index d64905c..1d4110b 100644 --- a/src/format.rs +++ b/src/format.rs @@ -38,7 +38,7 @@ impl + Clone> Formatter { /// /// If the formatter has the wrong number of arguments for the number of replacements /// it will return an error. Otherwise it will return the formatted string. - pub fn render(&self, pos: &Position) -> Result> { + pub fn render(&self, pos: &Position) -> Result> { let mut buf = String::new(); let mut should_escape = false; let mut count = 0; diff --git a/src/main.rs b/src/main.rs index 241a066..5d3f327 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,7 +80,7 @@ fn build_file<'a>( strict: bool, import_paths: &'a Vec, cache: Rc>, -) -> Result, Box> { +) -> Result, Box> { let mut file_path_buf = PathBuf::from(file); if file_path_buf.is_relative() { file_path_buf = std::env::current_dir().unwrap().join(file_path_buf); @@ -164,7 +164,7 @@ fn visit_ucg_files( import_paths: &Vec, cache: Rc>, registry: &ConverterRegistry, -) -> Result> { +) -> Result> { let our_path = String::from(path.to_string_lossy()); let mut result = true; let mut summary = String::new();