forked from Morganamilo/paru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.rs
164 lines (142 loc) · 4.22 KB
/
base.rs
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
use crate::{AurPackage, CustomPackage};
use std::fmt::{Display, Formatter};
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>,
}
/// 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>,
}
/// A package base.
/// This describes 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 pkgbase = self.package_base();
let ver = self.version();
let name = self.package_base();
let len = self.pkgs.len();
if len == 1 && name == pkgbase {
write!(f, "{}-{}", pkgbase, ver)
} else {
write!(f, "{}-{} ({}", self.package_base(), self.version(), name)?;
for pkg in self.pkgs.iter().skip(1) {
f.write_str(" ")?;
f.write_str(&pkg.pkg.name)?;
}
f.write_str(")")?;
Ok(())
}
}
}
impl Display for CustomPackages {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let pkgbase = self.package_base();
let ver = self.version();
let name = self.package_base();
let len = self.pkgs.len();
if len == 1 && name == pkgbase {
write!(f, "{}-{}", pkgbase, ver)
} else {
write!(f, "{}-{} ({}", self.package_base(), self.version(), name)?;
for pkg in self.pkgs.iter().skip(1) {
f.write_str(" ")?;
f.write_str(&pkg.pkg.pkgname)?;
}
f.write_str(")")?;
Ok(())
}
}
}
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(),
}
}
/// Amount 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()))
}
}
}
}