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