From 9b7a5c041e9856379154b109b2ee2f3e979d70f7 Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 26 Jun 2024 15:54:18 +0200 Subject: [PATCH] errors4 solution --- exercises/13_error_handling/errors4.rs | 23 ++++++++------ rustlings-macros/info.toml | 8 ++--- solutions/13_error_handling/errors4.rs | 43 +++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs index 993d42a1..ba01e54b 100644 --- a/exercises/13_error_handling/errors4.rs +++ b/exercises/13_error_handling/errors4.rs @@ -1,16 +1,16 @@ -#[derive(PartialEq, Debug)] -struct PositiveNonzeroInteger(u64); - #[derive(PartialEq, Debug)] enum CreationError { Negative, Zero, } +#[derive(PartialEq, Debug)] +struct PositiveNonzeroInteger(u64); + impl PositiveNonzeroInteger { - fn new(value: i64) -> Result { - // Hmm... Why is this always returning an Ok value? - Ok(PositiveNonzeroInteger(value as u64)) + fn new(value: i64) -> Result { + // TODO: This function shouldn't always return an `Ok`. + Ok(Self(value as u64)) } } @@ -24,11 +24,14 @@ mod tests { #[test] fn test_creation() { - assert!(PositiveNonzeroInteger::new(10).is_ok()); assert_eq!( - Err(CreationError::Negative), - PositiveNonzeroInteger::new(-10) + PositiveNonzeroInteger::new(10), + Ok(PositiveNonzeroInteger(10)), ); - assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0)); + assert_eq!( + PositiveNonzeroInteger::new(-10), + Err(CreationError::Negative), + ); + assert_eq!(PositiveNonzeroInteger::new(0), Err(CreationError::Zero)); } } diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index 74cb79dd..d39044c0 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -683,11 +683,9 @@ name = "errors4" dir = "13_error_handling" hint = """ `PositiveNonzeroInteger::new` is always creating a new instance and returning -an `Ok` result. - -It should be doing some checking, returning an `Err` result if those checks -fail, and only returning an `Ok` result if those checks determine that -everything is... okay :)""" +an `Ok` result. But it should be doing some checking, returning an `Err` if +those checks fail, and only returning an `Ok` if those checks determine that +everything is… okay :)""" [[exercises]] name = "errors5" diff --git a/solutions/13_error_handling/errors4.rs b/solutions/13_error_handling/errors4.rs index 4e181989..c43f493b 100644 --- a/solutions/13_error_handling/errors4.rs +++ b/solutions/13_error_handling/errors4.rs @@ -1 +1,42 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +#[derive(PartialEq, Debug)] +enum CreationError { + Negative, + Zero, +} + +#[derive(PartialEq, Debug)] +struct PositiveNonzeroInteger(u64); + +impl PositiveNonzeroInteger { + fn new(value: i64) -> Result { + if value == 0 { + Err(CreationError::Zero) + } else if value < 0 { + Err(CreationError::Negative) + } else { + Ok(Self(value as u64)) + } + } +} + +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_creation() { + assert_eq!( + PositiveNonzeroInteger::new(10), + Ok(PositiveNonzeroInteger(10)), + ); + assert_eq!( + PositiveNonzeroInteger::new(-10), + Err(CreationError::Negative), + ); + assert_eq!(PositiveNonzeroInteger::new(0), Err(CreationError::Zero)); + } +}