prppper error handling

This commit is contained in:
Hariettemaina 2023-05-25 11:31:14 +03:00
parent d47d23ee86
commit f872682252

View File

@ -8,7 +8,7 @@
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::num::ParseIntError;
@ -24,13 +24,15 @@ impl ParsePosNonzeroError {
ParsePosNonzeroError::Creation(err)
}
// TODO: add another error conversion function here.
// fn from_parseint...
fn from_parseint(err: ParseIntError)-> ParsePosNonzeroError{
ParsePosNonzeroError::ParseInt(err)
}
}
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
// TODO: change this to return an appropriate error instead of panicking
// when `parse()` returns an error.
let x: i64 = s.parse().unwrap();
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?;
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
}