Skip to main content

alpm_repo_db/files/
error.rs

1//! Error handling for alpm-repo-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-repo-files] format.
9///
10/// [alpm-repo-files]: https://alpm.archlinux.page/specifications/alpm-repo-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 [`RepoFiles`][`crate::files::RepoFiles`] 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    /// An I/O error occurred.
25    #[error("{msg}", msg = t!("error-io", { "context" => context, "source" => source.to_string() }))]
26    Io {
27        /// The context in which the error occurred.
28        ///
29        /// This is meant to complete the sentence "I/O error while ".
30        /// See the fluent-i18n token "error-io" for details.
31        context: String,
32        /// The source error.
33        source: std::io::Error,
34    },
35
36    /// An I/O error occurred at a path.
37    #[error(
38        "{msg}",
39        msg = t!(
40            "error-io",
41            {
42                "path" => path.display().to_string(),
43                "context" => context,
44                "source" => source.to_string(),
45            }
46        )
47    )]
48    IoPath {
49        /// The path at which the error occurred.
50        path: PathBuf,
51        /// The context in which the error occurred.
52        ///
53        /// This is meant to complete the sentence "I/O error at path while ".
54        /// See the fluent-i18n token "error-io-path" for details.
55        context: String,
56        /// The source error.
57        source: std::io::Error,
58    },
59
60    /// A winnow parser for a type didn't work and produced an error.
61    #[error("{msg}", msg = t!("error-parse", { "error" => .0 }))]
62    ParseError(String),
63
64    /// No schema version can be derived from [alpm-repo-files] data.
65    ///
66    /// [alpm-repo-files]: https://alpm.archlinux.page/specifications/alpm-repo-files.5.html
67    #[error("{msg}", msg = t!("error-version-is-unknown"))]
68    UnknownSchemaVersion,
69}
70
71impl<'a> From<ParseError<&'a str, ContextError>> for Error {
72    /// Converts a [`ParseError`] into an [`Error::ParseError`].
73    fn from(value: ParseError<&'a str, ContextError>) -> Self {
74        Self::ParseError(value.to_string())
75    }
76}