summaryrefslogtreecommitdiffstats
path: root/src/usecase/detect.rs
blob: 42f98d9d455d98ea2d9b89bf69ca913df2d1d6b4 (plain)
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
//! Checking whether an upstream moved — one pkgbase, or every active one.

use crate::config::Config;
use crate::db::{self, Db, SubscriptionState};
use crate::error::Result;
use crate::watch::detect::{self, Outcome};

/// One pkgbase's detection result. Per-item failures are returned, not raised,
/// so a sweep does not stop at the first unreachable mirror.
pub struct Checked {
    pub pkgbase: String,
    pub outcome: Result<Outcome>,
}

/// Detect one pkgbase, or every active subscription if none was named.
pub fn detect(cfg: &Config, db: &Db, pkgbase: Option<String>) -> Result<Vec<Checked>> {
    let names: Vec<String> = match pkgbase {
        Some(n) => vec![n],
        None => db::list_subscriptions(db)?
            .into_iter()
            .filter(|s| s.state == SubscriptionState::Active)
            .map(|s| s.pkgbase)
            .collect(),
    };
    Ok(names
        .into_iter()
        .map(|n| {
            let outcome = check(cfg, db, &n);
            Checked { pkgbase: n, outcome }
        })
        .collect())
}

/// Detect a single pkgbase and discard the report beyond its outcome.
pub fn check(cfg: &Config, db: &Db, pkgbase: &str) -> Result<Outcome> {
    detect::check_pkgbase(cfg, db, pkgbase).map(|r| r.outcome)
}