Skip to main content

alpm_db/files/
error.rs

1//! Error handling for alpm-db-files.
2
3use std::path::PathBuf;
4
5use alpm_parsers::error::{Input, ParseStack};
6use fluent_i18n::t;
7use winnow::error::ParseError;
8
9/// The error that can occur when working with the [alpm-db-files] format.
10///
11/// [alpm-db-files]: https://alpm.archlinux.page/specifications/alpm-db-files.5.html
12#[derive(Debug, thiserror::Error)]
13pub enum Error {
14    /// An [`alpm_common::Error`] occurred.
15    #[error(transparent)]
16    AlpmCommon(#[from] alpm_common::Error),
17
18    /// One or more invalid paths for a [`DbFiles`][`crate::files::DbFiles`] are encountered.
19    #[error("{msg}", msg = t!("error-invalid-files-paths", { "message" => message }))]
20    InvalidFilesPaths {
21        /// An error message that explains which paths are invalid and why.
22        message: String,
23    },
24
25    /// One or more invalid backup entries are encountered.
26    #[error(
27        "{msg}",
28        msg = t!(
29            "error-invalid-backup-entries",
30            { "message" => message }
31        )
32    )]
33    InvalidBackupEntries {
34        /// An error message that explains which backup entries are invalid and why.
35        message: String,
36    },
37
38    /// An I/O error occurred.
39    #[error("{msg}", msg = t!("error-io", { "context" => context, "source" => source.to_string() }))]
40    Io {
41        /// The context in which the error occurred.
42        ///
43        /// This is meant to complete the sentence "I/O error while ".
44        /// See the fluent-i18n token "error-io" for details.
45        context: String,
46        /// The source error.
47        source: std::io::Error,
48    },
49
50    /// An I/O error occurred at a path.
51    #[error(
52        "{msg}",
53        msg = t!(
54            "error-io",
55            {
56                "path" => path.display().to_string(),
57                "context" => context,
58                "source" => source.to_string(),
59            }
60        )
61    )]
62    IoPath {
63        /// The path at which the error occurred.
64        path: PathBuf,
65        /// The context in which the error occurred.
66        ///
67        /// This is meant to complete the sentence "I/O error at path while ".
68        /// See the fluent-i18n token "error-io-path" for details.
69        context: String,
70        /// The source error.
71        source: std::io::Error,
72    },
73
74    /// A winnow parser for a type didn't work and produced an error.
75    #[error("{msg}", msg = t!("error-parse", { "error" => .0 }))]
76    ParseError(String),
77
78    /// No schema version can be derived from [alpm-db-files] data.
79    ///
80    /// [alpm-db-files]: https://alpm.archlinux.page/specifications/alpm-db-files.5.html
81    #[error("{msg}", msg = t!("error-version-is-unknown"))]
82    UnknownSchemaVersion,
83}
84
85impl<'a> From<ParseError<Input<'a>, ParseStack<'a>>> for Error {
86    /// Converts a [`ParseError`] into an [`Error::ParseError`].
87    fn from(value: ParseError<Input<'a>, ParseStack<'a>>) -> Self {
88        // Only take the **inner** parser error of our own ParseStack error type.
89        // We don't want to hit the `Display` impl of `ParseError`.
90        Self::ParseError(value.into_inner().to_string())
91    }
92}