alpm_db/
error.rs

1//! Error handling.
2
3use std::path::PathBuf;
4
5use fluent_i18n::t;
6
7use crate::desc::SectionKeyword;
8
9/// The error that can occur when working with the ALPM database desc files.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13    /// An [`alpm_types::Error`].
14    #[error("{msg}", msg = t!("error-alpm-types", { "source" => .0.to_string() }))]
15    AlpmTypes(#[from] alpm_types::Error),
16
17    /// IO error.
18    #[error("{msg}", msg = t!("error-io", { "context" => context, "source" => source.to_string() }))]
19    Io {
20        /// The context in which the error occurred.
21        ///
22        /// This is meant to complete the sentence "I/O error while ".
23        context: String,
24        /// The source error.
25        source: std::io::Error,
26    },
27
28    /// An I/O error occurred at a path.
29    #[error("{msg}", msg = t!("error-io-path", {
30        "path" => path.display().to_string(),
31        "context" => context,
32        "source" => source.to_string(),
33    }))]
34    IoPathError {
35        /// The path at which the error occurred.
36        path: PathBuf,
37        /// The context in which the error occurred.
38        ///
39        /// This is meant to complete the sentence "I/O error at path while ".
40        context: String,
41        /// The source error.
42        source: std::io::Error,
43    },
44
45    /// I/O error while reading a buffer.
46    #[error("{msg}", msg = t!("error-io-read", { "context" => context, "source" => source.to_string() }))]
47    IoReadError {
48        /// The context in which the error occurred.
49        ///
50        /// This is meant to complete the sentence "Read error while ".
51        context: String,
52        /// The error source.
53        source: std::io::Error,
54    },
55
56    /// A winnow parser for a type didn't work and produced an error.
57    #[error("{msg}", msg = t!("error-parse", { "error" => .0 }))]
58    ParseError(String),
59
60    /// A section is missing in the parsed data.
61    #[error("{msg}", msg = t!("error-missing-section", { "section" => .0.to_string() }))]
62    MissingSection(SectionKeyword),
63
64    /// A section is duplicated in the parsed data.
65    #[error("{msg}", msg = t!("error-duplicate-section", { "section" => .0.to_string() }))]
66    DuplicateSection(SectionKeyword),
67
68    /// No input file given.
69    #[error("{msg}", msg = t!("error-no-input-file"))]
70    NoInputFile,
71
72    #[cfg(feature = "cli")]
73    /// JSON error.
74    #[error("{msg}", msg = t!("error-json", { "context" => context, "source" => source.to_string() }))]
75    Json {
76        /// The context in which the error occurred.
77        ///
78        /// This is meant to complete the sentence "JSON error while ".
79        context: String,
80        /// The error source.
81        source: serde_json::Error,
82    },
83
84    /// Unsupported schema version.
85    #[error("{msg}", msg = t!("error-unsupported-schema-version", { "version" => .0 }))]
86    UnsupportedSchemaVersion(String),
87
88    /// Failed to parse v1 or v2.
89    #[error("{msg}", msg = t!("error-invalid-format"))]
90    InvalidFormat,
91}
92
93impl<'a> From<winnow::error::ParseError<&'a str, winnow::error::ContextError>> for Error {
94    /// Converts a [`winnow::error::ParseError`] into an [`Error::ParseError`].
95    fn from(value: winnow::error::ParseError<&'a str, winnow::error::ContextError>) -> Self {
96        Self::ParseError(value.to_string())
97    }
98}