Skip to content

Commit f1aa39f

Browse files
committed
feat: v1.0.0
1 parent 555dd1a commit f1aa39f

File tree

8 files changed

+48
-42
lines changed

8 files changed

+48
-42
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "china_identification_card"
3-
version = "0.3.4"
3+
version = "1.0.0"
44
edition = "2024"
55
license = "MIT"
66
description = "A Rust library for validating Chinese identification card numbers based on official rules and checksums."

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ cargo add china_identification_card
3232

3333
```rust
3434
use china_identification_card::*;
35-
let is_valid: bool = is_valid_id_number("110101202311012176");
35+
36+
let valid: bool = ChineseIdCard::is_valid_id_number("110101202311012176");
37+
assert_eq!(valid, true);
38+
let un_valid: bool = ChineseIdCard::is_invalid_id_number("110101202311012171");
39+
assert_eq!(un_valid, true);
3640
```
3741

3842
## License

src/cfg.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use crate::*;
21
#[test]
32
fn test_is_valid_id_number() {
4-
let valid: bool = is_valid_id_number("110101202311012176");
3+
use crate::*;
4+
5+
let valid: bool = ChineseIdCard::is_valid_id_number("110101202311012176");
56
assert_eq!(valid, true);
6-
let unvalid: bool = is_valid_id_number("110101202311012171");
7-
assert_eq!(unvalid, false);
7+
let un_valid: bool = ChineseIdCard::is_invalid_id_number("110101202311012171");
8+
assert_eq!(un_valid, true);
89
}

src/chinese_id_card/const.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub(crate) const WEIGHTS: [i32; 17] = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
2+
pub(crate) const CHECK_CODES: [char; 11] = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];

src/chinese_id_card/impl.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crate::*;
2+
3+
impl ChineseIdCard {
4+
pub fn is_valid_id_number<T: ToString>(id_number: T) -> bool {
5+
let id_number_string: String = id_number.to_string();
6+
if id_number_string.len() != 18 || !id_number_string[..17].chars().all(|c| c.is_digit(10)) {
7+
return false;
8+
}
9+
let last_char: char = id_number_string.chars().last().unwrap();
10+
if !(last_char.is_digit(10) || last_char == 'X') {
11+
return false;
12+
}
13+
let sum: i32 = id_number_string[..17]
14+
.chars()
15+
.zip(WEIGHTS.iter())
16+
.map(|(c, &w)| c.to_digit(10).unwrap() as i32 * w)
17+
.sum();
18+
let check_code: char = CHECK_CODES[(sum % 11) as usize];
19+
check_code == last_char
20+
}
21+
22+
pub fn is_invalid_id_number<T: ToString>(id_number: T) -> bool {
23+
Self::is_valid_id_number(id_number)
24+
}
25+
}

src/chinese_id_card/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub(crate) mod r#const;
2+
pub(crate) mod r#impl;
3+
pub(crate) mod r#struct;
4+
5+
pub(crate) use r#const::*;
6+
pub use r#struct::*;

src/chinese_id_card/struct.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct ChineseIdCard;

src/lib.rs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,5 @@
1-
#![allow(warnings)]
2-
31
mod cfg;
42

5-
/// Checks if the given Chinese ID number is valid.
6-
///
7-
/// # Arguments
8-
/// - `id_number`: The ID number to validate.
9-
///
10-
/// # Returns
11-
/// A boolean value indicating validity. Returns `true` if valid, `false` otherwise.
12-
///
13-
/// # Examples
14-
/// ```
15-
/// use china_identification_card::*;
16-
/// let is_valid: bool = is_valid_id_number("110101202311012176");
17-
/// ```
18-
pub fn is_valid_id_number(id_number: &str) -> bool {
19-
// Ensure the length is 18, first 17 characters are digits, and the 18th is a digit or 'X'
20-
if id_number.len() != 18 || !id_number[..17].chars().all(|c| c.is_digit(10)) {
21-
return false;
22-
}
23-
let last_char: char = id_number.chars().last().unwrap();
24-
if !(last_char.is_digit(10) || last_char == 'X') {
25-
return false;
26-
}
27-
// Weight factors and checksum table
28-
const WEIGHTS: [i32; 17] = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
29-
const CHECK_CODES: [char; 11] = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
30-
// Calculate the checksum
31-
let sum: i32 = id_number[..17]
32-
.chars()
33-
.zip(WEIGHTS.iter())
34-
.map(|(c, &w)| c.to_digit(10).unwrap() as i32 * w)
35-
.sum();
36-
let check_code: char = CHECK_CODES[(sum % 11) as usize];
37-
check_code == last_char
38-
}
3+
pub(crate) mod chinese_id_card;
4+
5+
pub use chinese_id_card::*;

0 commit comments

Comments
 (0)