markdown_translator/
types.rs

1//! 类型定义模块
2//!
3//! 定义翻译库中使用的所有数据结构和配置类型。
4
5use serde::{Deserialize, Serialize};
6
7/// 翻译配置
8///
9/// 包含翻译服务的所有配置选项,如API地址、语言设置、性能参数等。
10///
11/// # 字段说明
12///
13/// * `enabled` - 是否启用翻译功能
14/// * `source_lang` - 源语言代码,"auto"表示自动检测
15/// * `target_lang` - 目标语言代码
16/// * `deeplx_api_url` - DeepLX API地址
17/// * `max_requests_per_second` - 每秒最大请求数
18/// * `max_text_length` - 单次翻译的最大文本长度
19/// * `max_paragraphs_per_request` - 单次请求的最大段落数
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct TranslationConfig {
22    /// 是否启用翻译功能
23    pub enabled: bool,
24    /// 源语言代码,"auto"表示自动检测
25    pub source_lang: String,
26    /// 目标语言代码
27    pub target_lang: String,
28    /// DeepLX API地址
29    pub deeplx_api_url: String,
30    /// 每秒最大请求数
31    pub max_requests_per_second: f64,
32    /// 单次翻译的最大文本长度
33    pub max_text_length: usize,
34    /// 单次请求的最大段落数
35    pub max_paragraphs_per_request: usize,
36}
37
38impl Default for TranslationConfig {
39    fn default() -> Self {
40        Self {
41            enabled: false,
42            source_lang: "auto".to_string(),
43            target_lang: "zh".to_string(),
44            deeplx_api_url: "http://localhost:1188/translate".to_string(),
45            max_requests_per_second: 0.5,
46            max_text_length: 3000,
47            max_paragraphs_per_request: 10,
48        }
49    }
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct RetryConfig {
54    pub max_retries: usize,
55    pub initial_delay_ms: u64,
56    pub max_delay_ms: u64,
57    pub backoff_multiplier: f64,
58}
59
60impl Default for RetryConfig {
61    fn default() -> Self {
62        Self {
63            max_retries: 1,          // 减少重试次数
64            initial_delay_ms: 100,   // 减少初始延迟
65            max_delay_ms: 1000,      // 减少最大延迟
66            backoff_multiplier: 1.2, // 减少退避倍数
67        }
68    }
69}
70
71#[derive(Debug, Serialize, Deserialize)]
72pub struct DeepLXRequest {
73    pub text: String,
74    pub source_lang: String,
75    pub target_lang: String,
76}
77
78#[derive(Debug, Serialize, Deserialize)]
79pub struct DpTransRequest {
80    pub text: String,
81    pub source_lang: String,
82    pub target_lang: String,
83}
84
85#[derive(Debug, Deserialize)]
86pub struct DeepLXResponse {
87    pub code: i32,
88    pub data: String,
89}
90
91#[derive(Debug, Clone)]
92pub struct TextSegment {
93    pub content: String,
94    pub is_code_block: bool,
95}