translation_lib/
types.rs

1//! 类型定义模块
2//!
3//! 定义翻译库中使用的简化数据结构和类型。
4
5use serde::{Deserialize, Serialize};
6
7/// 文本类型枚举
8///
9/// 用于标识不同类型的文本内容,以便进行相应的处理。
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub enum TextType {
12    /// 普通文本内容
13    Content,
14    /// 代码片段
15    Code,
16    /// URL链接
17    Url,
18    /// 邮箱地址
19    Email,
20    /// 空文本
21    Empty,
22    /// 标题
23    Title,
24    /// 按钮文本
25    Button,
26    /// 链接文本
27    Link,
28}
29
30/// 文本分析结果
31///
32/// 包含对文本进行语言检测和类型分析的结果。
33#[derive(Debug, Clone)]
34pub struct TextAnalysis {
35    /// 是否可翻译
36    pub is_translatable: bool,
37    /// 检测到的语言
38    pub detected_lang: Option<String>,
39    /// 检测置信度 (0.0 - 1.0)
40    pub confidence: f32,
41    /// 文本类型
42    pub text_type: TextType,
43}
44
45/// 翻译请求
46///
47/// 发送到翻译API的请求结构。
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct TranslationRequest {
50    /// 要翻译的文本
51    pub text: String,
52    /// 源语言代码
53    pub source_lang: String,
54    /// 目标语言代码
55    pub target_lang: String,
56}
57
58/// 翻译响应
59///
60/// 从翻译API返回的响应结构。
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct TranslationResponse {
63    /// 翻译后的文本
64    pub translated_text: String,
65    /// 检测到的源语言
66    pub detected_source_lang: Option<String>,
67}
68
69/// 缓存条目
70///
71/// 缓存中存储的翻译结果条目。
72#[derive(Debug, Clone)]
73pub struct CacheEntry {
74    /// 翻译结果
75    pub translated_text: String,
76    /// 创建时间戳
77    pub created_at: std::time::Instant,
78    /// 访问次数
79    pub access_count: usize,
80}
81
82impl CacheEntry {
83    /// 创建新的缓存条目
84    pub fn new(translated_text: String) -> Self {
85        Self {
86            translated_text,
87            created_at: std::time::Instant::now(),
88            access_count: 1,
89        }
90    }
91
92    /// 标记访问
93    pub fn access(&mut self) {
94        self.access_count += 1;
95    }
96
97    /// 检查是否过期
98    pub fn is_expired(&self, ttl: std::time::Duration) -> bool {
99        self.created_at.elapsed() > ttl
100    }
101}
102
103/// HTML元素信息
104///
105/// 在HTML翻译过程中使用的元素信息。
106#[cfg(feature = "html-support")]
107#[derive(Debug, Clone)]
108pub struct ElementInfo {
109    /// 元素标签名
110    pub tag_name: String,
111    /// 属性名(如果是属性翻译)
112    pub attr_name: Option<String>,
113    /// 文本类型
114    pub text_type: TextType,
115    /// 优先级
116    pub priority: u8,
117}
118
119#[cfg(feature = "html-support")]
120impl ElementInfo {
121    /// 创建新的元素信息
122    pub fn new(tag_name: String, attr_name: Option<String>) -> Self {
123        let text_type = match tag_name.as_str() {
124            "title" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => TextType::Title,
125            "a" => TextType::Link,
126            "button" | "input" => TextType::Button,
127            "code" | "pre" => TextType::Code,
128            _ => TextType::Content,
129        };
130
131        let priority = match text_type {
132            TextType::Title => 3,
133            TextType::Button | TextType::Link => 2,
134            TextType::Content => 1,
135            _ => 0,
136        };
137
138        Self {
139            tag_name,
140            attr_name,
141            text_type,
142            priority,
143        }
144    }
145}