diff options
Diffstat (limited to 'src/ram.rs')
-rw-r--r-- | src/ram.rs | 32 |
1 files changed, 22 insertions, 10 deletions
@@ -38,16 +38,22 @@ struct HexLine { } impl HexLine { - fn from(s: &str) -> Result<Self, i32> { + fn from(s: &str) -> Result<Self, usize> { // 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::<u16>().unwrap(); // TODO: handle unwrap - let bytecount = (&s[1..3]).parse::<usize>().unwrap(); // TODO: handle unwrap + let offset = match (&s[3..7]).parse::<u16>() { + Ok(value) => value, + Err(_) => return Err(3), + }; + let bytecount = match(&s[1..3]).parse::<usize>() { + 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::<u8>().unwrap()); // TODO handle unwrap + data.push(match(&s[counter..counter+2]).parse::<u8>() { + 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<Self, String> { 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 }) } } |