alpm_srcinfo/pkgbuild_bridge/
error.rs

1//! The error types used in the scope of `alpm-pkgbuild-bridge` output logic.
2
3use alpm_pkgbuild::bridge::Keyword;
4use alpm_types::{Name, SystemArchitecture};
5use thiserror::Error;
6use winnow::error::{ContextError, ParseError};
7
8#[cfg(doc)]
9use crate::SourceInfo;
10
11/// A lower-level error that may occur when converting `alpm-pkgbuild-bridge` script output into the
12/// [`SourceInfo`] format.
13#[derive(Debug, Error)]
14pub enum BridgeError {
15    /// ALPM type parse error
16    #[error(transparent)]
17    AlpmType(#[from] alpm_types::Error),
18
19    /// No `pkgname` has been specified.
20    #[error("No 'pkgname' has been specified. At least one must be given.")]
21    NoName,
22
23    /// A package name is not valid.
24    #[error("The package name '{name}' is not valid:\n{error}")]
25    InvalidPackageName {
26        /// The invalid package name.
27        name: String,
28        /// The source error.
29        error: alpm_types::Error,
30    },
31
32    /// A `package` function has been declared for a split package, but it is not defined in
33    /// `pkgname`.
34    #[error(
35        "The split package '{0}' is not declared in pkgname, but a package function is present for it."
36    )]
37    UndeclaredPackageName(String),
38
39    /// An unused package function exists for an undeclared [alpm-split-package].
40    ///
41    /// [alpm-split-package]: https://alpm.archlinux.page/specifications/alpm-split-package.7.html
42    #[error("An unused package function exists for undeclared split package: '{0}'")]
43    UnusedPackageFunction(Name),
44
45    /// A type parser fails on a certain keyword.
46    #[error("Missing keyword: '{keyword}'")]
47    MissingRequiredKeyword {
48        /// The keyword that cannot be parsed.
49        keyword: Keyword,
50    },
51
52    /// A type parser fails on a certain keyword.
53    #[error("Failed to parse input for keyword '{keyword}':\n{error}")]
54    ParseError {
55        /// The keyword that cannot be parsed.
56        keyword: Keyword,
57        /// The error message.
58        error: String,
59    },
60
61    /// A variable is expected to be of a different type.
62    /// E.g. `String` when an `Array` is expected.
63    #[error(
64        "Got wrong variable type for keyword '{keyword}'. Expected a {expected}, got a {actual}"
65    )]
66    WrongVariableType {
67        /// The name of the keyword for which a wrong variable type is used.
68        keyword: String,
69        /// The expected type of variable.
70        expected: String,
71        /// The actual type of variable.
72        actual: String,
73    },
74
75    /// A keyword has an architecture suffix even though it shouldn't have one.
76    #[error("Found unexpected architecture suffix '{suffix}' for keyword '{keyword}'")]
77    UnexpectedArchitecture {
78        /// The keyword for which an unexpected architecture suffix is found.
79        keyword: Keyword,
80        /// The architecture that is found for the `keyword`.
81        suffix: SystemArchitecture,
82    },
83
84    /// A keyword that cannot be cleared is attempted to be cleared.
85    #[error("Tried to clear value for keyword '{keyword}', which is not allowed.")]
86    UnclearableValue {
87        /// The keyword that is attempted to be cleared.
88        keyword: Keyword,
89    },
90
91    /// A keyword should have only a single value, but an array is found.
92    #[error(
93        "Found array of values for keyword '{keyword}' that expects a single value:\n{}",
94        values.iter().map(|s| format!("\"{s}\"")).collect::<Vec<String>>().join(", ")
95    )]
96    UnexpectedArray {
97        /// The keyword for which a single value should be used.
98        keyword: Keyword,
99        /// The values that are used for the `keyword`.
100        values: Vec<String>,
101    },
102}
103
104impl<'a> From<(Keyword, ParseError<&'a str, ContextError>)> for BridgeError {
105    /// Converts a tuple of ([`Keyword`] and [`ParseError`]) into a [`BridgeError::ParseError`].
106    fn from(value: (Keyword, ParseError<&'a str, ContextError>)) -> Self {
107        Self::ParseError {
108            keyword: value.0,
109            error: value.1.to_string(),
110        }
111    }
112}