1use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6pub const HUGGINGFACE_XET_ENDPOINT: &str = "https://transfer.xethub.hf.co";
8
9fn read_huggingface_token_file() -> Option<String> {
16 let token_path = if let Ok(path) = std::env::var("HF_TOKEN_PATH") {
17 PathBuf::from(path)
18 } else if let Ok(hf_home) = std::env::var("HF_HOME") {
19 PathBuf::from(hf_home).join("token")
20 } else if let Ok(home) = std::env::var("HOME") {
21 PathBuf::from(home)
22 .join(".cache")
23 .join("huggingface")
24 .join("token")
25 } else {
26 return None;
27 };
28
29 std::fs::read_to_string(&token_path)
30 .ok()
31 .map(|s| s.trim().to_owned())
32 .filter(|s| !s.is_empty())
33}
34
35#[derive(Clone, Serialize, Deserialize)]
37pub struct XetConfig {
38 pub endpoint: String,
47
48 pub token: Option<String>,
55
56 #[cfg(feature = "xet-storage")]
58 #[serde(skip)]
59 pub compression: Option<cas_object::CompressionScheme>,
60
61 #[cfg(not(feature = "xet-storage"))]
62 #[serde(skip)]
63 pub compression: Option<()>,
64}
65
66impl std::fmt::Debug for XetConfig {
67 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68 f.debug_struct("XetConfig")
69 .field("endpoint", &self.endpoint)
70 .field("token", &self.token.as_ref().map(|_| "[REDACTED]"))
71 .field("compression", &self.compression)
72 .finish()
73 }
74}
75
76impl Default for XetConfig {
77 fn default() -> Self {
78 let endpoint = std::env::var("GIT2DB_XET_ENDPOINT")
80 .or_else(|_| std::env::var("XETHUB_ENDPOINT"))
81 .unwrap_or_default();
82
83 let token = std::env::var("XETHUB_TOKEN")
88 .or_else(|_| std::env::var("HF_TOKEN"))
89 .ok()
90 .or_else(read_huggingface_token_file);
91
92 Self {
93 endpoint,
94 token,
95 compression: None,
96 }
97 }
98}
99
100impl XetConfig {
101 pub fn new(endpoint: impl Into<String>) -> Self {
103 Self {
104 endpoint: endpoint.into(),
105 ..Default::default()
106 }
107 }
108
109 pub fn huggingface() -> Self {
114 Self {
115 endpoint: HUGGINGFACE_XET_ENDPOINT.to_owned(),
116 ..Default::default()
117 }
118 }
119
120 pub fn is_huggingface(&self) -> bool {
122 self.endpoint.contains("xethub.hf.co")
123 }
124
125 pub fn is_enabled(&self) -> bool {
127 !self.endpoint.is_empty()
128 }
129
130 pub fn with_token(mut self, token: impl Into<String>) -> Self {
132 self.token = Some(token.into());
133 self
134 }
135
136 #[cfg(feature = "xet-storage")]
138 pub fn with_compression(mut self, compression: cas_object::CompressionScheme) -> Self {
139 self.compression = Some(compression);
140 self
141 }
142
143 pub fn for_url(url: &str) -> Option<Self> {
168 let default = Self::default();
170 if default.is_enabled() {
171 return Some(default);
172 }
173
174 if url.contains("huggingface.co") || url.contains("hf.co") {
176 return Some(Self::huggingface());
177 }
178 None
182 }
183
184 pub fn is_ssh_transport(&self) -> bool {
186 self.endpoint.starts_with("ssh://")
187 }
188
189 pub fn is_https_transport(&self) -> bool {
191 self.endpoint.starts_with("https://") || self.endpoint.starts_with("http://")
192 }
193
194 #[cfg(feature = "xet-storage")]
206 pub async fn create_storage(
207 &self,
208 ) -> crate::error::Result<Box<dyn crate::storage::StorageBackend>> {
209 #[cfg(feature = "gittorrent-transport")]
210 if self.endpoint.starts_with("gittorrent://") {
211 let gt_config = hyprstream_p2p::service::GitTorrentConfig::load().map_err(|e| {
213 crate::error::XetError::new(
214 crate::error::XetErrorKind::RuntimeError,
215 format!("Failed to load gittorrent config: {e}"),
216 )
217 })?;
218 let gt_service = std::sync::Arc::new(
219 hyprstream_p2p::service::GitTorrentService::new(gt_config)
220 .await
221 .map_err(|e| {
222 crate::error::XetError::new(
223 crate::error::XetErrorKind::RuntimeError,
224 format!("Failed to start gittorrent: {e}"),
225 )
226 })?,
227 );
228 let https_endpoint = format!(
230 "https://{}",
231 self.endpoint
232 .strip_prefix("gittorrent://")
233 .unwrap_or(&self.endpoint)
234 );
235 let fallback_config = Self {
236 endpoint: https_endpoint,
237 ..self.clone()
238 };
239 let fallback = match crate::storage::XetStorage::new(&fallback_config).await {
240 Ok(s) => Some(Box::new(s) as Box<dyn crate::storage::StorageBackend>),
241 Err(e) => {
242 tracing::warn!("Failed to initialize HTTPS fallback for gittorrent: {e}");
243 None
244 }
245 };
246 return Ok(Box::new(crate::gittorrent_storage::GittorrentStorage::new(
247 gt_service, fallback,
248 )));
249 }
250
251 #[cfg(not(feature = "gittorrent-transport"))]
252 if self.endpoint.starts_with("gittorrent://") {
253 return Err(crate::error::XetError::new(
254 crate::error::XetErrorKind::InvalidConfig,
255 "Gittorrent transport requires the 'gittorrent-transport' feature",
256 ));
257 }
258
259 #[cfg(feature = "ssh-transport")]
260 if self.is_ssh_transport() {
261 let storage = crate::ssh_client::SshStorage::connect(&self.endpoint).await?;
262 return Ok(Box::new(storage));
263 }
264
265 #[cfg(not(feature = "ssh-transport"))]
266 if self.is_ssh_transport() {
267 return Err(crate::error::XetError::new(
268 crate::error::XetErrorKind::InvalidConfig,
269 "SSH transport requires the 'ssh-transport' feature",
270 ));
271 }
272
273 let storage = crate::storage::XetStorage::new(self).await?;
275 Ok(Box::new(storage))
276 }
277}
278
279#[cfg(test)]
281mod tests {
282 use super::*;
283 use parking_lot::Mutex;
284
285 static ENV_MUTEX: Mutex<()> = Mutex::new(());
287
288 fn clear_xet_env_vars() {
290 std::env::remove_var("GIT2DB_XET_ENDPOINT");
291 std::env::remove_var("XETHUB_ENDPOINT");
292 }
293
294 #[test]
295 fn test_default_config_disabled_without_env() {
296 let _guard = ENV_MUTEX.lock();
297 clear_xet_env_vars();
298
299 let config = XetConfig::default();
300 assert!(!config.is_enabled());
302 }
303
304 #[test]
305 fn test_config_builder() {
306 let config = XetConfig::new("https://custom.endpoint.dev").with_token("test_token");
308
309 assert_eq!(config.endpoint, "https://custom.endpoint.dev");
310 assert_eq!(config.token, Some("test_token".to_owned()));
311 assert!(config.is_enabled());
312 }
313
314 #[test]
315 fn test_huggingface_config() {
316 let config = XetConfig::huggingface();
318
319 assert_eq!(config.endpoint, HUGGINGFACE_XET_ENDPOINT);
320 assert!(config.is_huggingface());
321 assert!(config.is_enabled());
322 }
323
324 #[test]
325 fn test_for_url_huggingface() {
326 let _guard = ENV_MUTEX.lock();
327 clear_xet_env_vars();
328
329 let config = XetConfig::for_url("https://huggingface.co/Qwen/Qwen3-0.6B");
331 assert!(config.is_some());
332 if let Some(cfg) = config {
333 assert!(cfg.is_huggingface());
334 }
335
336 let config = XetConfig::for_url("https://hf.co/Qwen/Qwen3-0.6B");
338 assert!(config.is_some());
339 if let Some(cfg) = config {
340 assert!(cfg.is_huggingface());
341 }
342 }
343
344 #[test]
345 fn test_for_url_unknown() {
346 let _guard = ENV_MUTEX.lock();
347 clear_xet_env_vars();
348
349 let config = XetConfig::for_url("https://github.com/some/repo");
351 assert!(config.is_none());
352 }
353
354 #[test]
355 fn test_for_url_env_override() {
356 let _guard = ENV_MUTEX.lock();
357 clear_xet_env_vars();
358
359 std::env::set_var("XETHUB_ENDPOINT", "https://custom.xet.server");
361
362 let config = XetConfig::for_url("https://github.com/any/repo");
363 assert!(config.is_some());
364 if let Some(cfg) = config {
365 assert_eq!(cfg.endpoint, "https://custom.xet.server");
366 }
367
368 std::env::remove_var("XETHUB_ENDPOINT");
370 }
371
372 #[test]
373 fn test_for_url_with_trailing_slash() {
374 let _guard = ENV_MUTEX.lock();
375 clear_xet_env_vars();
376
377 let config = XetConfig::for_url("https://huggingface.co/Qwen/Model/");
379 assert!(config.is_some());
380 if let Some(cfg) = config {
381 assert!(cfg.is_huggingface());
382 }
383 }
384
385 #[test]
386 fn test_debug_redacts_token() {
387 let config = XetConfig::new("https://example.com").with_token("secret_token_12345");
389
390 let debug_str = format!("{config:?}");
391
392 assert!(debug_str.contains("[REDACTED]"));
394 assert!(!debug_str.contains("secret_token_12345"));
395 assert!(debug_str.contains("https://example.com"));
397 }
398
399 #[test]
400 fn test_debug_without_token() {
401 let config = XetConfig::new("https://example.com");
403
404 let debug_str = format!("{config:?}");
405
406 assert!(debug_str.contains("None"));
408 assert!(debug_str.contains("https://example.com"));
409 }
410}