summaryrefslogtreecommitdiff
path: root/exercises/13_error_handling/errors6.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-06-27 01:12:50 +0200
committermo8it <mo8it@proton.me>2024-06-27 01:12:50 +0200
commitb1daea1fe8536d7b7b4463cb8fc36d69848ef77a (patch)
tree45cd0559e57354a06ad76cef54024faa7a735d25 /exercises/13_error_handling/errors6.rs
parent129884aff74964d13aba8309014554b5625d6e5b (diff)
errors6 solution
Diffstat (limited to 'exercises/13_error_handling/errors6.rs')
-rw-r--r--exercises/13_error_handling/errors6.rs71
1 files changed, 35 insertions, 36 deletions
diff --git a/exercises/13_error_handling/errors6.rs b/exercises/13_error_handling/errors6.rs
index 8b08e08..0652abd 100644
--- a/exercises/13_error_handling/errors6.rs
+++ b/exercises/13_error_handling/errors6.rs
@@ -1,12 +1,18 @@
-// Using catch-all error types like `Box<dyn error::Error>` isn't recommended
-// for library code, where callers might want to make decisions based on the
-// error content, instead of printing it out or propagating it further. Here, we
-// define a custom error type to make it possible for callers to decide what to
-// do next when our function returns an error.
+// Using catch-all error types like `Box<dyn Error>` isn't recommended for
+// library code where callers might want to make decisions based on the error
+// content instead of printing it out or propagating it further. Here, we define
+// a custom error type to make it possible for callers to decide what to do next
+// when our function returns an error.
use std::num::ParseIntError;
-// This is a custom error type that we will be using in `parse_pos_nonzero()`.
+#[derive(PartialEq, Debug)]
+enum CreationError {
+ Negative,
+ Zero,
+}
+
+// A custom error type that we will be using in `PositiveNonzeroInteger::parse`.
#[derive(PartialEq, Debug)]
enum ParsePosNonzeroError {
Creation(CreationError),
@@ -14,39 +20,32 @@ enum ParsePosNonzeroError {
}
impl ParsePosNonzeroError {
- fn from_creation(err: CreationError) -> ParsePosNonzeroError {
- ParsePosNonzeroError::Creation(err)
+ fn from_creation(err: CreationError) -> Self {
+ Self::Creation(err)
}
- // TODO: add another error conversion function here.
- // fn from_parseint...
-}
-fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
- // TODO: change this to return an appropriate error instead of panicking
- // when `parse()` returns an error.
- let x: i64 = s.parse().unwrap();
- PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
+ // TODO: Add another error conversion function here.
+ // fn from_parseint(???) -> Self { ??? }
}
-// Don't change anything below this line.
-
#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);
-#[derive(PartialEq, Debug)]
-enum CreationError {
- Negative,
- Zero,
-}
-
impl PositiveNonzeroInteger {
- fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
+ fn new(value: i64) -> Result<Self, CreationError> {
match value {
x if x < 0 => Err(CreationError::Negative),
x if x == 0 => Err(CreationError::Zero),
- x => Ok(PositiveNonzeroInteger(x as u64)),
+ x => Ok(Self(x as u64)),
}
}
+
+ fn parse(s: &str) -> Result<Self, ParsePosNonzeroError> {
+ // TODO: change this to return an appropriate error instead of panicking
+ // when `parse()` returns an error.
+ let x: i64 = s.parse().unwrap();
+ Self::new(x).map_err(ParsePosNonzeroError::from_creation)
+ }
}
fn main() {
@@ -56,36 +55,36 @@ fn main() {
#[cfg(test)]
mod test {
use super::*;
+ use std::num::IntErrorKind;
#[test]
fn test_parse_error() {
- // We can't construct a ParseIntError, so we have to pattern match.
assert!(matches!(
- parse_pos_nonzero("not a number"),
- Err(ParsePosNonzeroError::ParseInt(_))
+ PositiveNonzeroInteger::parse("not a number"),
+ Err(ParsePosNonzeroError::ParseInt(_)),
));
}
#[test]
fn test_negative() {
assert_eq!(
- parse_pos_nonzero("-555"),
- Err(ParsePosNonzeroError::Creation(CreationError::Negative))
+ PositiveNonzeroInteger::parse("-555"),
+ Err(ParsePosNonzeroError::Creation(CreationError::Negative)),
);
}
#[test]
fn test_zero() {
assert_eq!(
- parse_pos_nonzero("0"),
- Err(ParsePosNonzeroError::Creation(CreationError::Zero))
+ PositiveNonzeroInteger::parse("0"),
+ Err(ParsePosNonzeroError::Creation(CreationError::Zero)),
);
}
#[test]
fn test_positive() {
- let x = PositiveNonzeroInteger::new(42);
- assert!(x.is_ok());
- assert_eq!(parse_pos_nonzero("42"), Ok(x.unwrap()));
+ let x = PositiveNonzeroInteger::new(42).unwrap();
+ assert_eq!(x.0, 42);
+ assert_eq!(PositiveNonzeroInteger::parse("42"), Ok(x));
}
}