Created
September 3, 2022 16:08
-
-
Save jordanorelli/21369e29f2ccb226251d77ca27b131f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// errors6.rs | |
// 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. | |
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint. | |
use std::num::ParseIntError; | |
// This is a custom error type that we will be using in `parse_pos_nonzero()`. | |
#[derive(PartialEq, Debug)] | |
enum ParsePosNonzeroError { | |
Creation(CreationError), | |
ParseInt(ParseIntError) | |
} | |
impl ParsePosNonzeroError { | |
fn from_creation(err: CreationError) -> ParsePosNonzeroError { | |
ParsePosNonzeroError::Creation(err) | |
} | |
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError { | |
ParsePosNonzeroError::ParseInt(err) | |
} | |
} | |
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. | |
// we started with this: | |
// let x: i64 = s.parse().unwrap(); | |
// this is also a solution: | |
// let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?; | |
// this is one valid solution | |
let x = match s.parse::<i64>() { | |
Ok(n) => n, | |
Err(e) => return Err(ParsePosNonzeroError::from_parseint(e)), | |
}; | |
PositiveNonzeroInteger::new(x) | |
.map_err(ParsePosNonzeroError::from_creation) | |
} | |
// 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> { | |
match value { | |
x if x < 0 => Err(CreationError::Negative), | |
x if x == 0 => Err(CreationError::Zero), | |
x => Ok(PositiveNonzeroInteger(x as u64)) | |
} | |
} | |
} | |
#[cfg(test)] | |
mod test { | |
use super::*; | |
#[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(_)) | |
)); | |
} | |
#[test] | |
fn test_negative() { | |
assert_eq!( | |
parse_pos_nonzero("-555"), | |
Err(ParsePosNonzeroError::Creation(CreationError::Negative)) | |
); | |
} | |
#[test] | |
fn test_zero() { | |
assert_eq!( | |
parse_pos_nonzero("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())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment