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