assorted_debian_utils/
autoremovals.rs

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
// Copyright 2022 Sebastian Ramacher
// SPDX-License-Identifier: LGPL-3.0-or-later

//! # Helpers to handle `autoremovals.yaml`
//!
//! This module provides helpers to deserialize [autoremovals.yaml](https://udd.debian.org/cgi-bin/autoremovals.yaml.cgi)
//! with [serde].

use std::{collections::HashMap, io};

use chrono::{DateTime, Utc};
use serde::Deserialize;

use crate::{utils::DateTimeVisitor, version::PackageVersion};

/// Deserialize a datetime string into a `DateTime<Utc>`
fn deserialize_datetime<'de, D>(deserializer: D) -> std::result::Result<DateTime<Utc>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    deserializer.deserialize_str(DateTimeVisitor("%Y-%m-%d %H:%M:%S"))
}

/// All autoremovals
pub type AutoRemovals = HashMap<String, AutoRemoval>;

/// An autoremoval
#[derive(Debug, Deserialize, PartialEq, Eq)]
pub struct AutoRemoval {
    /// The package's RC bugs causing auto-removal.
    pub bugs: Vec<String>,
    /// The RC bugs of dependencies causing auto-removal.
    pub bugs_dependencies: Option<Vec<String>>,
    /// List of RC-buggy dependencies causing auto-removal.
    pub buggy_dependencies: Option<Vec<String>>,
    /// Auto-removal is caused by dependencies.
    pub dependencies_only: bool,
    /// Date of the last check.
    #[serde(deserialize_with = "deserialize_datetime")]
    pub last_checked: DateTime<Utc>,
    /// Auto-removal date.
    #[serde(deserialize_with = "deserialize_datetime")]
    pub removal_date: DateTime<Utc>,
    /// Source package name.
    pub source: String,
    /// Package version.
    pub version: PackageVersion,
    /// List of reverse dependencies that will also be auto-removed.
    pub rdeps: Option<Vec<String>>,
}

/// Result type
pub type Result<T> = serde_yaml::Result<T>;

/// Read autoremovals from a reader
pub fn from_reader(reader: impl io::Read) -> Result<AutoRemovals> {
    serde_yaml::from_reader(reader)
}

/// Read autoremovals from a string
pub fn from_str(data: &str) -> Result<AutoRemovals> {
    serde_yaml::from_str(data)
}

#[cfg(test)]
mod test {
    use super::from_str;

    #[test]
    fn base() {
        let data = r"---
mplayer:
  bugs:
  - '1005899'
  dependencies_only: false
  last_checked: 2022-04-10 17:55:40
  rdeps:
  - devede
  - diffoscope
  - dradio
  - mplayer-blue
  - ogmrip
  - qwinff
  - reprotest
  - vdr-plugin-mp3
  - videotrans
  removal_date: 2022-05-01 19:42:01
  source: mplayer
  version: 2:1.4+ds1-3
mplayer-blue:
  buggy_dependencies:
  - mplayer
  bugs: []
  bugs_dependencies:
  - '1005899'
  dependencies_only: true
  last_checked: 2022-04-10 17:55:40
  removal_date: 2022-05-01 19:42:01
  source: mplayer-blue
  version: 1.13-2
";
        let autoremovals = from_str(data).unwrap();

        assert!(autoremovals.contains_key("mplayer"));
        let mplayer = autoremovals.get("mplayer").unwrap();
        assert_eq!(mplayer.source, "mplayer");
        assert_eq!(mplayer.version.to_string(), "2:1.4+ds1-3");
        assert_eq!(mplayer.bugs, vec!["1005899"]);
        assert_eq!(
            mplayer.rdeps.as_ref().unwrap(),
            &vec![
                "devede",
                "diffoscope",
                "dradio",
                "mplayer-blue",
                "ogmrip",
                "qwinff",
                "reprotest",
                "vdr-plugin-mp3",
                "videotrans"
            ]
        );
        assert!(!mplayer.dependencies_only);

        assert!(autoremovals.contains_key("mplayer-blue"));
        let mplayer_blue = autoremovals.get("mplayer-blue").unwrap();
        assert_eq!(mplayer_blue.source, "mplayer-blue");
        assert_eq!(mplayer_blue.version.to_string(), "1.13-2");
        assert_eq!(mplayer_blue.bugs.len(), 0);
        assert_eq!(
            mplayer_blue.buggy_dependencies.as_ref().unwrap(),
            &vec!["mplayer"]
        );
        assert_eq!(
            mplayer_blue.bugs_dependencies.as_ref().unwrap(),
            &vec!["1005899"]
        );
        assert!(mplayer_blue.dependencies_only);
    }
}