summaryrefslogtreecommitdiff
path: root/src/exercise.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-03-31 18:25:54 +0200
committermo8it <mo8it@proton.me>2024-03-31 18:25:54 +0200
commitc1de4d46aad38d315e061b7262f773f48c6aab63 (patch)
treef4dd2c27e9dca918d0ae24391f4f079fda715b30 /src/exercise.rs
parent82b563f1654860ba3590d91ec3c0f321e3130ae2 (diff)
Some improvements to error handling
Diffstat (limited to 'src/exercise.rs')
-rw-r--r--src/exercise.rs23
1 files changed, 9 insertions, 14 deletions
diff --git a/src/exercise.rs b/src/exercise.rs
index 83d444f..48aaedd 100644
--- a/src/exercise.rs
+++ b/src/exercise.rs
@@ -114,14 +114,9 @@ impl Exercise {
}
}
- pub fn state(&self) -> State {
- let source_file = File::open(&self.path).unwrap_or_else(|e| {
- println!(
- "Failed to open the exercise file {}: {e}",
- self.path.display(),
- );
- exit(1);
- });
+ pub fn state(&self) -> Result<State> {
+ let source_file = File::open(&self.path)
+ .with_context(|| format!("Failed to open the exercise file {}", self.path.display()))?;
let mut source_reader = BufReader::new(source_file);
// Read the next line into `buf` without the newline at the end.
@@ -152,7 +147,7 @@ impl Exercise {
// Reached the end of the file and didn't find the comment.
if n == 0 {
- return State::Done;
+ return Ok(State::Done);
}
if contains_not_done_comment(&line) {
@@ -198,7 +193,7 @@ impl Exercise {
});
}
- return State::Pending(context);
+ return Ok(State::Pending(context));
}
current_line_number += 1;
@@ -218,8 +213,8 @@ impl Exercise {
// without actually having solved anything.
// The only other way to truly check this would to compile and run
// the exercise; which would be both costly and counterintuitive
- pub fn looks_done(&self) -> bool {
- self.state() == State::Done
+ pub fn looks_done(&self) -> Result<bool> {
+ self.state().map(|state| state == State::Done)
}
}
@@ -271,7 +266,7 @@ mod test {
},
];
- assert_eq!(state, State::Pending(expected));
+ assert_eq!(state.unwrap(), State::Pending(expected));
}
#[test]
@@ -283,7 +278,7 @@ mod test {
hint: String::new(),
};
- assert_eq!(exercise.state(), State::Done);
+ assert_eq!(exercise.state().unwrap(), State::Done);
}
#[test]