Skip to main content

alpm_db/files/
error.rs

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