Skip to main content

crates_docs/tools/docs/
mod.rs

1//! 文档查询工具模块
2//!
3//! 提供用于查询 Rust crate 文档的工具和服务。
4//!
5//! # 子模块
6//!
7//! - `cache`: 文档缓存
8//! - `html`: HTML 处理
9//! - `lookup_crate`: Crate 文档查找
10//! - `lookup_item`: 项目文档查找
11//! - `search`: Crate 搜索
12//!
13//! # 示例
14//!
15//! ```rust,no_run
16//! use std::sync::Arc;
17//! use crates_docs::tools::docs::DocService;
18//! use crates_docs::cache::memory::MemoryCache;
19//!
20//! let cache = Arc::new(MemoryCache::new(1000));
21//! let service = DocService::new(cache).expect("Failed to create DocService");
22//! ```
23
24pub mod cache;
25pub mod html;
26pub mod lookup_crate;
27pub mod lookup_item;
28pub mod search;
29
30use crate::cache::{Cache, CacheConfig};
31use crate::config::PerformanceConfig;
32use std::sync::Arc;
33
34/// 文档服务
35///
36/// 提供 HTTP 客户端(带自动重试)、缓存和文档缓存的集中管理。
37///
38/// # 字段
39///
40/// - `client`: 带重试中间件的 HTTP 客户端
41/// - `cache`: 通用缓存实例
42/// - `doc_cache`: 文档专用缓存
43pub struct DocService {
44    client: reqwest_middleware::ClientWithMiddleware,
45    cache: Arc<dyn Cache>,
46    doc_cache: cache::DocCache,
47}
48
49impl DocService {
50    /// 创建新的文档服务(使用默认 TTL)
51    ///
52    /// # 参数
53    ///
54    /// * `cache` - 缓存实例
55    ///
56    /// # 错误
57    ///
58    /// 如果 HTTP 客户端创建失败,返回错误
59    ///
60    /// # 示例
61    ///
62    /// ```rust,no_run
63    /// use std::sync::Arc;
64    /// use crates_docs::tools::docs::DocService;
65    /// use crates_docs::cache::memory::MemoryCache;
66    ///
67    /// let cache = Arc::new(MemoryCache::new(1000));
68    /// let service = DocService::new(cache).expect("Failed to create DocService");
69    /// ```
70    pub fn new(cache: Arc<dyn Cache>) -> crate::error::Result<Self> {
71        Self::with_config(cache, &CacheConfig::default())
72    }
73
74    /// 创建新的文档服务(使用自定义缓存配置)
75    ///
76    /// # 参数
77    ///
78    /// * `cache` - 缓存实例
79    /// * `cache_config` - 缓存配置
80    ///
81    /// # 错误
82    ///
83    /// 如果 HTTP 客户端创建失败,返回错误
84    pub fn with_config(
85        cache: Arc<dyn Cache>,
86        cache_config: &CacheConfig,
87    ) -> crate::error::Result<Self> {
88        let ttl = cache::DocCacheTtl::from_cache_config(cache_config);
89        let doc_cache = cache::DocCache::with_ttl(cache.clone(), ttl);
90        // Use default performance config for backward compatibility
91        let perf_config = PerformanceConfig::default();
92        let client = crate::utils::create_http_client_from_config(&perf_config)
93            .build()
94            .map_err(|e| {
95                crate::error::Error::initialization(
96                    "http_client",
97                    format!("Failed to create HTTP client with retry: {e}"),
98                )
99            })?;
100        Ok(Self {
101            client,
102            cache,
103            doc_cache,
104        })
105    }
106
107    /// 创建新的文档服务(使用完整配置)
108    ///
109    /// # 参数
110    ///
111    /// * `cache` - 缓存实例
112    /// * `cache_config` - 缓存配置
113    /// * `perf_config` - 性能配置
114    ///
115    /// # 错误
116    ///
117    /// 如果 HTTP 客户端创建失败,返回错误
118    pub fn with_full_config(
119        cache: Arc<dyn Cache>,
120        cache_config: &CacheConfig,
121        perf_config: &PerformanceConfig,
122    ) -> crate::error::Result<Self> {
123        let ttl = cache::DocCacheTtl::from_cache_config(cache_config);
124        let doc_cache = cache::DocCache::with_ttl(cache.clone(), ttl);
125        let client = crate::utils::create_http_client_from_config(perf_config)
126            .build()
127            .map_err(|e| {
128                crate::error::Error::initialization(
129                    "http_client",
130                    format!("Failed to create HTTP client with retry: {e}"),
131                )
132            })?;
133        Ok(Self {
134            client,
135            cache,
136            doc_cache,
137        })
138    }
139
140    /// 获取 HTTP 客户端(带重试中间件)
141    #[must_use]
142    pub fn client(&self) -> &reqwest_middleware::ClientWithMiddleware {
143        &self.client
144    }
145
146    /// 获取缓存实例
147    #[must_use]
148    pub fn cache(&self) -> &Arc<dyn Cache> {
149        &self.cache
150    }
151
152    /// 获取文档缓存
153    #[must_use]
154    pub fn doc_cache(&self) -> &cache::DocCache {
155        &self.doc_cache
156    }
157}
158
159impl Default for DocService {
160    fn default() -> Self {
161        let cache = Arc::new(crate::cache::memory::MemoryCache::new(1000));
162        Self::new(cache).expect("Failed to create default DocService")
163    }
164}
165
166/// 重新导出工具类型
167pub use lookup_crate::LookupCrateTool;
168pub use lookup_item::LookupItemTool;
169pub use search::SearchCratesTool;
170
171/// 重新导出缓存类型
172pub use cache::DocCacheTtl;