Skip to content

Commit e3db0ab

Browse files
committed
Cleaned up dependencies (2 removed, 1 updated)
1 parent 760f697 commit e3db0ab

File tree

6 files changed

+153
-204
lines changed

6 files changed

+153
-204
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ This project adheres to [Semantic Versioning](https://semver.org/).
55

66
---
77

8+
[1.1.1]: https://github.com/qbasic16/swiss_uid/releases/tag/1.1.1
89
[1.1.0]: https://github.com/qbasic16/swiss_uid/releases/tag/1.1.0
910
[1.0.2]: https://github.com/qbasic16/swiss_uid/releases/tag/1.0.2
1011
[1.0.1]: https://github.com/qbasic16/swiss_uid/releases/tag/1.0.1
1112
[1.0.0]: https://github.com/qbasic16/swiss_uid/releases/tag/1.0.0
1213

14+
## [1.1.1] - 2025-04-26
15+
16+
### Changed
17+
18+
- Updated crate `rand` to `0.9.*`
19+
20+
### Removed
21+
22+
- Removed crates `itertools` and `num` to simplify dependencies
23+
1324
## [1.1.0] - 2025-01-26
1425

1526
### Breaking

Cargo.lock

Lines changed: 33 additions & 105 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "swiss_uid"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
edition = "2021"
55
resolver = "2"
66
authors = ["Peter <qbasic16@gmail.com>"]
@@ -18,6 +18,4 @@ default = ["rand"]
1818
rand = ["dep:rand"]
1919

2020
[dependencies]
21-
itertools = "0.14.*"
22-
num = "0.4.*"
23-
rand = { version = "0.8.*", optional = true }
21+
rand = { version = "0.9.*", optional = true }

src/uid.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
use ::std::iter::zip;
12
use ::std::{error::Error, fmt, str::FromStr};
23

3-
use ::itertools::Itertools;
4-
54
use crate::utils::IntoNibblesNum;
65

76
// Factors as defined in the specification
@@ -11,19 +10,19 @@ const DIGIT_FACTORS: [u8; SwissUid::NUM_CHARS_DIGITS] = [5, 4, 3, 2, 7, 6, 5, 4]
1110
/// Calculates the check digit for the given 8 normal digits of the UID.
1211
#[inline]
1312
pub fn calculate_checkdigit(main_digits: &[u8]) -> Result<u8, UidError> {
14-
if main_digits.len() != DIGIT_FACTORS.len() {
15-
Err(UidError::InvalidFormat("UID must have 8 digits".to_owned()))
16-
} else {
17-
let checksum: u32 = DIGIT_FACTORS
18-
.iter()
19-
.zip_eq(main_digits.iter())
20-
.map(|v| (v.0 * v.1) as u32)
21-
.sum();
22-
match 11 - (checksum % 11) {
23-
11 => Ok(0u8),
24-
10 => Err(UidError::InvalidCheckDigit(10.to_string())),
25-
n => Ok(n as u8),
26-
}
13+
if main_digits.len() < DIGIT_FACTORS.len() {
14+
return Err(UidError::InvalidFormat(
15+
"UID must have at least 8 digits".to_owned(),
16+
));
17+
}
18+
19+
let checksum: u32 = zip(main_digits, &DIGIT_FACTORS)
20+
.map(|v| (v.0 * v.1) as u32)
21+
.sum();
22+
match 11 - (checksum % 11) {
23+
11 => Ok(0u8),
24+
10 => Err(UidError::InvalidCheckDigit(10.to_string())),
25+
n => Ok(n as u8),
2726
}
2827
}
2928

@@ -91,15 +90,15 @@ impl SwissUid {
9190
pub fn rand() -> Result<Self, UidError> {
9291
use rand::Rng;
9392

94-
let mut rng = rand::thread_rng();
93+
let mut rng = rand::rng();
9594
let mut n = [0u8; Self::NUM_CHARS_DIGITS];
9695
let mut n_iter = n.iter_mut();
9796

9897
// The first digit must be between 1 and 9
99-
*n_iter.next().unwrap() = rng.gen_range(1..10);
98+
*n_iter.next().unwrap() = rng.random_range(1..10);
10099
// The rest can be between 0 and 9
101100
for d in n_iter {
102-
*d = rng.gen_range(0..10);
101+
*d = rng.random_range(0..10);
103102
}
104103

105104
let p = calculate_checkdigit(&n).or_else(|_| {

src/utils.rs

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,3 @@
1-
use ::std::ops::{BitAnd, BitOr, Shl, Shr};
1+
mod nibble;
22

3-
use ::num::cast::AsPrimitive;
4-
5-
pub trait FromNibbles:
6-
Shl<usize, Output = Self> + Default + BitOr<Output = Self> + From<u8>
7-
{
8-
fn from_nibbles(digits: &[u8]) -> Self {
9-
digits
10-
.iter()
11-
.take(size_of::<Self>() * 2)
12-
.fold(Self::default(), |acc, &d| (acc << 4) | d.into())
13-
}
14-
}
15-
16-
impl FromNibbles for u16 {}
17-
impl FromNibbles for u32 {}
18-
19-
pub trait IntoNibblesNum<T>
20-
where
21-
T: FromNibbles,
22-
{
23-
fn into_nibbles_num(&self) -> T;
24-
}
25-
26-
impl<T> IntoNibblesNum<T> for [u8]
27-
where
28-
T: FromNibbles,
29-
{
30-
fn into_nibbles_num(&self) -> T {
31-
T::from_nibbles(self)
32-
}
33-
}
34-
35-
pub trait IntoNibbles:
36-
FromNibbles + Shr<usize, Output = Self> + BitAnd<Output = Self> + AsPrimitive<u8>
37-
{
38-
/// Returns an iterator over the nibbles (4-bit digits) of the number.
39-
/// The iterator starts with the most significant nibble.
40-
#[inline(always)]
41-
fn into_iter_nibbles(self) -> impl Iterator<Item = u8> {
42-
let n = self;
43-
(0..(size_of::<Self>() * 2))
44-
.into_iter()
45-
.rev()
46-
.map(move |i| (n >> (i * 4)).as_() & 0x0f)
47-
}
48-
}
49-
50-
impl IntoNibbles for u16 {}
51-
impl IntoNibbles for u32 {}
52-
53-
#[cfg(test)]
54-
mod tests {
55-
use super::*;
56-
57-
#[test]
58-
fn test_to_nibbles() {
59-
let n: u16 = 0x1234;
60-
let n_split: Vec<u8> = n.into_iter_nibbles().collect();
61-
assert_eq!(n_split, [1, 2, 3, 4]);
62-
}
63-
64-
#[test]
65-
fn test_from_nibbles() {
66-
let n = [1u8, 2u8, 3u8, 4u8];
67-
let n_quad: u16 = n.into_nibbles_num();
68-
assert_eq!(n_quad, 0x1234);
69-
}
70-
71-
#[test]
72-
fn test_to_quad_nibble_above_10() {
73-
let n = [11u8, 12u8, 13u8, 14u8];
74-
let n_quad: u16 = n.into_nibbles_num();
75-
assert_eq!(n_quad, 0xbcde);
76-
assert_eq!(format!("{n_quad:#x}"), format!("{:#x}", 0xbcde));
77-
}
78-
}
3+
pub use nibble::*;

0 commit comments

Comments
 (0)