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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

/*! A collection of source control package control files. */

use {
    crate::debian_source_control::DebianSourceControlFile,
    std::ops::{Deref, DerefMut},
};

/// Represents a collection of Debian source control paragraphs.
///
/// This provides a wrapper around [Vec<DebianSourceControlFile>] for convenience.
///
/// Note that [DebianSourceControlFile] within this collection may not conform to the
/// strict requirements of Debian source control `.dsc` files. For example, the
/// `Source` field may not be present (try `Package` instead).
#[derive(Default)]
pub struct DebianSourcePackageList<'a> {
    packages: Vec<DebianSourceControlFile<'a>>,
}

impl<'a> Deref for DebianSourcePackageList<'a> {
    type Target = Vec<DebianSourceControlFile<'a>>;

    fn deref(&self) -> &Self::Target {
        &self.packages
    }
}

impl<'a> DerefMut for DebianSourcePackageList<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.packages
    }
}

impl<'a> IntoIterator for DebianSourcePackageList<'a> {
    type Item = DebianSourceControlFile<'a>;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.packages.into_iter()
    }
}

impl<'a> DebianSourcePackageList<'a> {
    /// Find source packages having the given name.
    ///
    /// This patches against the `Package` field in the control files.
    pub fn iter_with_package_name(
        &self,
        package: String,
    ) -> impl Iterator<Item = &DebianSourceControlFile<'a>> {
        self.packages.iter().filter(
            move |cf| matches!(cf.required_field_str("Package"), Ok(name) if name == package),
        )
    }

    /// Find source packages providing the given binary package.
    ///
    /// This consults the list of binary packages in the `Binary` field and returns control
    /// paragraphs where `package` appears in that list.
    pub fn iter_with_binary_package(
        &self,
        package: String,
    ) -> impl Iterator<Item = &DebianSourceControlFile<'a>> {
        self.packages.iter().filter(move |cf| {
            if let Some(mut packages) = cf.binary() {
                packages.any(|p| p == package)
            } else {
                false
            }
        })
    }

    /// Find source packages providing packages for the given architecture.
    ///
    /// This consults the list of architectures in the `Architecture` field and returns
    /// control paragraphs where `architecture` appears in that list.
    pub fn iter_with_architecture(
        &self,
        architecture: String,
    ) -> impl Iterator<Item = &DebianSourceControlFile<'a>> {
        self.packages.iter().filter(move |cf| {
            if let Some(mut architectures) = cf.architecture() {
                architectures.any(|a| a == architecture)
            } else {
                false
            }
        })
    }
}