Remove redundant error conversion functions, use enum constructors instead

This commit is contained in:
Lev Krikken 2025-11-25 05:41:16 +01:00
parent b5d440fdc3
commit c6c6d27232
2 changed files with 4 additions and 23 deletions

View File

@ -19,15 +19,6 @@ enum ParsePosNonzeroError {
ParseInt(ParseIntError),
}
impl ParsePosNonzeroError {
fn from_creation(err: CreationError) -> Self {
Self::Creation(err)
}
// TODO: Add another error conversion function here.
// fn from_parse_int(???) -> Self { ??? }
}
#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);
@ -44,7 +35,7 @@ impl PositiveNonzeroInteger {
// TODO: change this to return an appropriate error instead of panicking
// when `parse()` returns an error.
let x: i64 = s.parse().unwrap();
Self::new(x).map_err(ParsePosNonzeroError::from_creation)
Self::new(x).map_err(ParsePosNonzeroError::Creation)
}
}

View File

@ -19,16 +19,6 @@ enum ParsePosNonzeroError {
ParseInt(ParseIntError),
}
impl ParsePosNonzeroError {
fn from_creation(err: CreationError) -> Self {
Self::Creation(err)
}
fn from_parse_int(err: ParseIntError) -> Self {
Self::ParseInt(err)
}
}
// As an alternative solution, implementing the `From` trait allows for the
// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
// using the `?` operator, without the need to call `map_err`.
@ -59,9 +49,9 @@ impl PositiveNonzeroInteger {
fn parse(s: &str) -> Result<Self, ParsePosNonzeroError> {
// Return an appropriate error instead of panicking when `parse()`
// returns an error.
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parse_int)?;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Self::new(x).map_err(ParsePosNonzeroError::from_creation)
let x: i64 = s.parse().map_err(ParsePosNonzeroError::ParseInt)?;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Self::new(x).map_err(ParsePosNonzeroError::Creation)
}
}