crates_docs/tools/docs/
mod.rs1pub 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
34pub struct DocService {
44 client: reqwest::Client,
45 cache: Arc<dyn Cache>,
46 doc_cache: cache::DocCache,
47}
48
49impl DocService {
50 pub fn new(cache: Arc<dyn Cache>) -> crate::error::Result<Self> {
71 Self::with_config(cache, &CacheConfig::default())
72 }
73
74 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 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: {e}"),
98 )
99 })?;
100 Ok(Self {
101 client,
102 cache,
103 doc_cache,
104 })
105 }
106
107 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: {e}"),
131 )
132 })?;
133 Ok(Self {
134 client,
135 cache,
136 doc_cache,
137 })
138 }
139
140 #[must_use]
142 pub fn client(&self) -> &reqwest::Client {
143 &self.client
144 }
145
146 #[must_use]
148 pub fn cache(&self) -> &Arc<dyn Cache> {
149 &self.cache
150 }
151
152 #[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
166pub use lookup_crate::LookupCrateTool;
168pub use lookup_item::LookupItemTool;
169pub use search::SearchCratesTool;
170
171pub use cache::DocCacheTtl;