Skip to main content

alpm_repo_db/files/
error.rs

1//! Error handling for alpm-repo-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-repo-files] format.
10///
11/// [alpm-repo-files]: https://alpm.archlinux.page/specifications/alpm-repo-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 [`RepoFiles`][`crate::files::RepoFiles`] 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    /// An I/O error occurred.
26    #[error("{msg}", msg = t!("error-io", { "context" => context, "source" => source.to_string() }))]
27    Io {
28        /// The context in which the error occurred.
29        ///
30        /// This is meant to complete the sentence "I/O error while ".
31        /// See the fluent-i18n token "error-io" for details.
32        context: String,
33        /// The source error.
34        source: std::io::Error,
35    },
36
37    /// An I/O error occurred at a path.
38    #[error(
39        "{msg}",
40        msg = t!(
41            "error-io",
42            {
43                "path" => path.display().to_string(),
44                "context" => context,
45                "source" => source.to_string(),
46            }
47        )
48    )]
49    IoPath {
50        /// The path at which the error occurred.
51        path: PathBuf,
52        /// The context in which the error occurred.
53        ///
54        /// This is meant to complete the sentence "I/O error at path while ".
55        /// See the fluent-i18n token "error-io-path" for details.
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    ParseError(String),
64
65    /// No schema version can be derived from [alpm-repo-files] data.
66    ///
67    /// [alpm-repo-files]: https://alpm.archlinux.page/specifications/alpm-repo-files.5.html
68    #[error("{msg}", msg = t!("error-version-is-unknown"))]
69    UnknownSchemaVersion,
70}
71
72impl<'a> From<ParseError<Input<'a>, ParseStack<'a>>> for Error {
73    /// Converts a [`ParseError`] into an [`Error::ParseError`].
74    fn from(value: ParseError<Input<'a>, ParseStack<'a>>) -> Self {
75        // Only take the **inner** parser error of our own ParseStack error type.
76        // We don't want to hit the `Display` impl of `ParseError`.
77        Self::ParseError(value.into_inner().to_string())
78    }
79}