1use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub enum TextType {
12 Content,
14 Code,
16 Url,
18 Email,
20 Empty,
22 Title,
24 Button,
26 Link,
28}
29
30#[derive(Debug, Clone)]
34pub struct TextAnalysis {
35 pub is_translatable: bool,
37 pub detected_lang: Option<String>,
39 pub confidence: f32,
41 pub text_type: TextType,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct TranslationRequest {
50 pub text: String,
52 pub source_lang: String,
54 pub target_lang: String,
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct TranslationResponse {
63 pub translated_text: String,
65 pub detected_source_lang: Option<String>,
67}
68
69#[derive(Debug, Clone)]
73pub struct CacheEntry {
74 pub translated_text: String,
76 pub created_at: std::time::Instant,
78 pub access_count: usize,
80}
81
82impl CacheEntry {
83 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 pub fn access(&mut self) {
94 self.access_count += 1;
95 }
96
97 pub fn is_expired(&self, ttl: std::time::Duration) -> bool {
99 self.created_at.elapsed() > ttl
100 }
101}
102
103#[cfg(feature = "html-support")]
107#[derive(Debug, Clone)]
108pub struct ElementInfo {
109 pub tag_name: String,
111 pub attr_name: Option<String>,
113 pub text_type: TextType,
115 pub priority: u8,
117}
118
119#[cfg(feature = "html-support")]
120impl ElementInfo {
121 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}