From 882d535ba8628d5e0b37e8664b3e2f26260b2671 Mon Sep 17 00:00:00 2001 From: Taylor Yu Date: Wed, 9 Jun 2021 23:26:10 -0500 Subject: [PATCH] feat: add advanced_errs1 New section and exercise to demonstrate the `From` trait for errors and its usefulness with the `?` operator. --- exercises/advanced_errors/advanced_errs1.rs | 98 +++++++++++++++++++++ info.toml | 20 +++++ 2 files changed, 118 insertions(+) create mode 100644 exercises/advanced_errors/advanced_errs1.rs diff --git a/exercises/advanced_errors/advanced_errs1.rs b/exercises/advanced_errors/advanced_errs1.rs new file mode 100644 index 0000000000..4bc7b635d6 --- /dev/null +++ b/exercises/advanced_errors/advanced_errs1.rs @@ -0,0 +1,98 @@ +// advanced_errs1.rs + +// Remember back in errors6, we had multiple mapping functions so that we +// could translate lower-level errors into our custom error type using +// `map_err()`? What if we could use the `?` operator directly instead? + +// Make this code compile! Execute `rustlings hint advanced_errs1` for +// hints :) + +// I AM NOT DONE + +use std::num::ParseIntError; +use std::str::FromStr; + +// This is a custom error type that we will be using in the `FromStr` +// implementation. +#[derive(PartialEq, Debug)] +enum ParsePosNonzeroError { + Creation(CreationError), + ParseInt(ParseIntError), +} + +impl From for ParsePosNonzeroError { + fn from(e: CreationError) -> Self { + // TODO: complete this implementation so that the `?` operator will + // work for `CreationError` + } +} + +// TODO: implement another instance of the `From` trait here so that the +// `?` operator will work in the other place in the `FromStr` +// implementation below. + +// Don't change anything below this line. + +impl FromStr for PositiveNonzeroInteger { + type Err = ParsePosNonzeroError; + fn from_str(s: &str) -> Result { + let x: i64 = s.parse()?; + Ok(PositiveNonzeroInteger::new(x)?) + } +} + +#[derive(PartialEq, Debug)] +struct PositiveNonzeroInteger(u64); + +#[derive(PartialEq, Debug)] +enum CreationError { + Negative, + Zero, +} + +impl PositiveNonzeroInteger { + fn new(value: i64) -> Result { + 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!( + PositiveNonzeroInteger::from_str("not a number"), + Err(ParsePosNonzeroError::ParseInt(_)) + )); + } + + #[test] + fn test_negative() { + assert_eq!( + PositiveNonzeroInteger::from_str("-555"), + Err(ParsePosNonzeroError::Creation(CreationError::Negative)) + ); + } + + #[test] + fn test_zero() { + assert_eq!( + PositiveNonzeroInteger::from_str("0"), + Err(ParsePosNonzeroError::Creation(CreationError::Zero)) + ); + } + + #[test] + fn test_positive() { + let x = PositiveNonzeroInteger::new(42); + assert!(x.is_ok()); + assert_eq!(PositiveNonzeroInteger::from_str("42"), Ok(x.unwrap())); + } +} diff --git a/info.toml b/info.toml index f5af88402d..8e24b7db76 100644 --- a/info.toml +++ b/info.toml @@ -974,3 +974,23 @@ path = "exercises/conversions/as_ref_mut.rs" mode = "test" hint = """ Add AsRef as a trait bound to the functions.""" + +# ADVANCED ERRORS + +[[exercises]] +name = "advanced_errs1" +path = "exercises/advanced_errors/advanced_errs1.rs" +mode = "test" +hint = """ +This exercise uses an updated version of the code in errors6. The parsing +code is now in an implementation of the `FromStr` trait. Note that the +parsing code uses `?` directly, without any calls to `map_err()`. There is +one partial implementation of the `From` trait example that you should +complete. + +Details: The `?` operator calls `From::from()` on the error type to convert +it to the error type of the return type of the surrounding function. + +Hint: You will need to write another implementation of `From` that has a +different input type. +"""