Revert "finished"

This reverts commit 5f724516dab85efd1ad9f1b3f71da7c25f765710.
This commit is contained in:
Stanislav Pankrashin 2022-06-23 17:05:48 +12:00
parent 849a1ec933
commit 1811f7e72a

View File

@ -16,6 +16,8 @@
// 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};
@ -36,7 +38,7 @@ enum ParseClimateError {
// `ParseIntError` values.
impl From<ParseIntError> for ParseClimateError {
fn from(e: ParseIntError) -> Self {
ParseClimateError::ParseInt(e)
Self::ParseInt(e)
}
}
@ -44,15 +46,12 @@ impl From<ParseIntError> for ParseClimateError {
// `ParseFloatError` values.
impl From<ParseFloatError> for ParseClimateError {
fn from(e: ParseFloatError) -> Self {
ParseClimateError::ParseFloat(e)
// TODO: Complete this function
}
}
// TODO: Implement a missing trait so that `main()` below will compile. It
// is not necessary to implement any methods inside the missing trait.
impl Error for ParseClimateError {
}
// The `Display` trait allows for other code to obtain the error formatted
// as a user-visible string.
@ -65,9 +64,6 @@ impl Display for ParseClimateError {
match self {
NoCity => write!(f, "no city name"),
ParseFloat(e) => write!(f, "error parsing temperature: {}", e),
Empty => write!(f, "empty input"),
BadLen => write!(f, "incorrect number of fields"),
ParseInt(e) => write!(f, "error parsing year: {}", e),
}
}
}
@ -92,23 +88,13 @@ impl FromStr for Climate {
// TODO: Complete this function by making it handle the missing error
// cases.
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() == 0 {
return Err(ParseClimateError::Empty);
}
let v: Vec<_> = s.split(',').collect();
let (city, year, temp) = match &v[..] {
[city, _, _] if city.len() == 0 => return Err(ParseClimateError::NoCity),
[city, year, temp] => (city.to_string(), year, temp),
_ => return Err(ParseClimateError::BadLen),
};
let year: u32 = match year.parse() {
Ok(year) => year,
Err(err) => return Err(ParseClimateError::ParseInt(err)),
};
let temp: f32 = match temp.parse() {
Ok(temp) => temp,
Err(err) => return Err(ParseClimateError::ParseFloat(err)),
};
let year: u32 = year.parse()?;
let temp: f32 = temp.parse()?;
Ok(Climate { city, year, temp })
}
}