diff --git a/exercises/advanced_errors/advanced_errs1.rs b/exercises/advanced_errors/advanced_errs1.rs index 4bc7b635..99508434 100644 --- a/exercises/advanced_errors/advanced_errs1.rs +++ b/exercises/advanced_errors/advanced_errs1.rs @@ -7,8 +7,6 @@ // Make this code compile! Execute `rustlings hint advanced_errs1` for // hints :) -// I AM NOT DONE - use std::num::ParseIntError; use std::str::FromStr; @@ -24,6 +22,13 @@ impl From for ParsePosNonzeroError { fn from(e: CreationError) -> Self { // TODO: complete this implementation so that the `?` operator will // work for `CreationError` + ParsePosNonzeroError::Creation(e) + } +} + +impl From for ParsePosNonzeroError { + fn from(e: ParseIntError) -> Self { + ParsePosNonzeroError::ParseInt(e) } } diff --git a/exercises/advanced_errors/advanced_errs2.rs b/exercises/advanced_errors/advanced_errs2.rs index 54e669fd..6dcf6f2a 100644 --- a/exercises/advanced_errors/advanced_errs2.rs +++ b/exercises/advanced_errors/advanced_errs2.rs @@ -16,8 +16,6 @@ // 4. Complete the partial implementation of `Display` for // `ParseClimateError`. -// I AM NOT DONE - use std::error::Error; use std::fmt::{self, Display, Formatter}; use std::num::{ParseFloatError, ParseIntError}; @@ -47,9 +45,12 @@ impl From for ParseClimateError { impl From for ParseClimateError { fn from(e: ParseFloatError) -> Self { // TODO: Complete this function + ParseClimateError::ParseFloat(e) } } +impl Error for ParseClimateError {} + // TODO: Implement a missing trait so that `main()` below will compile. It // is not necessary to implement any methods inside the missing trait. @@ -62,7 +63,10 @@ impl Display for ParseClimateError { // Imports the variants to make the following code more compact. use ParseClimateError::*; match self { + Empty => write!(f, "empty input"), + BadLen => write!(f, "incorrect number of fields"), NoCity => write!(f, "no city name"), + ParseInt(e) => write!(f, "error parsing year: {}", e), ParseFloat(e) => write!(f, "error parsing temperature: {}", e), } } @@ -90,6 +94,8 @@ impl FromStr for Climate { fn from_str(s: &str) -> Result { let v: Vec<_> = s.split(',').collect(); let (city, year, temp) = match &v[..] { + [""] => return Err(ParseClimateError::Empty), + ["", _, _] => return Err(ParseClimateError::NoCity), [city, year, temp] => (city.to_string(), year, temp), _ => return Err(ParseClimateError::BadLen), };