From 0832a0dddad7782b8ac7a06c76d3e58ab2a56864 Mon Sep 17 00:00:00 2001 From: Oxore Date: Mon, 30 Sep 2019 23:33:18 +0300 Subject: Remove most of unwraps, add Mov instruction --- src/ram.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'src/ram.rs') diff --git a/src/ram.rs b/src/ram.rs index c7c4d43..34723cb 100644 --- a/src/ram.rs +++ b/src/ram.rs @@ -38,16 +38,22 @@ struct HexLine { } impl HexLine { - fn from(s: &str) -> Result { + fn from(s: &str) -> Result { // The shortest possible sequence is EOF (:00000001FF) if s.len() < 11 { - return Err(1) + return Err(usize::max_value()) } if &s[0..1] != ":" { - return Err(2) + return Err(0) } - let offset = (&s[3..7]).parse::().unwrap(); // TODO: handle unwrap - let bytecount = (&s[1..3]).parse::().unwrap(); // TODO: handle unwrap + let offset = match (&s[3..7]).parse::() { + Ok(value) => value, + Err(_) => return Err(3), + }; + let bytecount = match(&s[1..3]).parse::() { + Ok(value) => value, + Err(_) => return Err(1), + }; // If EOF reached if &s[7..9] == "01" { @@ -56,7 +62,10 @@ impl HexLine { let mut counter = 9; let mut data = vec![]; while counter < s.len() - 2 && counter < (9 + bytecount * 2) { - data.push((&s[counter..counter+2]).parse::().unwrap()); // TODO handle unwrap + data.push(match(&s[counter..counter+2]).parse::() { + Ok(value) => value, + Err(_) => return Err(counter), + }); counter += 2; } // TODO: check checksum @@ -68,10 +77,13 @@ impl HexLine { } impl Ram { - pub fn from_hex(hex: String) -> Self { + pub fn from_hex(hex: String) -> Result { let mut array = [0; u16::max_value() as usize + 1]; - for line in hex.lines() { - let hex_line = HexLine::from(line).unwrap(); // TODO: handle unwrap + for (linenum, line) in hex.lines().enumerate() { + let hex_line = match HexLine::from(line) { + Ok(value) => value, + Err(charnum) => return Err(format!("ihex parsing error {}:{}", linenum, charnum)), + }; let offset = hex_line.offset; match hex_line.rectyp { Rectyp::Data => { @@ -82,6 +94,6 @@ impl Ram { Rectyp::EndOfFile => {} } } - Self { array } + Ok(Self { array }) } } -- cgit v1.2.3