diff --git a/src/main.rs b/src/main.rs index 9d7b7d6..ea5ae19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![allow(clippy::from_str_radix_10)] +use std::cmp::Ordering; use std::collections::HashMap; use std::fs::File; use std::io::Write; @@ -54,7 +56,7 @@ fn extract_operand(statement: Pair) -> &str { } fn parse_constant(text: &str) -> Result { - if text.starts_with("\"") { + if text.starts_with('\"') { let c = text.chars().nth(1).unwrap(); Ok(c as u32) } else if text.starts_with("0x") { @@ -79,7 +81,11 @@ fn to_bf(rule: Rule, operand: &str, state: &mut State, out: &mut Builder) { } Rule::delvar => { let variable_name = operand; - if let None = state.variables.remove(&String::from(variable_name)) { + if state + .variables + .remove(&String::from(variable_name)) + .is_none() + { panic!("Variable '{}' did not exists", variable_name); } } @@ -96,10 +102,14 @@ fn to_bf(rule: Rule, operand: &str, state: &mut State, out: &mut Builder) { Rule::pointa => { let address = operand; let address_parsed = parse_constant(address).unwrap(); - if address_parsed < state.mem_pointer { - out.append("<".repeat((state.mem_pointer - address_parsed) as usize)) - } else if address_parsed > state.mem_pointer { - out.append(">".repeat((address_parsed - state.mem_pointer) as usize)) + match address_parsed.cmp(&state.mem_pointer) { + Ordering::Less => { + out.append("<".repeat((state.mem_pointer - address_parsed) as usize)) + } + Ordering::Greater => { + out.append(">".repeat((address_parsed - state.mem_pointer) as usize)) + } + Ordering::Equal => (), } state.mem_pointer = address_parsed; } @@ -234,7 +244,7 @@ fn main() -> Result<(), Box> { let bf = builder.string().unwrap(); let mut out = File::create(args.output_file)?; - out.write(bf.as_bytes())?; + out.write_all(bf.as_bytes())?; out.sync_all()?; Ok(())