Skip to main content

alpm_db/
error.rs

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