From 95a38620c1d90110d9a16716a0fe3e2288fa8c02 Mon Sep 17 00:00:00 2001 From: laentropia Date: Mon, 27 Apr 2026 16:48:37 -0600 Subject: [PATCH] addition: Fraction Error, Fraction and constructor --- src/lib.rs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b93cf3f..d5c0a18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,38 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +pub struct Fraction { + num: i64, + den: i64, +} + +#[derive(Debug)] +pub enum FractionError { + DivisionByZero, + ZeroDenominator, + Overflow, +} + +impl std::fmt::Display for FractionError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + FractionError::DivisionByZero => write!(f, "Division by zero"), + FractionError::ZeroDenominator => write!(f, "Denominator can't be zero"), + FractionError::Overflow => write!(f, "Numeric overflow"), + } + } +} + +impl std::error::Error for FractionError {} + +impl Fraction { + pub fn new(num: i64, den: i64) -> Result { + if den == 0 { + return Err(FractionError::ZeroDenominator); + } + + Ok(Fraction { num, den }) + } } #[cfg(test)] mod tests { use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } }