pkgcraft 0.0.31

library of Gentoo functionality
Documentation
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::fmt;

use enum_as_inner::EnumAsInner;
use scallop::ExecStatus;

use crate::dep::{Cpn, Cpv, Dep, Version};
use crate::eapi::{Eapi, Restrict as EapiRestrict};
use crate::repo::{Repo, Repository};
use crate::restrict::str::Restrict as StrRestrict;
use crate::restrict::{Restrict as BaseRestrict, Restriction};
use crate::traits::Intersects;

pub mod ebuild;
pub mod fake;

#[allow(clippy::large_enum_variant)]
#[derive(EnumAsInner, Debug, Clone)]
pub enum Pkg {
    Configured(ebuild::EbuildConfiguredPkg),
    Ebuild(ebuild::EbuildPkg),
    Fake(fake::Pkg),
}

make_pkg_traits!(Pkg);

pub trait Package:
    fmt::Debug + fmt::Display + Intersects<Dep> + Intersects<Cpv> + Intersects<Cpn>
{
    /// Return a package's EAPI.
    fn eapi(&self) -> &'static Eapi;

    /// Return a package's Cpv.
    fn cpv(&self) -> &Cpv;

    /// Return the unversioned package.
    fn cpn(&self) -> &Cpn {
        self.cpv().cpn()
    }

    /// Return a package's category.
    fn category(&self) -> &str {
        self.cpv().category()
    }

    /// Return a package's name.
    fn package(&self) -> &str {
        self.cpv().package()
    }

    /// Return a package's version.
    fn version(&self) -> &Version {
        self.cpv().version()
    }

    /// Return a package's name and version.
    fn p(&self) -> String {
        self.cpv().p()
    }

    /// Return a package's name, version, and revision.
    fn pf(&self) -> String {
        self.cpv().pf()
    }

    /// Return a package's revision.
    fn pr(&self) -> String {
        self.cpv().pr()
    }

    /// Return a package's version.
    fn pv(&self) -> String {
        self.cpv().pv()
    }

    /// Returna package's version and revision.
    fn pvr(&self) -> String {
        self.cpv().pvr()
    }
}

pub trait RepoPackage: Package + Ord {
    type Repo: Repository;

    /// Return a package's repo.
    fn repo(&self) -> Self::Repo;
}

#[allow(dead_code)]
pub(crate) trait Build: Package {
    /// Run the build operations for a package.
    fn build(&self) -> scallop::Result<()>;
}

pub(crate) trait PkgPretend: Package {
    /// Run the pkg_pretend operation for a package.
    fn pkg_pretend(&self) -> scallop::Result<Option<String>>;
}

pub trait Source: Package {
    /// Source a package.
    fn source(&self) -> scallop::Result<ExecStatus>;
}

macro_rules! make_pkg_traits {
    ($($x:ty),+) => {$(
        impl PartialEq for $x {
            fn eq(&self, other: &Self) -> bool {
                self.repo() == other.repo() && self.cpv() == other.cpv()
            }
        }

        impl Eq for $x {}

        impl std::hash::Hash for $x {
            fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
                self.repo().hash(state);
                self.cpv().hash(state);
            }
        }

        impl PartialOrd for $x {
            fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
                Some(self.cmp(other))
            }
        }

        impl Ord for $x {
            fn cmp(&self, other: &Self) -> std::cmp::Ordering {
                self.cpv().cmp(other.cpv()).then_with(|| self.repo().cmp(&other.repo()))
            }
        }

        impl std::fmt::Display for $x {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "{}::{}", self.cpv(), self.repo())
            }
        }

        impl From<&$x> for $crate::restrict::Restrict {
            fn from(pkg: &$x) -> Self {
                let r1 = pkg.cpv().into();
                let r2 = $crate::restrict::Restrict::Dep(pkg.repo().into());
                $crate::restrict::Restrict::and([r1, r2])
            }
        }

        impl $crate::traits::Intersects<$x> for $crate::dep::Dep {
            fn intersects(&self, other: &$x) -> bool {
                other.intersects(self)
            }
        }

        impl $crate::traits::Intersects<$crate::dep::Cpv> for $x {
            fn intersects(&self, cpv: &$crate::dep::Cpv) -> bool {
                self.cpv() == cpv
            }
        }

        impl $crate::traits::Intersects<$x> for $crate::dep::Cpv {
            fn intersects(&self, other: &$x) -> bool {
                other.intersects(self)
            }
        }

        impl $crate::traits::Intersects<$crate::dep::Cpn> for $x {
            fn intersects(&self, cpn: &$crate::dep::Cpn) -> bool {
                self.cpn() == cpn
            }
        }

        impl $crate::traits::Intersects<$x> for $crate::dep::Cpn {
            fn intersects(&self, other: &$x) -> bool {
                other.intersects(self)
            }
        }

        impl From<&$x> for $crate::dep::Cpv {
            fn from(value: &$x) -> Self {
                value.cpv().clone()
            }
        }
    )+};
}
use make_pkg_traits;

impl Package for Pkg {
    fn eapi(&self) -> &'static Eapi {
        match self {
            Self::Configured(pkg) => pkg.eapi(),
            Self::Ebuild(pkg) => pkg.eapi(),
            Self::Fake(pkg) => pkg.eapi(),
        }
    }

    fn cpv(&self) -> &Cpv {
        match self {
            Self::Configured(pkg) => pkg.cpv(),
            Self::Ebuild(pkg) => pkg.cpv(),
            Self::Fake(pkg) => pkg.cpv(),
        }
    }
}

impl RepoPackage for Pkg {
    type Repo = Repo;

    fn repo(&self) -> Self::Repo {
        match self {
            Self::Configured(pkg) => pkg.repo().into(),
            Self::Ebuild(pkg) => pkg.repo().into(),
            Self::Fake(pkg) => pkg.repo().into(),
        }
    }
}

impl Intersects<Dep> for Pkg {
    fn intersects(&self, dep: &Dep) -> bool {
        match self {
            Self::Configured(pkg) => pkg.intersects(dep),
            Self::Ebuild(pkg) => pkg.intersects(dep),
            Self::Fake(pkg) => pkg.intersects(dep),
        }
    }
}

impl<T> Package for &T
where
    T: Package,
{
    fn eapi(&self) -> &'static Eapi {
        (*self).eapi()
    }
    fn cpv(&self) -> &Cpv {
        (*self).cpv()
    }
}

impl<T> RepoPackage for &T
where
    T: RepoPackage,
{
    type Repo = T::Repo;

    fn repo(&self) -> Self::Repo {
        (*self).repo()
    }
}

impl<T> Intersects<Dep> for &T
where
    T: Package,
{
    fn intersects(&self, dep: &Dep) -> bool {
        (*self).intersects(dep)
    }
}

impl<T> Intersects<Cpv> for &T
where
    T: Package,
{
    fn intersects(&self, cpv: &Cpv) -> bool {
        (*self).intersects(cpv)
    }
}

impl<T> Intersects<Cpn> for &T
where
    T: Package,
{
    fn intersects(&self, cpn: &Cpn) -> bool {
        (*self).intersects(cpn)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Restrict {
    Eapi(EapiRestrict),
    Ebuild(ebuild::Restrict),
    Repo(StrRestrict),
}

impl Restrict {
    pub fn eapi(s: &str) -> Self {
        Self::Eapi(EapiRestrict::Id(StrRestrict::equal(s)))
    }

    pub fn repo(s: &str) -> Self {
        Self::Repo(StrRestrict::equal(s))
    }
}

impl From<Restrict> for BaseRestrict {
    fn from(r: Restrict) -> Self {
        Self::Pkg(r)
    }
}

impl Restriction<&Pkg> for Restrict {
    fn matches(&self, pkg: &Pkg) -> bool {
        match self {
            Self::Eapi(r) => r.matches(pkg.eapi()),
            Self::Repo(r) => r.matches(pkg.repo().id()),
            Self::Ebuild(r) => match pkg {
                Pkg::Ebuild(p) => r.matches(p),
                _ => false,
            },
        }
    }
}

impl Restriction<&Pkg> for BaseRestrict {
    fn matches(&self, pkg: &Pkg) -> bool {
        crate::restrict::restrict_match! {self, pkg,
            Self::Dep(r) => r.matches(pkg),
            Self::Pkg(r) => r.matches(pkg),
        }
    }
}

impl Restriction<&Repo> for Restrict {
    fn matches(&self, repo: &Repo) -> bool {
        match self {
            Self::Repo(r) => r.matches(repo.id()),
            _ => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use itertools::Itertools;

    use crate::eapi::EAPI_LATEST_OFFICIAL;
    use crate::repo::{PkgRepository, fake};
    use crate::test::assert_ordered_eq;

    use super::*;

    #[test]
    fn ordering() {
        // unmatching pkgs sorted by dep attributes
        let r1: Repo = fake::FakeRepo::new("b", 0)
            .pkgs(["cat/pkg-1"])
            .unwrap()
            .into();
        let r2: Repo = fake::FakeRepo::new("a", 0)
            .pkgs(["cat/pkg-0"])
            .unwrap()
            .into();
        let pkgs: Vec<_> = r1.iter().chain(r2.iter()).try_collect().unwrap();
        let sorted_pkgs: Vec<_> = pkgs.iter().sorted().collect();
        assert_ordered_eq!(pkgs.iter().rev(), sorted_pkgs);

        // matching pkgs sorted by repo priority
        let r1: Repo = fake::FakeRepo::new("a", -1)
            .pkgs(["cat/pkg-0"])
            .unwrap()
            .into();
        let r2: Repo = fake::FakeRepo::new("b", 0)
            .pkgs(["cat/pkg-0"])
            .unwrap()
            .into();
        let pkgs: Vec<_> = r1.iter().chain(r2.iter()).try_collect().unwrap();
        let sorted_pkgs: Vec<_> = pkgs.iter().sorted().collect();
        assert_ordered_eq!(pkgs.iter().rev(), sorted_pkgs);

        // matching pkgs sorted by repo id since repos have matching priorities
        let r1: Repo = fake::FakeRepo::new("2", 0)
            .pkgs(["cat/pkg-0"])
            .unwrap()
            .into();
        let r2: Repo = fake::FakeRepo::new("1", 0)
            .pkgs(["cat/pkg-0"])
            .unwrap()
            .into();
        let pkgs: Vec<_> = r1.iter().chain(r2.iter()).try_collect().unwrap();
        let sorted_pkgs: Vec<_> = pkgs.iter().sorted().collect();
        assert_ordered_eq!(pkgs.iter().rev(), sorted_pkgs);
    }

    #[test]
    fn package_trait() {
        let cpv = Cpv::try_new("cat/pkg-1-r2").unwrap();
        let cpn = Cpn::try_new("cat/pkg").unwrap();
        let dep = Dep::try_new(">=cat/pkg-1").unwrap();
        let r: Repo = fake::FakeRepo::new("test", 0).pkgs([&cpv]).unwrap().into();
        let pkg = r.iter_restrict(&cpv).next().unwrap().unwrap();
        assert_eq!(pkg.eapi(), *EAPI_LATEST_OFFICIAL);
        assert_eq!(pkg.cpv(), &cpv);
        assert_eq!(pkg.cpn(), &cpn);
        assert_eq!(pkg.category(), "cat");
        assert_eq!(pkg.package(), "pkg");
        assert_eq!(pkg.version().to_string(), "1-r2");
        assert_eq!(pkg.p(), "pkg-1");
        assert_eq!(pkg.pf(), "pkg-1-r2");
        assert_eq!(pkg.pr(), "r2");
        assert_eq!(pkg.pv(), "1");
        assert_eq!(pkg.pvr(), "1-r2");

        // intersects
        assert!(pkg.intersects(&dep));
        assert!(dep.intersects(&pkg));
        assert!(pkg.intersects(&cpv));
        assert!(cpv.intersects(&pkg));
        assert!(pkg.intersects(&cpn));
        assert!(cpn.intersects(&pkg));

        let pkg = &&pkg;
        assert!(pkg.intersects(&dep));
        assert!(pkg.intersects(&cpv));
        assert!(pkg.intersects(&cpn));
    }

    #[test]
    fn intersects_dep() {
        let cpv = Cpv::try_new("cat/pkg-1-r2").unwrap();
        let r: Repo = fake::FakeRepo::new("test", 0).pkgs([&cpv]).unwrap().into();
        let pkg = r.iter_restrict(&cpv).next().unwrap().unwrap();

        for (s, expected) in [
            ("cat/pkg", true),
            ("a/b", false),
            ("=cat/pkg-1-r2", true),
            (">cat/pkg-1-r2", false),
            ("~cat/pkg-1", true),
        ] {
            let dep: Dep = s.parse().unwrap();
            assert_eq!(pkg.intersects(&dep), expected, "failed for {s}");
        }
    }
}