alpm_srcinfo/source_info/v1/
merged.rs

1//! Provides fully resolved package metadata derived from SRCINFO data.
2use alpm_types::{
3    Architecture,
4    FullVersion,
5    License,
6    MakepkgOption,
7    Name,
8    OpenPGPIdentifier,
9    OptionalDependency,
10    PackageDescription,
11    PackageRelation,
12    RelationOrSoname,
13    RelativePath,
14    SkippableChecksum,
15    Source,
16    Url,
17    digests::{Blake2b512, Md5, Sha1, Sha224, Sha256, Sha384, Sha512},
18};
19use serde::{Deserialize, Serialize};
20
21#[cfg(doc)]
22use crate::source_info::v1::package::Override;
23use crate::{
24    SourceInfoV1,
25    source_info::v1::{
26        package::Package,
27        package_base::{PackageBase, PackageBaseArchitecture},
28    },
29};
30
31/// Fully resolved metadata of a single package based on SRCINFO data.
32///
33/// This struct incorporates all [`PackageBase`] properties and the [`Package`] specific overrides
34/// in an architecture-specific representation of a package. It can be created using
35/// [`SourceInfoV1::packages_for_architecture`].
36#[derive(Clone, Debug, Deserialize, Serialize)]
37pub struct MergedPackage {
38    /// The alpm-package-name for the package.
39    pub name: Name,
40    /// The description for the package.
41    pub description: Option<PackageDescription>,
42    /// The upstream URL for the package.
43    pub url: Option<Url>,
44    /// The list of licenses that apply to the package.
45    pub licenses: Vec<License>,
46    /// The alpm-architecture for the package.
47    pub architecture: Architecture,
48    /// The optional relative path to a changelog file for the package.
49    pub changelog: Option<RelativePath>,
50
51    // Build or package management related meta fields
52    /// The optional relative path to an alpm-install-scriptlet for the package.
53    pub install: Option<RelativePath>,
54    /// The list of alpm-package-groups the package is part of.
55    pub groups: Vec<String>,
56    /// The list of build tool options used when builidng the package.
57    pub options: Vec<MakepkgOption>,
58    /// The list of relative paths to files in the package that should be backed up.
59    pub backups: Vec<RelativePath>,
60
61    /// The full version of the package.
62    pub version: FullVersion,
63    /// The list of OpenPGP fingerprints of OpenPGP certificates used for the verification of
64    /// upstream sources.
65    pub pgp_fingerprints: Vec<OpenPGPIdentifier>,
66
67    /// The list of run-time dependencies.
68    pub dependencies: Vec<RelationOrSoname>,
69    /// The list of optional dependencies.
70    pub optional_dependencies: Vec<OptionalDependency>,
71    /// The list of provisions.
72    pub provides: Vec<RelationOrSoname>,
73    /// The list of conflicts.
74    pub conflicts: Vec<PackageRelation>,
75    /// The list of replacements.
76    pub replaces: Vec<PackageRelation>,
77    /// The list of test dependencies.
78    pub check_dependencies: Vec<PackageRelation>,
79    /// The list of build dependencies.
80    pub make_dependencies: Vec<PackageRelation>,
81
82    /// The list of sources for the package.
83    pub sources: Vec<MergedSource>,
84    /// The list of sources for the package that are not extracted.
85    pub no_extracts: Vec<String>,
86}
87
88/// An iterator over all packages of a specific architecture.
89#[derive(Clone, Debug)]
90pub struct MergedPackagesIterator<'a> {
91    pub(crate) architecture: Architecture,
92    pub(crate) source_info: &'a SourceInfoV1,
93    pub(crate) package_iterator: std::slice::Iter<'a, Package>,
94}
95
96impl Iterator for MergedPackagesIterator<'_> {
97    type Item = MergedPackage;
98
99    fn next(&mut self) -> Option<MergedPackage> {
100        // Search for the next package that is valid for the the architecture we're looping over.
101        let package = self.package_iterator.find(|package| {
102            // If the package provides target architecture overrides, use those, otherwise fallback
103            // to package base architectures.
104            let architectures = match &package.architectures {
105                Some(value) => value,
106                None => &self.source_info.base.architectures,
107            };
108
109            architectures.contains(&self.architecture) || architectures.contains(&Architecture::Any)
110        })?;
111
112        Some(MergedPackage::from_base_and_package(
113            self.architecture,
114            &self.source_info.base,
115            package,
116        ))
117    }
118}
119
120/// A merged representation of source related information.
121///
122/// SRCINFO provides this info as separate lists. This struct resolves that list representation and
123/// provides a convenient aggregated representation for a single source.
124#[derive(Clone, Debug, Deserialize, Serialize)]
125pub struct MergedSource {
126    /// The source.
127    pub source: Source,
128    /// The optional Blake2 hash digest of `source`.
129    pub b2_checksum: Option<SkippableChecksum<Blake2b512>>,
130    /// The optional MD-5 hash digest of `source`.
131    pub md5_checksum: Option<SkippableChecksum<Md5>>,
132    /// The optional SHA-1 hash digest of `source`.
133    pub sha1_checksum: Option<SkippableChecksum<Sha1>>,
134    /// The optional SHA-224 hash digest of `source`.
135    pub sha224_checksum: Option<SkippableChecksum<Sha224>>,
136    /// The optional SHA-256 hash digest of `source`.
137    pub sha256_checksum: Option<SkippableChecksum<Sha256>>,
138    /// The optional SHA-384 hash digest of `source`.
139    pub sha384_checksum: Option<SkippableChecksum<Sha384>>,
140    /// The optional SHA-512 hash digest of `source`.
141    pub sha512_checksum: Option<SkippableChecksum<Sha512>>,
142}
143
144/// A convenience iterator to build a list of [`MergedSource`] from the disjoint vectors of sources
145/// and digests.
146///
147/// The checksums and sources are by convention all in the same order, which makes this quite
148/// convenient to convert into a aggregated struct representation.
149#[derive(Clone, Debug)]
150pub struct MergedSourceIterator<'a> {
151    sources: std::slice::Iter<'a, Source>,
152    b2_checksums: std::slice::Iter<'a, SkippableChecksum<Blake2b512>>,
153    md5_checksums: std::slice::Iter<'a, SkippableChecksum<Md5>>,
154    sha1_checksums: std::slice::Iter<'a, SkippableChecksum<Sha1>>,
155    sha224_checksums: std::slice::Iter<'a, SkippableChecksum<Sha224>>,
156    sha256_checksums: std::slice::Iter<'a, SkippableChecksum<Sha256>>,
157    sha384_checksums: std::slice::Iter<'a, SkippableChecksum<Sha384>>,
158    sha512_checksums: std::slice::Iter<'a, SkippableChecksum<Sha512>>,
159}
160
161impl Iterator for MergedSourceIterator<'_> {
162    type Item = MergedSource;
163
164    fn next(&mut self) -> Option<MergedSource> {
165        let source = self.sources.next()?;
166
167        Some(MergedSource {
168            source: source.clone(),
169            b2_checksum: self.b2_checksums.next().cloned(),
170            md5_checksum: self.md5_checksums.next().cloned(),
171            sha1_checksum: self.sha1_checksums.next().cloned(),
172            sha224_checksum: self.sha224_checksums.next().cloned(),
173            sha256_checksum: self.sha256_checksums.next().cloned(),
174            sha384_checksum: self.sha384_checksums.next().cloned(),
175            sha512_checksum: self.sha512_checksums.next().cloned(),
176        })
177    }
178}
179
180impl MergedPackage {
181    /// Creates the fully resolved, architecture-specific metadata representation of a package.
182    ///
183    /// Takes an [`Architecture`] (which defines the architecture for which to create the
184    /// representation), as well as a [`PackageBase`] and a [`Package`] (from which to derive the
185    /// metadata).
186    ///
187    /// The metadata representation is created using the following steps:
188    /// 1. [`MergedPackage::from_base`] is called to create a basic representation of a
189    ///    [`MergedPackage`] based on the default values in [`PackageBase`].
190    /// 2. [`MergedPackage::merge_package`] is called to merge all architecture-agnostic fields of
191    ///    the [`Package`] into the [`MergedPackage`].
192    /// 3. The architecture-specific properties of the [`PackageBase`] and [`Package`] are
193    ///    extracted.
194    /// 4. [`PackageBaseArchitecture::merge_package_properties`] is called to merge the
195    ///    architecture-specific properties of the [`Package`] into those of the [`PackageBase`].
196    /// 5. [`MergedPackage::merge_architecture_properties`] is called to merge the combined
197    ///    architecture-specific properties into the [`MergedPackage`].
198    pub fn from_base_and_package(
199        architecture: Architecture,
200        base: &PackageBase,
201        package: &Package,
202    ) -> MergedPackage {
203        let name = package.name.clone();
204        // Step 1
205        let mut merged_package = Self::from_base(&architecture, name, base);
206
207        // Step 2
208        merged_package.merge_package(package);
209
210        // Get the architecture specific properties from the PackageBase.
211        // Use an empty default without any properties as default if none are found.
212        let mut architecture_properties =
213            if let Some(properties) = base.architecture_properties.get(&architecture) {
214                properties.clone()
215            } else {
216                PackageBaseArchitecture::default()
217            };
218
219        // Apply package specific overrides for architecture specific properties.
220        if let Some(package_properties) = package.architecture_properties.get(&architecture) {
221            architecture_properties.merge_package_properties(package_properties.clone());
222        }
223
224        // Merge the architecture specific properties into the final MergedPackage.
225        merged_package.merge_architecture_properties(&architecture_properties);
226
227        merged_package
228    }
229
230    /// Creates a basic, architecture-specific, but incomplete [`MergedPackage`].
231    ///
232    /// Takes an [`Architecture`] (which defines the architecture for which to create the
233    /// representation), a [`Name`] which defines the name of the package and a [`PackageBase`]
234    /// which provides the initial data.
235    ///
236    /// # Note
237    ///
238    /// The returned [`MergedPackage`] is not complete, as it neither contains package-specific nor
239    /// architecture-specific overrides for its fields.
240    /// Use [`from_base_and_package`](MergedPackage::from_base_and_package) to create a fully
241    /// resolved representation of a package.
242    pub fn from_base(architecture: &Architecture, name: Name, base: &PackageBase) -> MergedPackage {
243        // Merge all source related info into aggregated structs.
244        let merged_sources = MergedSourceIterator {
245            sources: base.sources.iter(),
246            b2_checksums: base.b2_checksums.iter(),
247            md5_checksums: base.md5_checksums.iter(),
248            sha1_checksums: base.sha1_checksums.iter(),
249            sha224_checksums: base.sha224_checksums.iter(),
250            sha256_checksums: base.sha256_checksums.iter(),
251            sha384_checksums: base.sha384_checksums.iter(),
252            sha512_checksums: base.sha512_checksums.iter(),
253        };
254
255        MergedPackage {
256            name,
257            description: base.description.clone(),
258            url: base.url.clone(),
259            licenses: base.licenses.clone(),
260            architecture: *architecture,
261            changelog: base.changelog.clone(),
262            install: base.install.clone(),
263            groups: base.groups.clone(),
264            options: base.options.clone(),
265            backups: base.backups.clone(),
266            version: base.version.clone(),
267            pgp_fingerprints: base.pgp_fingerprints.clone(),
268            dependencies: base.dependencies.clone(),
269            optional_dependencies: base.optional_dependencies.clone(),
270            provides: base.provides.clone(),
271            conflicts: base.conflicts.clone(),
272            replaces: base.replaces.clone(),
273            check_dependencies: base.check_dependencies.clone(),
274            make_dependencies: base.make_dependencies.clone(),
275            sources: merged_sources.collect(),
276            no_extracts: base.no_extracts.clone(),
277        }
278    }
279
280    /// Merges the non-architecture specific fields of a [`Package`] into `self`.
281    ///
282    /// Any field on `package` that is not [`Override::No`] overrides the pendant on `self`.
283    pub fn merge_package(&mut self, package: &Package) {
284        let package = package.clone();
285        package.description.merge_option(&mut self.description);
286
287        package.url.merge_option(&mut self.url);
288        package.changelog.merge_option(&mut self.changelog);
289        package.licenses.merge_vec(&mut self.licenses);
290        package.install.merge_option(&mut self.install);
291        package.groups.merge_vec(&mut self.groups);
292        package.options.merge_vec(&mut self.options);
293        package.backups.merge_vec(&mut self.backups);
294        package.dependencies.merge_vec(&mut self.dependencies);
295        package
296            .optional_dependencies
297            .merge_vec(&mut self.optional_dependencies);
298        package.provides.merge_vec(&mut self.provides);
299        package.conflicts.merge_vec(&mut self.conflicts);
300        package.replaces.merge_vec(&mut self.replaces);
301    }
302
303    /// Merges in architecture-specific overrides for fields.
304    ///
305    /// Takes a [`PackageBaseArchitecture`] and extends the non-architecture specific values
306    /// with the architecture specific ones.
307    /// This is an accumulative and non-destructive operation.
308    pub fn merge_architecture_properties(&mut self, base_architecture: &PackageBaseArchitecture) {
309        // Merge all source related info into aggregated structs.
310        let merged_sources = MergedSourceIterator {
311            sources: base_architecture.sources.iter(),
312            b2_checksums: base_architecture.b2_checksums.iter(),
313            md5_checksums: base_architecture.md5_checksums.iter(),
314            sha1_checksums: base_architecture.sha1_checksums.iter(),
315            sha224_checksums: base_architecture.sha224_checksums.iter(),
316            sha256_checksums: base_architecture.sha256_checksums.iter(),
317            sha384_checksums: base_architecture.sha384_checksums.iter(),
318            sha512_checksums: base_architecture.sha512_checksums.iter(),
319        };
320
321        self.dependencies
322            .extend_from_slice(&base_architecture.dependencies);
323        self.optional_dependencies
324            .extend_from_slice(&base_architecture.optional_dependencies);
325        self.provides.extend_from_slice(&base_architecture.provides);
326        self.conflicts
327            .extend_from_slice(&base_architecture.conflicts);
328        self.replaces.extend_from_slice(&base_architecture.replaces);
329        self.check_dependencies
330            .extend_from_slice(&base_architecture.check_dependencies);
331        self.make_dependencies
332            .extend_from_slice(&base_architecture.make_dependencies);
333
334        self.sources
335            .extend_from_slice(&merged_sources.collect::<Vec<MergedSource>>());
336    }
337}