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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::{AurPackage, CustomPackage};

use std::fmt::{Display, Formatter, Write};

enum PkgNames<A, C> {
    Aur(A),
    Custom(C),
}

impl<'a, A, C> Iterator for PkgNames<A, C>
where
    A: Iterator<Item = &'a str>,
    C: Iterator<Item = &'a str>,
{
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            PkgNames::Aur(i) => i.next(),
            PkgNames::Custom(i) => i.next(),
        }
    }
}

/// Packages from a custom repo.
#[derive(Debug, Eq, Clone, PartialEq, Ord, PartialOrd, Hash)]
pub struct CustomPackages {
    /// the repo the package came from.
    pub repo: String,
    /// The srcinfo of the pkgbase.
    pub srcinfo: Box<srcinfo::Srcinfo>,
    /// The pkgs from the srcinfo to install.
    pub pkgs: Vec<CustomPackage>,
    /// Should the package be built.
    pub build: bool,
}

/// Describes an AUR package base.
#[derive(Debug, Eq, Clone, PartialEq, Ord, PartialOrd, Hash)]
pub struct AurBase {
    /// List of packages belonging to the package base.
    pub pkgs: Vec<AurPackage>,
    /// Should the package be built.
    pub build: bool,
}

/// A package base.
/// This descripes  packages that should be built then installed.
#[derive(Debug, Eq, Clone, PartialEq, Ord, PartialOrd, Hash)]
pub enum Base {
    /// Aur packages.
    Aur(AurBase),
    /// Custom packages.
    Custom(CustomPackages),
}

impl Display for AurBase {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let pkgs = self.pkgs.iter().map(|p| p.pkg.name.as_str());
        Base::write_base(f, self.package_base(), &self.version(), pkgs)
    }
}

impl Display for CustomPackages {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let pkgs = self.pkgs.iter().map(|p| p.pkg.pkgname.as_str());
        Base::write_base(f, self.package_base(), &self.version(), pkgs)
    }
}

impl Display for Base {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Base::Aur(base) => base.fmt(f),
            Base::Custom(base) => base.fmt(f),
        }
    }
}

impl AurBase {
    /// Gets the package base of base.
    pub fn package_base(&self) -> &str {
        &self.pkgs[0].pkg.package_base
    }

    /// Gets the version of base.
    pub fn version(&self) -> String {
        self.pkgs[0].pkg.version.clone()
    }
}

impl CustomPackages {
    /// Gets the package base of base.
    pub fn package_base(&self) -> &str {
        &self.srcinfo.base.pkgbase
    }

    /// Gets the version of base.
    pub fn version(&self) -> String {
        self.srcinfo.version()
    }
}

impl Base {
    /// Gets the package base of base.
    pub fn package_base(&self) -> &str {
        match self {
            Base::Aur(base) => base.package_base(),
            Base::Custom(base) => base.package_base(),
        }
    }

    /// Gets the version of base.
    pub fn version(&self) -> String {
        match self {
            Base::Aur(base) => base.version(),
            Base::Custom(base) => base.version(),
        }
    }

    /// Ammount of packages in this base.
    pub fn package_count(&self) -> usize {
        match self {
            Base::Aur(base) => base.pkgs.len(),
            Base::Custom(base) => base.pkgs.len(),
        }
    }

    /// Iterator of package names in this base.
    pub fn packages(&self) -> impl Iterator<Item = &str> {
        match self {
            Base::Aur(base) => PkgNames::Aur(base.pkgs.iter().map(|p| p.pkg.name.as_str())),
            Base::Custom(base) => {
                PkgNames::Custom(base.pkgs.iter().map(|p| p.pkg.pkgname.as_str()))
            }
        }
    }

    /// Are any packages in this base make only.
    pub fn make(&self) -> bool {
        match self {
            Base::Aur(a) => a.pkgs.iter().any(|p| p.make),
            Base::Custom(c) => c.pkgs.iter().any(|p| p.make),
        }
    }

    /// Are any packages in this base targets.
    pub fn target(&self) -> bool {
        match self {
            Base::Aur(a) => a.pkgs.iter().any(|p| p.target),
            Base::Custom(c) => c.pkgs.iter().any(|p| p.target),
        }
    }

    /// Should the packages be built
    pub fn build(&self) -> bool {
        match self {
            Base::Aur(a) => a.build,
            Base::Custom(c) => c.build,
        }
    }

    /// Formats a base into the format:
    /// pkgname-ver
    /// or, if there are multiple packages:
    /// pkgbase-ver (pkg1 pkg2 pkg2)
    pub fn write_base<'a, W: Write, I: IntoIterator<Item = &'a str>>(
        mut writer: W,
        pkgbase: &str,
        ver: &str,
        pkgs: I,
    ) -> std::fmt::Result {
        let mut pkgs = pkgs.into_iter().peekable();
        let name = pkgs.next().unwrap_or("");

        if pkgs.peek().is_none() && name == pkgbase {
            write!(writer, "{pkgbase}-{ver}")
        } else {
            write!(writer, "{pkgbase}-{ver} ({name}")?;
            for pkg in pkgs {
                writer.write_str(" ")?;
                writer.write_str(pkg.as_ref())?;
            }
            writer.write_str(")")?;
            Ok(())
        }
    }

    /// True if the package base contains a single package
    /// with the same name as the pkgbase.
    pub fn base_is_pkg<'a, I: IntoIterator<Item = &'a str>>(pkgbase: &str, pkgs: I) -> bool {
        let mut pkgs = pkgs.into_iter();
        let Some(p) = pkgs.next() else { return false };
        p == pkgbase && pkgs.next().is_none()
    }
}