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
//! Lintian-brush configuration file.
use crate::Certainty;
use breezyshim::tree::WorkingTree;
use configparser::ini::Ini;
use log::warn;

const SUPPORTED_KEYS: &[&str] = &[
    "compat-release",
    "minimum-certainty",
    "allow-reformatting",
    "update-changelog",
];

pub const PACKAGE_CONFIG_FILENAME: &str = "debian/lintian-brush.conf";

pub struct Config {
    obj: Ini,
}

impl Config {
    pub fn from_workingtree(
        tree: &WorkingTree,
        subpath: &std::path::Path,
    ) -> std::io::Result<Self> {
        let path = tree
            .abspath(&subpath.join(PACKAGE_CONFIG_FILENAME))
            .unwrap();
        Self::load_from_path(&path)
    }

    pub fn load_from_path(path: &std::path::Path) -> Result<Self, std::io::Error> {
        let mut ini = Ini::new();
        let data = std::fs::read_to_string(path)?;
        ini.read(data)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;

        for (section, contents) in ini.get_map_ref() {
            if section != "default" {
                warn!(
                    "unknown section {} in {}, ignoring.",
                    section,
                    path.display()
                );
                continue;
            }
            for key in contents.keys() {
                if !SUPPORTED_KEYS.contains(&key.as_str()) {
                    warn!(
                        "unknown key {} in section {} in {}, ignoring.",
                        key,
                        section,
                        path.display()
                    );

                    continue;
                }
            }
        }

        Ok(Config { obj: ini })
    }

    pub fn compat_release(&self) -> Option<String> {
        if let Some(value) = self.obj.get("default", "compat-release") {
            let codename = crate::release_info::resolve_release_codename(value.as_str(), None);
            if codename.is_none() {
                warn!("unknown compat release {}, ignoring.", value);
            }
            codename
        } else {
            None
        }
    }

    pub fn allow_reformatting(&self) -> Option<bool> {
        match self.obj.getbool("default", "allow-reformatting") {
            Ok(value) => value,
            Err(e) => {
                warn!("invalid allow-reformatting value {}, ignoring.", e);
                None
            }
        }
    }

    pub fn minimum_certainty(&self) -> Option<Certainty> {
        self.obj
            .get("default", "minimum-certainty")
            .and_then(|value| {
                value
                    .parse::<Certainty>()
                    .map_err(|e| {
                        warn!("invalid minimum-certainty value {}, ignoring.", value);
                        e
                    })
                    .ok()
            })
    }

    pub fn update_changelog(&self) -> Option<bool> {
        match self.obj.getbool("default", "update-changelog") {
            Ok(value) => value,
            Err(e) => {
                warn!("invalid update-changelog value {}, ignoring.", e);
                None
            }
        }
    }
}