Skip to main content

alpm_types/
file_type.rs

1//! File type handling.
2
3use std::str::FromStr;
4
5use alpm_parsers::prelude::*;
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8use strum::{AsRefStr, Display, EnumString, IntoStaticStr, VariantNames};
9use winnow::{ascii::alpha1, error::ErrMode};
10
11/// The identifier of a file type used in ALPM.
12///
13/// These identifiers are used in the file names of file types such as binary packages (see
14/// [alpm-package]), source packages and repository sync databases (see alpm-repo-db).
15///
16/// [alpm-package]: https://alpm.archlinux.page/specifications/alpm-package.7.html
17#[derive(
18    AsRefStr, Clone, Copy, Debug, Display, EnumString, Eq, IntoStaticStr, PartialEq, VariantNames,
19)]
20#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
21pub enum FileTypeIdentifier {
22    /// The identifier for [alpm-package] files.
23    ///
24    /// [alpm-package]: https://alpm.archlinux.page/specifications/alpm-package.7.html
25    #[cfg_attr(feature = "serde", serde(rename = "pkg"))]
26    #[strum(to_string = "pkg")]
27    BinaryPackage,
28
29    /// The identifier for alpm-repo-db files.
30    #[cfg_attr(feature = "serde", serde(rename = "db"))]
31    #[strum(to_string = "db")]
32    RepositorySyncDatabase,
33
34    /// The identifier for source package files.
35    #[cfg_attr(feature = "serde", serde(rename = "src"))]
36    #[strum(to_string = "src")]
37    SourcePackage,
38}
39
40impl AlpmParser for FileTypeIdentifier {
41    /// Recognizes a [`FileTypeIdentifier`] in a string slice.
42    ///
43    /// # Errors
44    ///
45    /// Returns an error if `input` does not begin with a valid variant
46    /// of a [`FileTypeIdentifier`].
47    fn parser<'a>(input: &mut Input<'a>) -> PResult<'a, Self> {
48        alpha1
49            .try_map(FileTypeIdentifier::from_str)
50            .expected_strings(FileTypeIdentifier::VARIANTS)
51            .layer("file type identifier")
52            .parse_next(input)
53    }
54
55    fn delimiter_error_context<'a, O, P>(
56        parser: P,
57    ) -> impl Parser<Input<'a>, O, ErrMode<ParseStack<'a>>>
58    where
59        P: Parser<Input<'a>, O, ErrMode<ParseStack<'a>>>,
60    {
61        parser
62            .expected_text("a string consisting of alphabetic characters")
63            .layer("file type identifier")
64    }
65}