more updates

This commit is contained in:
Steven Johnson 2022-07-05 20:36:12 +12:00
parent ecd727a33d
commit ad4c5c286c
2 changed files with 15 additions and 4 deletions

View File

@ -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<CreationError> for ParsePosNonzeroError {
fn from(e: CreationError) -> Self {
// TODO: complete this implementation so that the `?` operator will
// work for `CreationError`
ParsePosNonzeroError::Creation(e)
}
}
impl From<ParseIntError> for ParsePosNonzeroError {
fn from(e: ParseIntError) -> Self {
ParsePosNonzeroError::ParseInt(e)
}
}

View File

@ -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<ParseIntError> for ParseClimateError {
impl From<ParseFloatError> 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<Self, Self::Err> {
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),
};