arch_pkg_text/srcinfo/
field.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
165
166
167
168
169
170
171
172
use strum::{AsRefStr, Display, EnumString, IntoStaticStr};

/// Field of a `.SRCINFO` file.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Field<Name, Architecture> {
    name: Name,
    architecture: Option<Architecture>,
}

impl Field<(), ()> {
    /// Create a blank [`Field`].
    ///
    /// This function when combined with [`with_name`](Field::with_name) and [`with_architecture`](Field::with_architecture)
    /// would produce a usable [`Field`].
    pub fn blank() -> Self {
        Field {
            name: (),
            architecture: None,
        }
    }
}

impl<Name, Architecture> Field<Name, Architecture> {
    /// Replace the name of the field.
    pub fn with_name<NewName>(self, name: NewName) -> Field<NewName, Architecture> {
        Field {
            name,
            architecture: self.architecture,
        }
    }

    /// Replace the architecture suffix of the field.
    pub fn with_architecture<NewArchitecture>(
        self,
        architecture: Option<NewArchitecture>,
    ) -> Field<Name, NewArchitecture> {
        Field {
            name: self.name,
            architecture,
        }
    }

    /// Get an immutable reference to the name of the field.
    pub const fn name(&self) -> &'_ Name {
        &self.name
    }

    /// Get an immutable reference to the architecture suffix of the field.
    pub const fn architecture(&self) -> Option<&'_ Architecture> {
        self.architecture.as_ref()
    }

    /// Destructure into a tuple of field name and architecture.
    pub fn into_components(self) -> (Name, Option<Architecture>) {
        (self.name, self.architecture)
    }
}

impl<'a, Architecture> Field<&'a str, Architecture> {
    /// Get the name of the field as a string slice.
    pub const fn name_str(&self) -> &'a str {
        self.name()
    }
}

impl<'a, Name> Field<Name, &'a str> {
    /// Get the name of the field as a string slice.
    pub fn architecture_str(&self) -> Option<&'a str> {
        self.architecture().copied()
    }
}

/// Raw string field of a `.SRCINFO` file.
pub type RawField<'a> = Field<&'a str, &'a str>;

/// Parsed field of a `.SRCINFO` file.
pub type ParsedField<Architecture> = Field<FieldName, Architecture>;

impl<Architecture> ParsedField<Architecture> {
    /// Get the name of the field as a string slice.
    pub fn name_str(&self) -> &'static str {
        self.name().into()
    }
}

/// Convert a [`FieldName`] into a [`ParsedField`] without an architecture.
impl<Architecture> From<FieldName> for ParsedField<Architecture> {
    fn from(value: FieldName) -> Self {
        Field::blank().with_name(value).with_architecture(None)
    }
}

/// Field name of a `.SRCINFO` file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)] // core traits
#[derive(AsRefStr, Display, EnumString, IntoStaticStr)] // strum traits
#[strum(use_phf)]
pub enum FieldName {
    /* SECTION HEADERS */
    #[strum(serialize = "pkgbase")]
    Base,
    #[strum(serialize = "pkgname")]
    Name,

    /* BASE SECTION ONLY */
    #[strum(serialize = "epoch")]
    Epoch,
    #[strum(serialize = "pkgrel")]
    Release,
    #[strum(serialize = "validpgpkeys")]
    ValidPgpKeys,
    #[strum(serialize = "pkgver")]
    Version,

    /* ANY SECTION: MISC */
    #[strum(serialize = "arch")]
    Architecture,
    #[strum(serialize = "backup")]
    Backup,
    #[strum(serialize = "changelog")]
    ChangeLog,
    #[strum(serialize = "pkgdesc")]
    Description,
    #[strum(serialize = "groups")]
    Groups,
    #[strum(serialize = "install")]
    InstallScript,
    #[strum(serialize = "license")]
    License,
    #[strum(serialize = "noextract")]
    NoExtract,
    #[strum(serialize = "options")]
    Options,
    #[strum(serialize = "source")]
    Source,
    #[strum(serialize = "url")]
    Url,

    /* ANY SECTION: DEPENDENCIES */
    #[strum(serialize = "depends")]
    Dependencies,
    #[strum(serialize = "checkdepends")]
    CheckDependencies,
    #[strum(serialize = "makedepends")]
    MakeDependencies,
    #[strum(serialize = "optdepends")]
    OptionalDependencies,
    #[strum(serialize = "provides")]
    Provides,
    #[strum(serialize = "conflicts")]
    Conflicts,
    #[strum(serialize = "replaces")]
    Replaces,

    /* ANY SECTION: CHECKSUMS */
    #[strum(serialize = "md5sums")]
    Md5Checksums,
    #[strum(serialize = "sha1sums")]
    Sha1Checksums,
    #[strum(serialize = "sha224sums")]
    Sha224Checksums,
    #[strum(serialize = "sha256sums")]
    Sha256Checksums,
    #[strum(serialize = "sha384sums")]
    Sha384Checksums,
    #[strum(serialize = "sha512sums")]
    Sha512Checksums,
    #[strum(serialize = "b2sums")]
    Blake2bChecksums,
}

mod parse;
pub use parse::*;