Skip to main content

alpm_db/
error.rs

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