File tree Expand file tree Collapse file tree
exercises/13_error_handling
solutions/13_error_handling Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -19,15 +19,6 @@ enum ParsePosNonzeroError {
1919 ParseInt ( ParseIntError ) ,
2020}
2121
22- impl ParsePosNonzeroError {
23- fn from_creation ( err : CreationError ) -> Self {
24- Self :: Creation ( err)
25- }
26-
27- // TODO: Add another error conversion function here.
28- // fn from_parse_int(???) -> Self { ??? }
29- }
30-
3122#[ derive( PartialEq , Debug ) ]
3223struct PositiveNonzeroInteger ( u64 ) ;
3324
@@ -44,7 +35,7 @@ impl PositiveNonzeroInteger {
4435 // TODO: change this to return an appropriate error instead of panicking
4536 // when `parse()` returns an error.
4637 let x: i64 = s. parse ( ) . unwrap ( ) ;
47- Self :: new ( x) . map_err ( ParsePosNonzeroError :: from_creation )
38+ Self :: new ( x) . map_err ( ParsePosNonzeroError :: Creation )
4839 }
4940}
5041
Original file line number Diff line number Diff line change @@ -19,16 +19,6 @@ enum ParsePosNonzeroError {
1919 ParseInt ( ParseIntError ) ,
2020}
2121
22- impl ParsePosNonzeroError {
23- fn from_creation ( err : CreationError ) -> Self {
24- Self :: Creation ( err)
25- }
26-
27- fn from_parse_int ( err : ParseIntError ) -> Self {
28- Self :: ParseInt ( err)
29- }
30- }
31-
3222// As an alternative solution, implementing the `From` trait allows for the
3323// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
3424// using the `?` operator, without the need to call `map_err`.
@@ -59,9 +49,9 @@ impl PositiveNonzeroInteger {
5949 fn parse ( s : & str ) -> Result < Self , ParsePosNonzeroError > {
6050 // Return an appropriate error instead of panicking when `parse()`
6151 // returns an error.
62- let x: i64 = s. parse ( ) . map_err ( ParsePosNonzeroError :: from_parse_int ) ?;
63- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
64- Self :: new ( x) . map_err ( ParsePosNonzeroError :: from_creation )
52+ let x: i64 = s. parse ( ) . map_err ( ParsePosNonzeroError :: ParseInt ) ?;
53+ // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54+ Self :: new ( x) . map_err ( ParsePosNonzeroError :: Creation )
6555 }
6656}
6757
You can’t perform that action at this time.
0 commit comments