summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: b7b01a700dd6b749315cb3d60e45344a93fe0325 (plain)
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
//! Gentoo and PMS related utils.
//!
//! Currently implements:
//! - parsers for atoms and DEPEND expressions
//! - strongly typed representations of atoms, versions, etc
//! - version comparison and equality impls
//! - iterator over repos categories and ebuilds
//!
//! Planned features
//! - profile evaluation
//! - vdb reader
//! - sourcing ebuilds with bash
//!

#![deny(clippy::pedantic, unused_imports)]
#![allow(
    dead_code,
    unstable_name_collisions,
    clippy::missing_errors_doc,
    clippy::missing_panics_doc
)]
#![feature(impl_trait_in_assoc_type)]

use mon::{
    Parser,
    input::{Input, InputIter},
};

pub trait Parseable<'a, I: Input + 'a> {
    type Parser: Parser<I, Output = Self>;

    fn parser() -> Self::Parser;

    fn parse(input: I) -> Result<Self, I>
    where
        Self: Sized,
    {
        Self::parser()
            .parse_finished(InputIter::new(input))
            .map_err(|e| e.rest())
    }
}

/// Strongly typed atom and cpv representations.
///
/// Create atoms from parsers:
/// ```
/// use gentoo_utils::{Parseable, atom::Atom};
///
/// let emacs = Atom::parse("=app-editors/emacs-31.0-r1")
///    .expect("failed to parse atom");
///
/// assert_eq!(emacs.to_string(), "=app-editors/emacs-31.0-r1");
/// ````
///
/// Compare versions:
/// ```
/// use gentoo_utils::{Parseable, atom::Cpv};
///
/// let a = Cpv::parse("foo/bar-1.0").unwrap();
/// let b = Cpv::parse("foo/bar-2.0").unwrap();
///
/// assert!(a < b);
/// ```
pub mod atom;

/// Access to repos and ebuilds.
///
/// ```
/// use gentoo_utils::repo::Repo;
///
/// let repo = Repo::new("/var/db/repos/gentoo");
///
/// for result in repo.categories().expect("failed to read categories") {
///    let category = result.expect("failed to read category");
///
///    for result in category.ebuilds().expect("failed to read ebuilds") {
///        let ebuild = result.expect("failed to read ebuild");
///
///        println!(
///            "{}-{}: {}",
///            ebuild.name(),
///            ebuild.version(),
///            ebuild.description().clone().unwrap_or("no description available".to_string())
///        );
///    }
/// }
///
/// ```
pub mod repo;
pub mod useflag;