2021-06-06 23:05:01 -05:00
|
|
|
// 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.
|
|
|
|
|
2022-07-14 11:02:33 -05:00
|
|
|
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint.
|
2021-06-06 23:05:01 -05:00
|
|
|
|
2021-06-09 18:13:57 -05:00
|
|
|
use std::num::ParseIntError;
|
|
|
|
|
2021-06-06 23:05:01 -05:00
|
|
|
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
enum ParsePosNonzeroError {
|
2021-06-09 18:13:57 -05:00
|
|
|
Creation(CreationError),
|
2022-11-06 13:42:17 -06:00
|
|
|
ParseInt(ParseIntError),
|
2021-06-09 18:13:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParsePosNonzeroError {
|
2022-07-14 11:02:33 -05:00
|
|
|
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
|
|
|
|
ParsePosNonzeroError::Creation(err)
|
|
|
|
}
|
2021-06-09 18:13:57 -05:00
|
|
|
// TODO: add another error conversion function here.
|
2022-07-14 11:02:33 -05:00
|
|
|
// fn from_parseint...
|
2023-03-24 16:18:51 -05:00
|
|
|
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
|
|
|
|
ParsePosNonzeroError::ParseInt(err)
|
|
|
|
}
|
2021-06-06 23:05:01 -05:00
|
|
|
}
|
|
|
|
|
2022-11-06 13:42:17 -06:00
|
|
|
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
|
2021-06-06 23:05:01 -05:00
|
|
|
// TODO: change this to return an appropriate error instead of panicking
|
|
|
|
// when `parse()` returns an error.
|
2023-03-24 16:18:51 -05:00
|
|
|
match s.parse() {
|
|
|
|
Ok(x) => match PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation) {
|
|
|
|
Ok(y) => Ok(y),
|
|
|
|
Err(y) => Err(y),
|
|
|
|
}
|
|
|
|
Err(x) => Err(ParsePosNonzeroError::from_parseint(x)),
|
|
|
|
}
|
2021-06-06 23:05:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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),
|
2022-11-06 13:42:17 -06:00
|
|
|
x => Ok(PositiveNonzeroInteger(x as u64)),
|
2021-06-06 23:05:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_error() {
|
2021-06-09 18:13:57 -05:00
|
|
|
// We can't construct a ParseIntError, so we have to pattern match.
|
|
|
|
assert!(matches!(
|
2021-06-06 23:05:01 -05:00
|
|
|
parse_pos_nonzero("not a number"),
|
2021-06-09 18:13:57 -05:00
|
|
|
Err(ParsePosNonzeroError::ParseInt(_))
|
|
|
|
));
|
2021-06-06 23:05:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_negative() {
|
|
|
|
assert_eq!(
|
|
|
|
parse_pos_nonzero("-555"),
|
2021-06-09 18:13:57 -05:00
|
|
|
Err(ParsePosNonzeroError::Creation(CreationError::Negative))
|
2021-06-06 23:05:01 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_zero() {
|
|
|
|
assert_eq!(
|
|
|
|
parse_pos_nonzero("0"),
|
2021-06-09 18:13:57 -05:00
|
|
|
Err(ParsePosNonzeroError::Creation(CreationError::Zero))
|
2021-06-06 23:05:01 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_positive() {
|
2021-06-09 18:13:57 -05:00
|
|
|
let x = PositiveNonzeroInteger::new(42);
|
|
|
|
assert!(x.is_ok());
|
|
|
|
assert_eq!(parse_pos_nonzero("42"), Ok(x.unwrap()));
|
2021-06-06 23:05:01 -05:00
|
|
|
}
|
|
|
|
}
|