use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TextType {
Content,
Code,
Url,
Email,
Empty,
Title,
Button,
Link,
}
#[derive(Debug, Clone)]
pub struct TextAnalysis {
pub is_translatable: bool,
pub detected_lang: Option<String>,
pub confidence: f32,
pub text_type: TextType,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranslationRequest {
pub text: String,
pub source_lang: String,
pub target_lang: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranslationResponse {
pub translated_text: String,
pub detected_source_lang: Option<String>,
}
#[derive(Debug, Clone)]
pub struct CacheEntry {
pub translated_text: String,
pub created_at: std::time::Instant,
pub access_count: usize,
}
impl CacheEntry {
pub fn new(translated_text: String) -> Self {
Self {
translated_text,
created_at: std::time::Instant::now(),
access_count: 1,
}
}
pub fn access(&mut self) {
self.access_count += 1;
}
pub fn is_expired(&self, ttl: std::time::Duration) -> bool {
self.created_at.elapsed() > ttl
}
}
#[cfg(feature = "html-support")]
#[derive(Debug, Clone)]
pub struct ElementInfo {
pub tag_name: String,
pub attr_name: Option<String>,
pub text_type: TextType,
pub priority: u8,
}
#[cfg(feature = "html-support")]
impl ElementInfo {
pub fn new(tag_name: String, attr_name: Option<String>) -> Self {
let text_type = match tag_name.as_str() {
"title" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => TextType::Title,
"a" => TextType::Link,
"button" | "input" => TextType::Button,
"code" | "pre" => TextType::Code,
_ => TextType::Content,
};
let priority = match text_type {
TextType::Title => 3,
TextType::Button | TextType::Link => 2,
TextType::Content => 1,
_ => 0,
};
Self {
tag_name,
attr_name,
text_type,
priority,
}
}
}