apt_pkg_native/
simple.rs

1//! Some structs representing basic concepts, and utilities to copy out of "iterators".
2
3use std::fmt;
4
5use crate::sane;
6
7#[derive(Clone, Debug)]
8pub struct BinaryPackage {
9    pub name: String,
10    pub arch: String,
11    pub current_version: Option<String>,
12    pub candidate_version: Option<String>,
13}
14
15impl BinaryPackage {
16    pub fn new(view: &sane::PkgView) -> Self {
17        BinaryPackage {
18            name: view.name(),
19            arch: view.arch(),
20            current_version: view.current_version(),
21            candidate_version: view.candidate_version(),
22        }
23    }
24}
25
26impl fmt::Display for BinaryPackage {
27    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28        write!(f, "{}:{}", self.name, self.arch)?;
29        if let Some(ref version) = self.current_version {
30            write!(f, " @ {version}")?;
31        }
32        if let Some(ref version) = self.candidate_version {
33            write!(f, " -> {version}")?;
34        }
35        Ok(())
36    }
37}
38
39#[derive(Clone, Debug)]
40pub struct Version {
41    pub version: String,
42    pub arch: String,
43    pub section: Option<String>,
44
45    #[cfg(not(feature = "ye-olde-apt"))]
46    pub source_package: String,
47    #[cfg(not(feature = "ye-olde-apt"))]
48    pub source_version: String,
49    #[cfg(not(feature = "ye-olde-apt"))]
50    pub priority: i32,
51}
52
53impl Version {
54    pub fn new(view: &sane::VerView) -> Self {
55        Version {
56            version: view.version(),
57            arch: view.arch(),
58            section: view.section(),
59            #[cfg(not(feature = "ye-olde-apt"))]
60            source_package: view.source_package(),
61            #[cfg(not(feature = "ye-olde-apt"))]
62            source_version: view.source_version(),
63            #[cfg(not(feature = "ye-olde-apt"))]
64            priority: view.priority(),
65        }
66    }
67}
68
69impl fmt::Display for Version {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        write!(f, "{}:{}", self.version, self.arch)?;
72        if let Some(ref section) = self.section {
73            write!(f, " in {section}")?;
74        }
75        #[cfg(not(feature = "ye-olde-apt"))]
76        write!(
77            f,
78            " from {}:{} at {}",
79            self.source_package, self.source_version, self.priority,
80        )?;
81
82        Ok(())
83    }
84}
85
86#[derive(Clone, Debug)]
87pub struct Origin {
88    pub file_name: String,
89    pub archive: String,
90    pub version: Option<String>,
91    pub origin: Option<String>,
92    pub codename: Option<String>,
93    pub label: Option<String>,
94    pub site: Option<String>,
95    pub component: String,
96    pub architecture: Option<String>,
97    pub index_type: String,
98}
99
100impl Origin {
101    pub fn from_ver_file(view: &sane::VerFileView) -> Option<Self> {
102        view.file().next().map(|x| Self::new(&x))
103    }
104
105    pub fn new(view: &sane::PkgFileView) -> Self {
106        Origin {
107            file_name: view.file_name(),
108            archive: view.archive(),
109            version: view.version(),
110            origin: view.origin(),
111            codename: view.codename(),
112            label: view.label(),
113            site: view.site(),
114            component: view.component(),
115            architecture: view.architecture(),
116            index_type: view.index_type(),
117        }
118    }
119}
120
121impl fmt::Display for Origin {
122    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
123        // trying to simulate apt-cache policy, but a lot of information is missing
124        if self.site.is_some()
125            && self.origin.is_some()
126            && self.label.is_some()
127            && self.codename.is_some()
128            && self.architecture.is_some()
129        {
130            write!(
131                f,
132                "TODO://{}/TODO(o:{}/l:{}/c:{}) {}/{} {} (f:{})",
133                self.site.as_ref().unwrap(),
134                self.origin.as_ref().unwrap(),
135                self.label.as_ref().unwrap(),
136                self.codename.as_ref().unwrap(),
137                self.archive,
138                self.component,
139                self.architecture.as_ref().unwrap(),
140                self.file_name
141            )
142        } else {
143            write!(f, "{}", self.file_name)
144        }
145    }
146}
147
148#[derive(Clone, Debug)]
149pub struct VersionOrigins {
150    pub version: Version,
151    pub origins: Vec<Origin>,
152}
153
154impl VersionOrigins {
155    pub fn new(view: &sane::VerView) -> Self {
156        VersionOrigins {
157            version: Version::new(view),
158            origins: view
159                .origin_iter()
160                .map(|o| {
161                    Origin::from_ver_file(o)
162                        .expect("a version's origin should always have a backing file")
163                })
164                .collect(),
165        }
166    }
167}
168
169#[derive(Clone, Debug)]
170pub struct BinaryPackageVersions {
171    pub pkg: BinaryPackage,
172    pub versions: Vec<Version>,
173}
174
175impl BinaryPackageVersions {
176    pub fn new(view: &sane::PkgView) -> Self {
177        BinaryPackageVersions {
178            pkg: BinaryPackage::new(view),
179            versions: view.versions().map(Version::new).collect(),
180        }
181    }
182}
183
184impl fmt::Display for BinaryPackageVersions {
185    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
186        write!(f, "{} + {} versions", self.pkg, self.versions.len())
187    }
188}