clippy suggestions
This commit is contained in:
@@ -30,7 +30,7 @@ pub fn compile(program: String) -> Result<(), Box<dyn Error>> {
|
||||
.first_element_child()
|
||||
.ok_or(InvalidProgram)?
|
||||
.children()
|
||||
.find(|node| util::tag_name(&node) == "main")
|
||||
.find(|node| util::tag_name(node) == "main")
|
||||
.ok_or(MissingChild("program", "main"))?;
|
||||
let main_ast = Instruction::from_children(main)?;
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ impl Instruction {
|
||||
Ok(match util::tag_name(&node).as_str() {
|
||||
"value" => Instruction::Value(
|
||||
node.attribute("variable")
|
||||
.and_then(|a| Some(String::from(a)))
|
||||
.map(String::from)
|
||||
.ok_or(MissingAttribute("value", "variable"))?,
|
||||
),
|
||||
"assign" => Instruction::Assign(
|
||||
|
||||
@@ -234,7 +234,7 @@ impl RunUtil for Instruction {
|
||||
pub trait Run {
|
||||
fn run(&self, ctx: &mut Context, globals: &Context) -> Result<Option<Value>, Box<dyn Error>>;
|
||||
fn run_all(
|
||||
ins: &Vec<Instruction>,
|
||||
ins: &[Instruction],
|
||||
ctx: &mut Context,
|
||||
globals: &Context,
|
||||
) -> Result<Option<Vec<Value>>, Box<dyn Error>>;
|
||||
@@ -242,7 +242,7 @@ pub trait Run {
|
||||
|
||||
impl Run for Instruction {
|
||||
fn run_all(
|
||||
ins: &Vec<Instruction>,
|
||||
ins: &[Instruction],
|
||||
ctx: &mut Context,
|
||||
globals: &Context,
|
||||
) -> Result<Option<Vec<Value>>, Box<dyn Error>> {
|
||||
@@ -250,11 +250,11 @@ impl Run for Instruction {
|
||||
}
|
||||
|
||||
fn run(&self, ctx: &mut Context, globals: &Context) -> Result<Option<Value>, Box<dyn Error>> {
|
||||
Ok(if let None = ctx.value(&String::from("__return")) {
|
||||
Ok(if ctx.value(&String::from("__return")).is_none() {
|
||||
match self {
|
||||
Instruction::Value(key) => {
|
||||
Some(match ctx.value(key).ok_or(UnknownVariable(key.clone()))? {
|
||||
Value::Array(vecrc) => Value::Array(Rc::clone(&vecrc)),
|
||||
Value::Array(vecrc) => Value::Array(Rc::clone(vecrc)),
|
||||
val => val.clone(),
|
||||
})
|
||||
}
|
||||
@@ -388,7 +388,7 @@ impl Run for Instruction {
|
||||
let vals: Vec<Value> =
|
||||
Instruction::run_all(args, ctx, globals)?.ok_or(InvalidValue("call"))?;
|
||||
let fct_val = ctx
|
||||
.value(&fct_name)
|
||||
.value(fct_name)
|
||||
.ok_or(UnknownVariable(fct_name.clone()))?;
|
||||
if let Value::Function(f) = fct_val {
|
||||
let mut local = ctx.clone();
|
||||
@@ -511,7 +511,7 @@ pub fn run(program: String) -> Result<(), Box<dyn Error>> {
|
||||
.first_element_child()
|
||||
.ok_or(InvalidProgram)?
|
||||
.children()
|
||||
.find(|node| util::tag_name(&node) == "main")
|
||||
.find(|node| util::tag_name(node) == "main")
|
||||
.ok_or(MissingChild("program", "main"))?;
|
||||
let main_ast = Instruction::from_children(main)?;
|
||||
|
||||
@@ -519,7 +519,7 @@ pub fn run(program: String) -> Result<(), Box<dyn Error>> {
|
||||
.first_element_child()
|
||||
.ok_or(InvalidProgram)?
|
||||
.children()
|
||||
.filter(|node| node.tag_name().name() == String::from("function"));
|
||||
.filter(|node| node.tag_name().name() == "function");
|
||||
|
||||
for fun in functions {
|
||||
ctx.assign(
|
||||
|
||||
@@ -62,7 +62,7 @@ fn print_line(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||
}
|
||||
|
||||
fn input(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||
if vals.len() == 0 {
|
||||
if vals.is_empty() {
|
||||
let mut line = String::new();
|
||||
stdin().read_line(&mut line)?;
|
||||
line.pop();
|
||||
@@ -181,7 +181,7 @@ fn array_length(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||
fn to_ascii(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||
if vals.len() == 1 {
|
||||
if let Value::Integer(i) = &vals[0] {
|
||||
if &0 <= i && i <= &255 {
|
||||
if (0..=255).contains(i) {
|
||||
Ok(Some(Value::String(String::from_utf8(vec![*i as u8])?)))
|
||||
} else {
|
||||
Err(InvalidArgument("to-ascii", "integer").into())
|
||||
@@ -211,12 +211,9 @@ fn from_ascii(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||
}
|
||||
|
||||
fn get_args(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||
if vals.len() == 0 {
|
||||
if vals.is_empty() {
|
||||
Ok(Some(Value::Array(Rc::new(RefCell::new(
|
||||
std::env::args()
|
||||
.skip(1)
|
||||
.map(|arg| Value::String(arg))
|
||||
.collect(),
|
||||
std::env::args().skip(1).map(Value::String).collect(),
|
||||
)))))
|
||||
} else {
|
||||
Err(BadArgumentCount("get-args", vals.len(), 0).into())
|
||||
@@ -233,7 +230,7 @@ fn write_file(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
|
||||
.append(Value::to_bool(&vals[2]))
|
||||
.open(path)
|
||||
{
|
||||
if let Ok(_) = write!(file, "{}", contents) {
|
||||
if write!(file, "{}", contents).is_ok() {
|
||||
Ok(None)
|
||||
} else {
|
||||
Err(InaccessibleFile(path.clone()).into())
|
||||
|
||||
@@ -27,7 +27,7 @@ impl Function {
|
||||
}
|
||||
self.args
|
||||
.iter()
|
||||
.zip(args.into_iter())
|
||||
.zip(args)
|
||||
.for_each(|(p, a)| ctx.assign(p.clone(), a));
|
||||
for i in self.ins.iter() {
|
||||
i.run(ctx, globals)?;
|
||||
@@ -37,20 +37,22 @@ impl Function {
|
||||
|
||||
pub fn from(fun: &Node<'_, '_>) -> Result<Function, Box<dyn Error>> {
|
||||
Ok(Function {
|
||||
args: util::find_node(&fun, "arguments")
|
||||
args: util::find_node(fun, "arguments")
|
||||
.ok_or(MissingChild("call", "arguments"))?
|
||||
.children()
|
||||
.filter(Node::is_element)
|
||||
.map(|n| n.attribute("name").and_then(|s| Some(String::from(s))))
|
||||
.map(|n| n.attribute("name").map(String::from))
|
||||
.collect::<Option<Vec<String>>>()
|
||||
.ok_or(Unnamed("argument"))?,
|
||||
ins: Instruction::from_children(
|
||||
util::find_node(&fun, "body").ok_or(MissingChild("call", "body"))?,
|
||||
util::find_node(fun, "body").ok_or(MissingChild("call", "body"))?,
|
||||
)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type StdFn = fn(Vec<Value>) -> Result<Option<Value>, Box<dyn Error>>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Value {
|
||||
Integer(i64),
|
||||
@@ -58,7 +60,7 @@ pub enum Value {
|
||||
String(String),
|
||||
Array(Rc<RefCell<Vec<Value>>>),
|
||||
Function(Function),
|
||||
StdFunction(fn(Vec<Value>) -> Result<Option<Value>, Box<dyn Error>>),
|
||||
StdFunction(StdFn),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
@@ -66,7 +68,7 @@ impl Value {
|
||||
match self {
|
||||
Value::Integer(i) => *i != 0,
|
||||
Value::Real(f) => *f != 0.0,
|
||||
Value::String(s) => s.len() != 0,
|
||||
Value::String(s) => !s.is_empty(),
|
||||
Value::Array(v) => v.borrow().len() != 0,
|
||||
_ => true,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user