feat(core): 扩展 API、完善日志与代码文档规范

- 公开颜色过滤与字符集限制扩展 API,修复宏路径
- 库内打印替换为 tracing 日志,清理遗留废弃代码
- 补充核心逻辑单元测试与 crate 元数据
- 开启 missing_docs 并统一 rustfmt/clippy 格式
This commit is contained in:
2026-08-06 19:58:54 +08:00
parent 1362243f4e
commit fe61895926
20 changed files with 696 additions and 202 deletions

View File

@@ -17,6 +17,7 @@ pub enum Normalization {
}
impl Normalization {
/// 对像素值执行归一化。
#[inline(always)]
pub fn normalize(&self, pixel: f32) -> f32 {
match self {
@@ -40,16 +41,20 @@ pub enum Resize {
/// OCR 模型元数据:字符集、缩放策略、通道数与归一化配置。
#[derive(Debug, Clone)]
pub struct ModelMetadata {
/// 字符集。
pub charset: Charset,
/// 是否为单字识别模型
pub word: bool,
/// 缩放策略。
pub resize: Resize,
/// 图像通道数1 或 3
pub channel: u8,
/// 像素归一化配置。
pub normalization: Normalization,
}
impl ModelMetadata {
/// 创建模型元数据。
pub fn new(
charset: Charset,
word: bool,
@@ -84,3 +89,37 @@ impl ModelMetadata {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn normalization_zero_to_one() {
let n = Normalization::ZeroToOne;
assert_eq!(n.normalize(0.0), 0.0);
assert_eq!(n.normalize(255.0), 1.0);
}
#[test]
fn normalization_minus_one_to_one() {
let n = Normalization::MinusOneToOne;
assert_eq!(n.normalize(0.0), -1.0);
assert_eq!(n.normalize(255.0), 1.0);
}
#[test]
fn from_static_slice_builds_charset() {
let meta = ModelMetadata::from_static_slice(
&["", "a"],
false,
Resize::Fixed(64, 64),
1,
Normalization::ZeroToOne,
);
assert_eq!(meta.charset.size(), 2);
assert_eq!(meta.charset.char_to_index("a"), 1);
assert_eq!(meta.channel, 1);
assert!(!meta.word);
}
}