Skip to content

Commit

Permalink
Implement TryFrom<&DigitSequence> for unsigned types
Browse files Browse the repository at this point in the history
  • Loading branch information
giancosta86 committed Apr 13, 2024
1 parent 0e67b7a commit c8e5d3a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "digit-sequence"
version = "0.3.0"
version = "0.3.1"

authors = ["Gianluca Costa <gianluca@gianlucacosta.info>"]
edition = "2021"
Expand Down
15 changes: 15 additions & 0 deletions src/digit_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ use std::fmt::Display;
/// # }
/// ```
///
/// Conversion from &DigitSequence is also supported:
///
/// ```
/// use digit_sequence::{CrateResult, CrateError, DigitSequence};
///
/// # fn main() -> CrateResult<()> {
/// let sequence: DigitSequence = "90".parse()?;
/// let reference = &sequence;
/// let number: u128 = reference.try_into()?;
/// assert_eq!(number, 90);
///
/// # Ok(())
/// # }
/// ```
///
/// # Usage
///
/// This data structure implements [IntoIterator] and also provides the [iter](Self::iter) method - thus enabling standard iterations as well as the [Iterator] methods.
Expand Down
88 changes: 86 additions & 2 deletions src/integers/to_ints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ macro_rules! impl_try_to_unsigned {
type Error = CrateError;

fn try_from(value: DigitSequence) -> CrateResult<Self> {
Self::try_from(&value)
}
}

impl TryFrom<&DigitSequence> for $type {
type Error = CrateError;

fn try_from(value: &DigitSequence) -> CrateResult<Self> {
let mut result = 0 as Self;

let enumerated_increasing_digits = value.iter().rev().enumerate();
Expand Down Expand Up @@ -58,9 +66,9 @@ mod tests {
+ Copy,
{
let sequence: DigitSequence = source.into();
let roundtrip_result: T = sequence.try_into().unwrap();
let roundtrip: T = sequence.try_into().unwrap();

eq!(roundtrip_result, source);
eq!(roundtrip, source);
}

fn test_case_failure(source: &str) {
Expand Down Expand Up @@ -110,5 +118,81 @@ mod tests {
test_case_failure("1".repeat(100).as_str());
}
}

describe "Converting a reference to digit sequence to an integer" {
it "should work" {
let source = 90u8;

let sequence: DigitSequence = source.into();

let roundtrip: u8 = (&sequence).try_into().unwrap();

eq!(roundtrip, source);

}

macro_rules! test_case_ok(
($source: expr, $type: ty) => {
let sequence: DigitSequence = $source.into();
let reference = &sequence;
let roundtrip: $type = reference.try_into().unwrap();

eq!(roundtrip, $source);
}
);

fn test_case_failure(source: &str) {
let sequence: DigitSequence = source.parse().unwrap();
let reference = &sequence;
let conversion_result: CrateResult<u128> = reference.try_into();

eq!(conversion_result.unwrap_err(), CrateError::Overflow);
}

it "should convert to u8" {
test_case_ok!(90u8, u8);
}

it "should convert to u16" {
test_case_ok!(90u16, u16);
}

it "should convert to u32" {
test_case_ok!(90u32, u32);
}

it "should convert to u64" {
test_case_ok!(90u64, u64);
}

it "should convert to u128" {
test_case_ok!(90u128, u128);
}

it "should convert to usize" {
test_case_ok!(90usize, usize);
}

it "should convert 0" {
test_case_ok!(0u8, u8);
}

it "should convert u128::MAX" {
test_case_ok!(u128::MAX, u128);
}

it "should NOT convert u128::MAX + 1" {
test_case_failure("340282366920938463463374607431768211456");
}

it "should NOT convert a huge sequence of 1" {
test_case_failure("1".repeat(100).as_str());
}

it "should NOT convert a 1 of huge magnitude" {
test_case_failure(format!("1{}", "0".repeat(100)).as_str());
}
}

}
}
6 changes: 6 additions & 0 deletions src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ mod tests {
eq!(CrateError::NegativeNumber(-90).to_string(), "Cannot convert negative number: -90");
}
}

describe "when an overflow occurs" {
it "should describe it" {
eq!(CrateError::Overflow.to_string(), "Overflow");
}
}
}
}
}

0 comments on commit c8e5d3a

Please sign in to comment.