2024-06-26 19:12:50 -04:00
|
|
|
// 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.
|
2021-06-07 00:05:01 -04:00
|
|
|
|
2021-06-09 19:13:57 -04:00
|
|
|
use std::num::ParseIntError;
|
|
|
|
|
2024-06-26 19:12:50 -04:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
enum CreationError {
|
|
|
|
Negative,
|
|
|
|
Zero,
|
|
|
|
}
|
|
|
|
|
|
|
|
// A custom error type that we will be using in `PositiveNonzeroInteger::parse`.
|
2021-06-07 00:05:01 -04:00
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
enum ParsePosNonzeroError {
|
2021-06-09 19:13:57 -04:00
|
|
|
Creation(CreationError),
|
2022-11-06 14:42:17 -05:00
|
|
|
ParseInt(ParseIntError),
|
2021-06-09 19:13:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParsePosNonzeroError {
|
2024-06-26 19:12:50 -04:00
|
|
|
fn from_creation(err: CreationError) -> Self {
|
|
|
|
Self::Creation(err)
|
2022-07-14 12:02:33 -04:00
|
|
|
}
|
2021-06-07 00:05:01 -04:00
|
|
|
|
2024-06-26 19:12:50 -04:00
|
|
|
// TODO: Add another error conversion function here.
|
|
|
|
// fn from_parseint(???) -> Self { ??? }
|
2021-06-07 00:05:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
struct PositiveNonzeroInteger(u64);
|
|
|
|
|
|
|
|
impl PositiveNonzeroInteger {
|
2024-06-26 19:12:50 -04:00
|
|
|
fn new(value: i64) -> Result<Self, CreationError> {
|
2021-06-07 00:05:01 -04:00
|
|
|
match value {
|
|
|
|
x if x < 0 => Err(CreationError::Negative),
|
2024-07-04 07:38:35 -04:00
|
|
|
0 => Err(CreationError::Zero),
|
2024-06-26 19:12:50 -04:00
|
|
|
x => Ok(Self(x as u64)),
|
2021-06-07 00:05:01 -04:00
|
|
|
}
|
|
|
|
}
|
2024-06-26 19:12:50 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-06-07 00:05:01 -04:00
|
|
|
}
|
|
|
|
|
2024-04-17 16:46:21 -04:00
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
2021-06-07 00:05:01 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_error() {
|
2021-06-09 19:13:57 -04:00
|
|
|
assert!(matches!(
|
2024-06-26 19:12:50 -04:00
|
|
|
PositiveNonzeroInteger::parse("not a number"),
|
|
|
|
Err(ParsePosNonzeroError::ParseInt(_)),
|
2021-06-09 19:13:57 -04:00
|
|
|
));
|
2021-06-07 00:05:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_negative() {
|
|
|
|
assert_eq!(
|
2024-06-26 19:12:50 -04:00
|
|
|
PositiveNonzeroInteger::parse("-555"),
|
|
|
|
Err(ParsePosNonzeroError::Creation(CreationError::Negative)),
|
2021-06-07 00:05:01 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_zero() {
|
|
|
|
assert_eq!(
|
2024-06-26 19:12:50 -04:00
|
|
|
PositiveNonzeroInteger::parse("0"),
|
|
|
|
Err(ParsePosNonzeroError::Creation(CreationError::Zero)),
|
2021-06-07 00:05:01 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_positive() {
|
2024-06-26 19:12:50 -04:00
|
|
|
let x = PositiveNonzeroInteger::new(42).unwrap();
|
|
|
|
assert_eq!(x.0, 42);
|
|
|
|
assert_eq!(PositiveNonzeroInteger::parse("42"), Ok(x));
|
2021-06-07 00:05:01 -04:00
|
|
|
}
|
|
|
|
}
|