alpm_db/files/
error.rs

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