cargo-bless 0.3.1

Modernize your Rust dependencies with blessed.rs + live intel
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Live intelligence layer — fetches metadata from crates.io and GitHub
//! to assess freshness, popularity, and maintenance status.
//!
//! All network operations are **non-fatal**: failures are logged and the
//! tool continues with whatever data it has.

use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{Context, Result};
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};

const USER_AGENT: &str = concat!(
    "cargo-bless/",
    env!("CARGO_PKG_VERSION"),
    " (https://github.com/Ruffian-L/cargo-bless)"
);
const CACHE_TTL_SECS: u64 = 3600; // 1 hour

// ── Public types ─────────────────────────────────────────────────────

/// Live metadata for a single crate.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrateIntel {
    pub name: String,
    pub latest_version: String,
    pub downloads: u64,
    pub recent_downloads: Option<u64>,
    pub last_updated: String,
    pub repository_url: Option<String>,
    pub description: Option<String>,
}

/// GitHub repository activity summary.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubActivity {
    pub last_push: String,
    pub stars: u64,
    pub is_archived: bool,
    pub open_issues: u64,
}

/// Cache wrapper that tracks when data was fetched.
#[derive(Debug, Serialize, Deserialize)]
struct CacheEntry<T> {
    data: T,
    fetched_at: u64,
}

impl<T> CacheEntry<T> {
    fn is_fresh(&self) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        now.saturating_sub(self.fetched_at) < CACHE_TTL_SECS
    }

    fn new(data: T) -> Self {
        let fetched_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        Self { data, fetched_at }
    }
}

// ── IntelClient ──────────────────────────────────────────────────────

/// Client for fetching live dependency intelligence.
pub struct IntelClient {
    client: crates_io_api::SyncClient,
    http: reqwest::blocking::Client,
    cache_dir: Option<PathBuf>,
}

impl IntelClient {
    /// Create a new IntelClient with crates.io API access and disk cache.
    pub fn new() -> Result<Self> {
        let client = crates_io_api::SyncClient::new(USER_AGENT, Duration::from_secs(1))
            .context("failed to create crates.io client")?;
        let http = reqwest::blocking::Client::builder()
            .user_agent(USER_AGENT)
            .timeout(Duration::from_secs(10))
            .build()
            .context("failed to create GitHub HTTP client")?;

        let mut cache_dir = ProjectDirs::from("rs", "", "cargo-bless")
            .map(|dirs| dirs.cache_dir().to_path_buf())
            .filter(|path| !path.as_os_str().is_empty());

        if let Some(dir) = &cache_dir {
            if let Err(err) = fs::create_dir_all(dir) {
                eprintln!(
                    "⚠️  Could not create cache directory at {}: {}. Continuing without cache.",
                    dir.display(),
                    err
                );
                cache_dir = None;
            }
        }

        Ok(Self {
            client,
            http,
            cache_dir,
        })
    }

    /// Fetch live intel for a crate. Checks disk cache first (1hr TTL).
    pub fn fetch_crate_intel(&self, name: &str) -> Result<CrateIntel> {
        // Check cache
        let cache_path = self
            .cache_dir
            .as_ref()
            .map(|dir| dir.join(format!("{}.json", name)));

        if let Some(path) = &cache_path {
            if let Ok(contents) = fs::read_to_string(path) {
                if let Ok(entry) = serde_json::from_str::<CacheEntry<CrateIntel>>(&contents) {
                    if entry.is_fresh() {
                        return Ok(entry.data);
                    }
                }
            }
        }

        // Cache miss or stale — fetch from crates.io
        let response = self
            .client
            .get_crate(name)
            .with_context(|| format!("failed to fetch crate info for '{}'", name))?;

        let crate_data = &response.crate_data;
        let latest_version = response
            .versions
            .first()
            .map(|v| v.num.clone())
            .unwrap_or_else(|| crate_data.max_version.clone());

        let intel = CrateIntel {
            name: name.to_string(),
            latest_version,
            downloads: crate_data.downloads,
            recent_downloads: crate_data.recent_downloads,
            last_updated: crate_data.updated_at.to_string(),
            repository_url: crate_data.repository.clone(),
            description: crate_data.description.clone(),
        };

        // Write to cache (best-effort)
        if let Some(path) = &cache_path {
            let entry = CacheEntry::new(intel.clone());
            if let Ok(json) = serde_json::to_string_pretty(&entry) {
                let _ = fs::write(path, json);
            }
        }

        Ok(intel)
    }

    /// Fetch GitHub activity for a repository URL.
    /// Returns None if the URL is not a GitHub URL or if the fetch fails.
    pub fn fetch_github_activity(&self, repo_url: &str) -> Option<GitHubActivity> {
        let (owner, repo) = parse_github_url(repo_url)?;

        let url = format!("https://api.github.com/repos/{owner}/{repo}");
        let repo_info = self
            .http
            .get(url)
            .send()
            .ok()?
            .error_for_status()
            .ok()?
            .json::<GitHubRepoResponse>()
            .ok()?;

        Some(GitHubActivity {
            last_push: repo_info.pushed_at.unwrap_or_else(|| "unknown".into()),
            stars: repo_info.stargazers_count.unwrap_or(0),
            is_archived: repo_info.archived.unwrap_or(false),
            open_issues: repo_info.open_issues_count.unwrap_or(0),
        })
    }

    /// Fetch intel for all unique crate names, returning what we can get.
    /// Failures for individual crates are silently skipped.
    pub fn fetch_bulk_intel(&self, crate_names: &[&str]) -> HashMap<String, CrateIntel> {
        let mut intel = HashMap::new();
        for name in crate_names {
            match self.fetch_crate_intel(name) {
                Ok(info) => {
                    intel.insert(name.to_string(), info);
                }
                Err(_) => {
                    // Non-fatal: skip this crate
                }
            }
        }
        intel
    }
}

#[derive(Debug, Deserialize)]
struct GitHubRepoResponse {
    pushed_at: Option<String>,
    stargazers_count: Option<u64>,
    archived: Option<bool>,
    open_issues_count: Option<u64>,
}

// ── Helpers ──────────────────────────────────────────────────────────

/// Parse a GitHub URL into (owner, repo).
/// Supports: https://github.com/owner/repo, https://github.com/owner/repo.git,
/// https://github.com/owner/repo/tree/main, etc.
pub fn parse_github_url(url: &str) -> Option<(String, String)> {
    let url = url.trim().trim_end_matches('/');

    // Find the github.com part
    let after_github = if let Some(pos) = url.find("github.com/") {
        &url[pos + "github.com/".len()..]
    } else {
        return None;
    };

    let parts: Vec<&str> = after_github.splitn(3, '/').collect();
    if parts.len() < 2 {
        return None;
    }

    let owner = parts[0].to_string();
    let repo = parts[1].trim_end_matches(".git").to_string();

    if owner.is_empty() || repo.is_empty() {
        return None;
    }

    Some((owner, repo))
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_parse_github_url_basic() {
        let result = parse_github_url("https://github.com/serde-rs/serde");
        assert_eq!(result, Some(("serde-rs".into(), "serde".into())));
    }

    #[test]
    fn test_parse_github_url_with_git_suffix() {
        let result = parse_github_url("https://github.com/tokio-rs/tokio.git");
        assert_eq!(result, Some(("tokio-rs".into(), "tokio".into())));
    }

    #[test]
    fn test_parse_github_url_with_path() {
        let result = parse_github_url("https://github.com/dtolnay/anyhow/tree/main");
        assert_eq!(result, Some(("dtolnay".into(), "anyhow".into())));
    }

    #[test]
    fn test_parse_github_url_trailing_slash() {
        let result = parse_github_url("https://github.com/clap-rs/clap/");
        assert_eq!(result, Some(("clap-rs".into(), "clap".into())));
    }

    #[test]
    fn test_parse_github_url_not_github() {
        assert!(parse_github_url("https://gitlab.com/foo/bar").is_none());
        assert!(parse_github_url("https://crates.io/crates/serde").is_none());
    }

    #[test]
    fn test_parse_github_url_too_short() {
        assert!(parse_github_url("https://github.com/just-user").is_none());
        assert!(parse_github_url("https://github.com/").is_none());
    }

    #[test]
    fn test_cache_entry_fresh() {
        let entry = CacheEntry::new("some data".to_string());
        assert!(entry.is_fresh());
    }

    #[test]
    fn test_cache_entry_stale() {
        let entry = CacheEntry {
            data: "old data".to_string(),
            fetched_at: 0, // epoch = definitely stale
        };
        assert!(!entry.is_fresh());
    }

    #[test]
    fn test_cache_entry_roundtrip() {
        let intel = CrateIntel {
            name: "serde".into(),
            latest_version: "1.0.228".into(),
            downloads: 100_000_000,
            recent_downloads: Some(5_000_000),
            last_updated: "2026-01-15T12:00:00Z".into(),
            repository_url: Some("https://github.com/serde-rs/serde".into()),
            description: Some("A serialization framework".into()),
        };
        let entry = CacheEntry::new(intel);
        let json = serde_json::to_string(&entry).unwrap();
        let roundtrip: CacheEntry<CrateIntel> = serde_json::from_str(&json).unwrap();
        assert_eq!(roundtrip.data.name, "serde");
        assert_eq!(roundtrip.data.downloads, 100_000_000);
        assert!(roundtrip.is_fresh());
    }

    #[test]
    fn test_cache_disk_write_and_read() {
        let tmp = TempDir::new().unwrap();
        let cache_path = tmp.path().join("test_crate.json");

        let intel = CrateIntel {
            name: "test_crate".into(),
            latest_version: "0.1.0".into(),
            downloads: 42,
            recent_downloads: None,
            last_updated: "2026-02-27T00:00:00Z".into(),
            repository_url: None,
            description: None,
        };

        // Write
        let entry = CacheEntry::new(intel);
        let json = serde_json::to_string_pretty(&entry).unwrap();
        fs::write(&cache_path, &json).unwrap();

        // Read back
        let contents = fs::read_to_string(&cache_path).unwrap();
        let loaded: CacheEntry<CrateIntel> = serde_json::from_str(&contents).unwrap();
        assert_eq!(loaded.data.name, "test_crate");
        assert!(loaded.is_fresh());
    }

    #[test]
    fn test_fetch_bulk_intel() {
        let tmp = TempDir::new().unwrap();

        let mut client = IntelClient::new().unwrap();
        client.cache_dir = Some(tmp.path().to_path_buf());

        // Inject successful cache hit
        let intel = CrateIntel {
            name: "test_success".into(),
            latest_version: "1.0.0".into(),
            downloads: 100,
            recent_downloads: None,
            last_updated: "2026-02-27T00:00:00Z".into(),
            repository_url: None,
            description: None,
        };
        let entry = CacheEntry::new(intel.clone());
        let json = serde_json::to_string_pretty(&entry).unwrap();
        fs::write(tmp.path().join("test_success.json"), json).unwrap();

        // Fetch one that succeeds and one that fails (cache miss and fake crate)
        let results = client.fetch_bulk_intel(&["test_success", "test_failure_not_exist_abc123"]);

        // Validate
        assert_eq!(results.len(), 1);
        assert!(results.contains_key("test_success"));
        assert_eq!(results.get("test_success").unwrap().name, "test_success");
    }

    /// Live network test — run with `cargo test -- --ignored`
    #[test]
    #[ignore]
    fn test_live_fetch_serde() {
        let client = IntelClient::new().expect("client should init");
        let intel = client
            .fetch_crate_intel("serde")
            .expect("should fetch serde");
        assert_eq!(intel.name, "serde");
        assert!(intel.downloads > 0);
        println!(
            "serde: v{}, {} downloads",
            intel.latest_version, intel.downloads
        );
    }

    /// Live GitHub test — run with `cargo test -- --ignored`
    #[test]
    #[ignore]
    fn test_live_github_serde() {
        let client = IntelClient::new().expect("client should init");
        let activity = client
            .fetch_github_activity("https://github.com/serde-rs/serde")
            .expect("should get activity");
        assert!(activity.stars > 0);
        println!(
            "serde: {} stars, archived={}",
            activity.stars, activity.is_archived
        );
    }
}