summaryrefslogtreecommitdiff
path: root/rustlings/solutions/13_error_handling/errors6.rs
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2026-10-02 21:53:51 -0500
committerHunter Kvalevog <hunter@kvog.sh>2026-10-02 21:53:51 -0500
commit4dfc11d74ab02ac5765ae5d08fb750c91f4744b4 (patch)
tree17ba62056d6b691dd0217b0e8f1b3a5826dce0f6 /rustlings/solutions/13_error_handling/errors6.rs
parent02d8f1c62896576e1266553a56980eddbc2a88d7 (diff)
rustlings: move to rust/
Diffstat (limited to 'rustlings/solutions/13_error_handling/errors6.rs')
-rw-r--r--rustlings/solutions/13_error_handling/errors6.rs106
1 files changed, 0 insertions, 106 deletions
diff --git a/rustlings/solutions/13_error_handling/errors6.rs b/rustlings/solutions/13_error_handling/errors6.rs
deleted file mode 100644
index ce18073..0000000
--- a/rustlings/solutions/13_error_handling/errors6.rs
+++ /dev/null
@@ -1,106 +0,0 @@
-// Using catch-all error types like `Box<dyn Error>` isn't recommended for
-// library code where callers might want to make decisions based on the error
-// content instead of printing it out or propagating it further. Here, we define
-// a custom error type to make it possible for callers to decide what to do next
-// when our function returns an error.
-
-use std::num::ParseIntError;
-
-#[derive(PartialEq, Debug)]
-enum CreationError {
- Negative,
- Zero,
-}
-
-// A custom error type that we will be using in `PositiveNonzeroInteger::parse`.
-#[derive(PartialEq, Debug)]
-enum ParsePosNonzeroError {
- Creation(CreationError),
- 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`.
-//
-// ```
-// let x: i64 = s.parse()?;
-// ```
-//
-// Traits like `From` will be dealt with in later exercises.
-impl From<ParseIntError> for ParsePosNonzeroError {
- fn from(err: ParseIntError) -> Self {
- ParsePosNonzeroError::ParseInt(err)
- }
-}
-
-#[derive(PartialEq, Debug)]
-struct PositiveNonzeroInteger(u64);
-
-impl PositiveNonzeroInteger {
- fn new(value: i64) -> Result<Self, CreationError> {
- match value {
- x if x < 0 => Err(CreationError::Negative),
- 0 => Err(CreationError::Zero),
- x => Ok(Self(x as u64)),
- }
- }
-
- 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)
- }
-}
-
-fn main() {
- // You can optionally experiment here.
-}
-
-#[cfg(test)]
-mod test {
- use super::*;
-
- #[test]
- fn test_parse_error() {
- assert!(matches!(
- PositiveNonzeroInteger::parse("not a number"),
- Err(ParsePosNonzeroError::ParseInt(_)),
- ));
- }
-
- #[test]
- fn test_negative() {
- assert_eq!(
- PositiveNonzeroInteger::parse("-555"),
- Err(ParsePosNonzeroError::Creation(CreationError::Negative)),
- );
- }
-
- #[test]
- fn test_zero() {
- assert_eq!(
- PositiveNonzeroInteger::parse("0"),
- Err(ParsePosNonzeroError::Creation(CreationError::Zero)),
- );
- }
-
- #[test]
- fn test_positive() {
- let x = PositiveNonzeroInteger::new(42).unwrap();
- assert_eq!(x.0, 42);
- assert_eq!(PositiveNonzeroInteger::parse("42"), Ok(x));
- }
-}