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
//! # svp-client
//!
//! `svp-client` is a library to interact with the [SVP
//! protocol](https://github.com/jelmer/silver-platter/blob/master/codemod-protocol.md), as supported by
//! the `svp` command-line tool.
use std::collections::HashMap;

pub mod debian;

#[derive(Debug, serde::Serialize)]
struct Failure {
    /// The result code, e.g. "header-missing".
    result_code: String,

    /// The versions of the packages involved.
    versions: HashMap<String, String>,

    /// A human-readable description of the failure.
    description: String,

    /// Whether the failure is transient.
    transient: Option<bool>,
}

#[derive(Debug, serde::Serialize)]
struct Success {
    versions: HashMap<String, String>,
    value: Option<i32>,
    context: Option<serde_json::Value>,
    debian: Option<debian::Context>,
}

/// Report a success to the SVP server.
///
/// # Arguments
/// * `versions` - A map of package names to versions.
pub fn report_success(
    versions: HashMap<String, String>,
    value: Option<i32>,
    context: Option<serde_json::Value>,
) {
    if std::env::var("SVP_API").ok().as_deref() == Some("1") {
        let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap();

        serde_json::to_writer(
            f,
            &Success {
                versions,
                value,
                context,
                debian: None,
            },
        )
        .unwrap();
    }
}

pub fn report_nothing_to_do(versions: HashMap<String, String>, description: Option<&str>) -> ! {
    let description = description.unwrap_or("Nothing to do");
    if std::env::var("SVP_API").ok().as_deref() == Some("1") {
        let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap();

        serde_json::to_writer(
            f,
            &Failure {
                result_code: "nothing-to-do".to_string(),
                versions,
                description: description.to_string(),
                transient: None,
            },
        )
        .unwrap();
    }
    log::error!("{}", description);
    std::process::exit(0);
}

pub fn report_fatal(
    versions: HashMap<String, String>,
    code: &str,
    description: &str,
    hint: Option<&str>,
    transient: Option<bool>,
) -> ! {
    if std::env::var("SVP_API").ok().as_deref() == Some("1") {
        let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap();

        serde_json::to_writer(
            f,
            &Failure {
                result_code: code.to_string(),
                versions,
                description: description.to_string(),
                transient,
            },
        )
        .unwrap();
    }
    log::error!("{}", description);
    if let Some(hint) = hint {
        log::info!("{}", hint);
    }
    std::process::exit(1);
}

pub fn load_resume() -> Option<serde_json::Value> {
    if std::env::var("SVP_API").ok().as_deref() == Some("1") {
        if let Ok(resume_path) = std::env::var("SVP_RESUME") {
            let f = std::fs::File::open(resume_path).unwrap();
            let resume: serde_json::Value = serde_json::from_reader(f).unwrap();
            Some(resume)
        } else {
            None
        }
    } else {
        None
    }
}

pub fn enabled() -> bool {
    std::env::var("SVP_API").ok().as_deref() == Some("1")
}