improve error handling

This commit is contained in:
2026-04-26 22:29:07 -04:00
parent 2fe8f1a287
commit 9c7562064c
2 changed files with 20 additions and 8 deletions
+19 -7
View File
@@ -8,6 +8,12 @@ const FILENAME_WIP : &str = "state.wip";
struct LetterCounts([u32; 26]); // tuple struct LetterCounts([u32; 26]); // tuple
enum DeserializeError {
MissingLetter,
MissingCount(std::num::ParseIntError),
UnexpectedChar,
}
impl LetterCounts { impl LetterCounts {
fn add(&mut self, i : usize) { fn add(&mut self, i : usize) {
self.0[i] += 1; self.0[i] += 1;
@@ -32,17 +38,17 @@ impl LetterCounts {
} }
// todo: use serde? // todo: use serde?
fn deserialize(&mut self, s: &str) -> Result<(), Vec<String>> { fn deserialize(&mut self, s: &str) -> Result<(), Vec<DeserializeError>> {
println!("deserializing..."); println!("deserializing...");
let mut error_strings = Vec::new(); let mut error_strings : Vec<DeserializeError> = Vec::new();
// todo: don't just assume no duplicate (and disagreeing) lines // todo: don't just assume no duplicate (and disagreeing) lines
for line in s.lines() { for line in s.lines() {
if let Some((letter, val)) = line.split_once(' ') { if let Some((letter, val)) = line.split_once(' ') {
let c = match letter.chars().next() { let c = match letter.chars().next() {
Some(c) => c, Some(c) => c,
None => { None => {
error_strings.push("ERROR: invalid line format, missing character.".to_string()); error_strings.push(DeserializeError::MissingLetter);
continue; continue;
} }
}; };
@@ -51,14 +57,13 @@ impl LetterCounts {
let count = match val.parse::<u32>() { let count = match val.parse::<u32>() {
Ok(i) => i, Ok(i) => i,
Err(e) => { Err(e) => {
// todo: don't store strings, unnecessary allocation error_strings.push(DeserializeError::MissingCount(e));
error_strings.push(format!("ERROR: invalid line format, missing count. ({})", e));
continue; continue;
} }
}; };
self.0[index] = count; self.0[index] = count;
} else { } else {
error_strings.push("ERROR: encountered non-lowercase alphabetic char".to_string()); error_strings.push(DeserializeError::UnexpectedChar);
continue; // we just drop the char/line continue; // we just drop the char/line
} }
} }
@@ -82,7 +87,14 @@ fn main() {
// deserialize file contents into data structure // deserialize file contents into data structure
if let Ok(contents) = fs::read_to_string(FILENAME) { if let Ok(contents) = fs::read_to_string(FILENAME) {
if let Err(v) = data.deserialize(&contents) { if let Err(v) = data.deserialize(&contents) {
v.iter().for_each(|s| eprintln!("{}",s)); v.iter().for_each(|e| {
let e_string = match e {
DeserializeError::MissingLetter => "ERROR: invalid line format, missing character.".to_string(),
DeserializeError::UnexpectedChar => "ERROR: unexpected character".to_string(),
DeserializeError::MissingCount(e) => format!("Error: missing count {}",e),
};
eprintln!("{}", e_string)
});
} }
} else { } else {
eprintln!("ERROR: failed to read file"); eprintln!("ERROR: failed to read file");
+1 -1
View File
@@ -5,7 +5,7 @@ d 1
e 2 e 2
f 3 f 3
g 3 g 3
h 3 h 4
i 1 i 1
j 0 j 0
k 1 k 1