debian_packaging/
debian_source_package_list.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5/*! A collection of source control package control files. */
6
7use {
8    crate::debian_source_control::DebianSourceControlFile,
9    std::ops::{Deref, DerefMut},
10};
11
12/// Represents a collection of Debian source control paragraphs.
13///
14/// This provides a wrapper around [Vec<DebianSourceControlFile>] for convenience.
15///
16/// Note that [DebianSourceControlFile] within this collection may not conform to the
17/// strict requirements of Debian source control `.dsc` files. For example, the
18/// `Source` field may not be present (try `Package` instead).
19#[derive(Default)]
20pub struct DebianSourcePackageList<'a> {
21    packages: Vec<DebianSourceControlFile<'a>>,
22}
23
24impl<'a> Deref for DebianSourcePackageList<'a> {
25    type Target = Vec<DebianSourceControlFile<'a>>;
26
27    fn deref(&self) -> &Self::Target {
28        &self.packages
29    }
30}
31
32impl<'a> DerefMut for DebianSourcePackageList<'a> {
33    fn deref_mut(&mut self) -> &mut Self::Target {
34        &mut self.packages
35    }
36}
37
38impl<'a> IntoIterator for DebianSourcePackageList<'a> {
39    type Item = DebianSourceControlFile<'a>;
40    type IntoIter = std::vec::IntoIter<Self::Item>;
41
42    fn into_iter(self) -> Self::IntoIter {
43        self.packages.into_iter()
44    }
45}
46
47impl<'a> DebianSourcePackageList<'a> {
48    /// Find source packages having the given name.
49    ///
50    /// This patches against the `Package` field in the control files.
51    pub fn iter_with_package_name(
52        &self,
53        package: String,
54    ) -> impl Iterator<Item = &DebianSourceControlFile<'a>> {
55        self.packages.iter().filter(
56            move |cf| matches!(cf.required_field_str("Package"), Ok(name) if name == package),
57        )
58    }
59
60    /// Find source packages providing the given binary package.
61    ///
62    /// This consults the list of binary packages in the `Binary` field and returns control
63    /// paragraphs where `package` appears in that list.
64    pub fn iter_with_binary_package(
65        &self,
66        package: String,
67    ) -> impl Iterator<Item = &DebianSourceControlFile<'a>> {
68        self.packages.iter().filter(move |cf| {
69            if let Some(mut packages) = cf.binary() {
70                packages.any(|p| p == package)
71            } else {
72                false
73            }
74        })
75    }
76
77    /// Find source packages providing packages for the given architecture.
78    ///
79    /// This consults the list of architectures in the `Architecture` field and returns
80    /// control paragraphs where `architecture` appears in that list.
81    pub fn iter_with_architecture(
82        &self,
83        architecture: String,
84    ) -> impl Iterator<Item = &DebianSourceControlFile<'a>> {
85        self.packages.iter().filter(move |cf| {
86            if let Some(mut architectures) = cf.architecture() {
87                architectures.any(|a| a == architecture)
88            } else {
89                false
90            }
91        })
92    }
93}