| Crates.io | location-rs |
| lib.rs | location-rs |
| version | 0.1.3 |
| created_at | 2025-10-22 13:24:40.97794+00 |
| updated_at | 2025-10-23 14:40:26.673591+00 |
| description | A Rust library for parsing country codes from text titles with support for Chinese language |
| homepage | |
| repository | https://github.com/chenxxpro/location-rs |
| max_upload_size | |
| id | 1895659 |
| size | 323,738 |
一个高性能的 Rust 库,用于从文本标题中解析国家或地区代码。
注意: 本项目的实现代码和测试用例均由 AI 生成,采用最新的 Rust 2021 语言标准开发。
将以下内容添加到你的 Cargo.toml 文件中:
[dependencies]
location-rs = "0.1.3"
use location_rs::parse_country_code;
// 解析带有国家代码的文本
let result = parse_country_code("@HK Vip会员");
assert!(result.is_ok());
// 解析中文国家名称
let result = parse_country_code("中国用户数据");
assert!(result.is_ok());
// 解析 ISO 代码
let result = parse_country_code("US-123456");
assert!(result.is_ok());
use location_rs::{Parser, ParserConfig};
// 创建自定义配置
let config = ParserConfig {
case_sensitive: true, // 区分大小写
fuzzy_match: true, // 启用模糊匹配
};
// 使用自定义配置创建解析器
let parser = Parser::with_config(config);
// 执行解析
let result = parser.parse("CN-SHANGHAI");
assert!(result.is_ok());
use location_rs::parse_country_code;
// 处理可能的解析错误
let result = parse_country_code("这是一段没有国家代码的文本");
if let Err(error) = result {
println!("解析失败: {}", error);
}
CN, US, JP)CHN, USA, JPN)中国, 美国, 日本)中國, 美國, 日本)该库采用多阶段解析策略:
InvalidInput:输入文本为空或过长NotFound:在文本中未找到有效的国家代码ConfigError:配置加载或解析失败本库包含全面的单元测试和集成测试,确保在各种输入情况下的正确性。
# 运行所有测试
cargo test
# 运行基准测试(可选)
cargo bench
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情
欢迎提交 Issues 和 Pull Requests!请确保你的代码通过所有测试并遵循 Rust 的代码风格指南。
serde/serde_json:配置解析thiserror:错误处理criterion(开发依赖):性能基准测试