alpm_repo_db/
error.rs

1//! Error handling.
2
3use std::path::PathBuf;
4
5use fluent_i18n::t;
6
7use crate::desc::SectionKeyword;
8
9/// The error that can occur when working with the [`alpm-repo-desc`] files.
10///
11/// [`alpm-repo-desc`]: https://alpm.archlinux.page/specifications/alpm-repo-desc.5.html
12#[derive(Debug, thiserror::Error)]
13#[non_exhaustive]
14pub enum Error {
15    /// IO error.
16    #[error("{msg}", msg = t!("error-io", { "context" => context, "source" => source.to_string() }))]
17    Io {
18        /// The context in which the error occurred.
19        ///
20        /// This is meant to complete the sentence "I/O error while ".
21        context: String,
22        /// The source error.
23        source: std::io::Error,
24    },
25
26    /// An I/O error occurred at a path.
27    #[error("{msg}", msg = t!("error-io-path", {
28        "path" => path.display().to_string(),
29        "context" => context,
30        "source" => source.to_string(),
31    }))]
32    IoPathError {
33        /// The path at which the error occurred.
34        path: PathBuf,
35        /// The context in which the error occurred.
36        ///
37        /// This is meant to complete the sentence "I/O error at path while ".
38        context: String,
39        /// The source error.
40        source: std::io::Error,
41    },
42
43    /// I/O error while reading a buffer.
44    #[error("{msg}", msg = t!("error-io-read", { "context" => context, "source" => source.to_string() }))]
45    IoReadError {
46        /// The context in which the error occurred.
47        ///
48        /// This is meant to complete the sentence "Read error while ".
49        context: String,
50        /// The error source.
51        source: std::io::Error,
52    },
53
54    /// A winnow parser for a type didn't work and produced an error.
55    #[error("{msg}", msg = t!("error-parse", { "error" => .0 }))]
56    ParseError(String),
57
58    /// A section is missing in the parsed data.
59    #[error("{msg}", msg = t!("error-missing-section", { "section" => .0.to_string() }))]
60    MissingSection(SectionKeyword),
61
62    /// A section is duplicated in the parsed data.
63    #[error("{msg}", msg = t!("error-duplicate-section", { "section" => .0.to_string() }))]
64    DuplicateSection(SectionKeyword),
65
66    /// A section is invalid for the given schema version.
67    #[error("{msg}", msg = t!("error-invalid-section-for-version", { "section" => section.to_string(), "version" => version.to_string() }))]
68    InvalidSectionForVersion {
69        /// The section keyword.
70        section: SectionKeyword,
71        /// The schema version.
72        version: u8,
73    },
74
75    /// Found an empty section that should either contain value(s) or be omitted.
76    #[error("{msg}", msg = t!("error-empty-section", { "section" => .0.to_string() }))]
77    EmptySection(SectionKeyword),
78
79    /// No input file given.
80    #[error("{msg}", msg = t!("error-no-input-file"))]
81    NoInputFile,
82
83    #[cfg(feature = "cli")]
84    /// JSON error.
85    #[error("{msg}", msg = t!("error-json", { "context" => context, "source" => source.to_string() }))]
86    Json {
87        /// The context in which the error occurred.
88        ///
89        /// This is meant to complete the sentence "JSON error while ".
90        context: String,
91        /// The error source.
92        source: serde_json::Error,
93    },
94
95    /// Unsupported schema version.
96    #[error("{msg}", msg = t!("error-unsupported-schema-version", { "version" => .0 }))]
97    UnsupportedSchemaVersion(String),
98
99    /// Failed to parse v1 or v2.
100    #[error("{msg}", msg = t!("error-invalid-format"))]
101    InvalidFormat,
102}
103
104impl<'a> From<winnow::error::ParseError<&'a str, winnow::error::ContextError>> for Error {
105    /// Converts a [`winnow::error::ParseError`] into an [`Error::ParseError`].
106    fn from(value: winnow::error::ParseError<&'a str, winnow::error::ContextError>) -> Self {
107        Self::ParseError(value.to_string())
108    }
109}