write id_card test

This commit is contained in:
2025-08-15 10:50:54 +08:00
parent e0492e1d74
commit 02f1d7f850
8 changed files with 127 additions and 4 deletions

View File

@@ -6,5 +6,6 @@ edition = "2024"
[dependencies]
tokio.workspace = true
gotify-ws.workspace = true
log = "0.4.27"
env_logger = "0.11.8"
log.workspace = true
sea-orm.workspace = true
regex.workspace = true

View File

@@ -0,0 +1,74 @@
use regex::Regex;
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::sync::OnceLock;
static ID_REG: OnceLock<Regex> = OnceLock::new();
#[test]
fn test_split() {
let str = "朱春艳----320681197904304224";
if let Some((name, id)) = split_str(str) {
println!("name: {}, id: {}", name, id);
get_id_info(id)
}
}
fn split_str(str: &str) -> Option<(&str, &str)> {
let regex = ID_REG.get_or_init(|| Regex::new(r"(.*?)-+(\d+)").expect("正则创建失败"));
let mut value = None;
if let Some(res) = regex.captures(str) {
value = Some((res.get(1)?.as_str(), res.get(2)?.as_str()));
}
value
}
fn get_id_info(id: &str) {
let location = &id[0..6];
println!("location: {}", location);
let born = &id[6..14];
println!("born: {}", born);
let born_year = &id[6..10];
println!("born: {}", born_year);
let born_month = &id[10..12];
println!("born month: {}", born_month);
let born_day = &id[12..14];
println!("born day: {}", born_day);
let count = &id[14..18];
println!("born count: {}", count);
let validate = &id[18..19];
println!("validate number: {validate}")
}
#[test]
fn test_get_file() {
get_id_file();
}
fn get_id_file() {
let _ = env::current_dir().unwrap();
println!("当前的路径 {:#?}", env::current_dir().unwrap());
let path = Path::new("resources/ID/id.txt");
// let path = Path::new("resources/id.txt");
let file = File::open(path).expect("打开文件失败");
let buffer = BufReader::new(file);
for line in buffer.lines() {
// println!("line {}", line.unwrap());
if let Some((name, id)) = split_str(line.unwrap().as_str()) {
// println!("name: {}, id: {}", name, id);
if id.len() != 18 {
// println!("name: {}, id: {}", name, id);
continue;
// if id.len() != 17 {
// println!("name: {}, id: {}", name, id);
// continue;
// }
}
}
}
}

View File

@@ -1,2 +1,3 @@
#[cfg(test)]
mod base_api;
mod id_card;