summaryrefslogtreecommitdiffstats
path: root/src/usecase/detect.rs
blob: 67cc83c37bf49d161e898ccdb1b82204d65e2c66 (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
38
39
40
41
//! 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 rather than raised: a sweep over every
/// subscription should not stop at the first pkgbase whose mirror is
/// unreachable, and the caller decides whether a failure is a line on stderr
/// or a row in a table.
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)
}