arch_pkg_text/srcinfo/
field.rs

1use strum::{AsRefStr, Display, EnumString, IntoStaticStr};
2
3/// Field of a `.SRCINFO` file.
4#[derive(Debug, Clone, Copy, Eq, PartialEq)]
5pub struct Field<Name, Architecture> {
6    name: Name,
7    architecture: Option<Architecture>,
8}
9
10impl Field<(), ()> {
11    /// Create a blank [`Field`].
12    ///
13    /// This function when combined with [`with_name`](Field::with_name) and [`with_architecture`](Field::with_architecture)
14    /// would produce a usable [`Field`].
15    pub fn blank() -> Self {
16        Field {
17            name: (),
18            architecture: None,
19        }
20    }
21}
22
23impl<Name, Architecture> Field<Name, Architecture> {
24    /// Replace the name of the field.
25    pub fn with_name<NewName>(self, name: NewName) -> Field<NewName, Architecture> {
26        Field {
27            name,
28            architecture: self.architecture,
29        }
30    }
31
32    /// Replace the architecture suffix of the field.
33    pub fn with_architecture<NewArchitecture>(
34        self,
35        architecture: Option<NewArchitecture>,
36    ) -> Field<Name, NewArchitecture> {
37        Field {
38            name: self.name,
39            architecture,
40        }
41    }
42
43    /// Get an immutable reference to the name of the field.
44    pub const fn name(&self) -> &'_ Name {
45        &self.name
46    }
47
48    /// Get an immutable reference to the architecture suffix of the field.
49    pub const fn architecture(&self) -> Option<&'_ Architecture> {
50        self.architecture.as_ref()
51    }
52
53    /// Destructure into a tuple of field name and architecture.
54    pub fn into_components(self) -> (Name, Option<Architecture>) {
55        (self.name, self.architecture)
56    }
57}
58
59impl<'a, Architecture> Field<&'a str, Architecture> {
60    /// Get the name of the field as a string slice.
61    pub const fn name_str(&self) -> &'a str {
62        self.name()
63    }
64}
65
66impl<'a, Name> Field<Name, &'a str> {
67    /// Get the name of the field as a string slice.
68    pub fn architecture_str(&self) -> Option<&'a str> {
69        self.architecture().copied()
70    }
71}
72
73/// Raw string field of a `.SRCINFO` file.
74pub type RawField<'a> = Field<&'a str, &'a str>;
75
76/// Parsed field of a `.SRCINFO` file.
77pub type ParsedField<Architecture> = Field<FieldName, Architecture>;
78
79impl<Architecture> ParsedField<Architecture> {
80    /// Get the name of the field as a string slice.
81    pub fn name_str(&self) -> &'static str {
82        self.name().into()
83    }
84}
85
86/// Convert a [`FieldName`] into a [`ParsedField`] without an architecture.
87impl<Architecture> From<FieldName> for ParsedField<Architecture> {
88    fn from(value: FieldName) -> Self {
89        Field::blank().with_name(value).with_architecture(None)
90    }
91}
92
93/// Field name of a `.SRCINFO` file.
94#[derive(Debug, Clone, Copy, PartialEq, Eq)] // core traits
95#[derive(AsRefStr, Display, EnumString, IntoStaticStr)] // strum traits
96#[strum(use_phf)]
97pub enum FieldName {
98    /* SECTION HEADERS */
99    #[strum(serialize = "pkgbase")]
100    Base,
101    #[strum(serialize = "pkgname")]
102    Name,
103
104    /* BASE SECTION ONLY */
105    #[strum(serialize = "epoch")]
106    Epoch,
107    #[strum(serialize = "pkgrel")]
108    Release,
109    #[strum(serialize = "validpgpkeys")]
110    ValidPgpKeys,
111    #[strum(serialize = "pkgver")]
112    Version,
113
114    /* ANY SECTION: MISC */
115    #[strum(serialize = "arch")]
116    Architecture,
117    #[strum(serialize = "backup")]
118    Backup,
119    #[strum(serialize = "changelog")]
120    ChangeLog,
121    #[strum(serialize = "pkgdesc")]
122    Description,
123    #[strum(serialize = "groups")]
124    Groups,
125    #[strum(serialize = "install")]
126    InstallScript,
127    #[strum(serialize = "license")]
128    License,
129    #[strum(serialize = "noextract")]
130    NoExtract,
131    #[strum(serialize = "options")]
132    Options,
133    #[strum(serialize = "source")]
134    Source,
135    #[strum(serialize = "url")]
136    Url,
137
138    /* ANY SECTION: DEPENDENCIES */
139    #[strum(serialize = "depends")]
140    Dependencies,
141    #[strum(serialize = "checkdepends")]
142    CheckDependencies,
143    #[strum(serialize = "makedepends")]
144    MakeDependencies,
145    #[strum(serialize = "optdepends")]
146    OptionalDependencies,
147    #[strum(serialize = "provides")]
148    Provides,
149    #[strum(serialize = "conflicts")]
150    Conflicts,
151    #[strum(serialize = "replaces")]
152    Replaces,
153
154    /* ANY SECTION: CHECKSUMS */
155    #[strum(serialize = "md5sums")]
156    Md5Checksums,
157    #[strum(serialize = "sha1sums")]
158    Sha1Checksums,
159    #[strum(serialize = "sha224sums")]
160    Sha224Checksums,
161    #[strum(serialize = "sha256sums")]
162    Sha256Checksums,
163    #[strum(serialize = "sha384sums")]
164    Sha384Checksums,
165    #[strum(serialize = "sha512sums")]
166    Sha512Checksums,
167    #[strum(serialize = "b2sums")]
168    Blake2bChecksums,
169}
170
171mod parse;
172pub use parse::*;