error handling
This commit is contained in:
25
src/lib.rs
25
src/lib.rs
@@ -1,15 +1,17 @@
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
|
||||
use roxmltree::{Document, Node};
|
||||
use roxmltree::Document;
|
||||
|
||||
mod context;
|
||||
mod error;
|
||||
mod instruction;
|
||||
mod stl;
|
||||
mod util;
|
||||
mod value;
|
||||
|
||||
use context::Context;
|
||||
use error::{InvalidProgram, MissingChild, Unnamed};
|
||||
use instruction::Instruction;
|
||||
use value::{Function, Value};
|
||||
|
||||
@@ -23,33 +25,22 @@ pub fn run(filename: &str) -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let main = root
|
||||
.first_element_child()
|
||||
.ok_or("invalid program structure")?
|
||||
.ok_or(InvalidProgram)?
|
||||
.children()
|
||||
.find(|node| util::tag_name(&node) == "main")
|
||||
.ok_or("No 'main' block")?;
|
||||
.ok_or(MissingChild("program", "main"))?;
|
||||
let main_ast = Instruction::from_children(main)?;
|
||||
|
||||
let functions = root
|
||||
.first_element_child()
|
||||
.ok_or("invalid program structure")?
|
||||
.ok_or(InvalidProgram)?
|
||||
.children()
|
||||
.filter(|node| node.tag_name().name() == String::from("function"));
|
||||
|
||||
for fun in functions {
|
||||
ctx.assign(
|
||||
String::from(fun.attribute("name").ok_or("unnamed function")?),
|
||||
Value::Function(Function {
|
||||
args: util::find_node(&fun, "arguments")
|
||||
.ok_or("missing 'arguments' block in 'call' tag")?
|
||||
.children()
|
||||
.filter(Node::is_element)
|
||||
.map(|n| n.attribute("name").and_then(|s| Some(String::from(s))))
|
||||
.collect::<Option<Vec<String>>>()
|
||||
.ok_or("unnamed argument")?,
|
||||
ins: Instruction::from_children(
|
||||
util::find_node(&fun, "body").ok_or("missing 'body' block in 'call' tag")?,
|
||||
)?,
|
||||
}),
|
||||
String::from(fun.attribute("name").ok_or(Unnamed("function"))?),
|
||||
Value::Function(Function::from(&fun)?),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user