alpm_db/
error.rs

1//! Error handling.
2
3use std::path::PathBuf;
4
5use crate::desc::SectionKeyword;
6
7/// The error that can occur when working with the ALPM database desc files.
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum Error {
11    /// An [`alpm_types::Error`].
12    #[error(transparent)]
13    AlpmTypes(#[from] alpm_types::Error),
14
15    /// IO error.
16    #[error("I/O error while {0}:\n{1}")]
17    Io(&'static str, std::io::Error),
18
19    /// An I/O error occurred at a path.
20    #[error("I/O error at {path} while {context}:\n{source}")]
21    IoPathError {
22        /// The path at which the error occurred.
23        path: PathBuf,
24        /// The context in which the error occurred.
25        ///
26        /// This is meant to complete the sentence "I/O error at path while ".
27        context: &'static str,
28        /// The source error.
29        source: std::io::Error,
30    },
31
32    /// I/O error while reading a buffer.
33    #[error("Read error while {context}:\n{source}")]
34    IoReadError {
35        /// The context in which the error occurred.
36        ///
37        /// This is meant to complete the sentence "Read error while ".
38        context: &'static str,
39        /// The error source.
40        source: std::io::Error,
41    },
42
43    /// A winnow parser for a type didn't work and produced an error.
44    #[error("Parser failed with the following error:\n{0}")]
45    ParseError(String),
46
47    /// A section is missing in the parsed data.
48    #[error("Missing section: %{0}%")]
49    MissingSection(SectionKeyword),
50
51    /// A section is duplicated in the parsed data.
52    #[error("Duplicate section: %{0}%")]
53    DuplicateSection(SectionKeyword),
54
55    /// No input file given.
56    #[error("No input file given.")]
57    NoInputFile,
58
59    #[cfg(feature = "cli")]
60    /// JSON error.
61    #[error("JSON error while {context}:\n{source}")]
62    Json {
63        /// The context in which the error occurred.
64        ///
65        /// This is meant to complete the sentence "JSON error while ".
66        context: &'static str,
67        /// The error source.
68        source: serde_json::Error,
69    },
70
71    /// Unsupported schema version.
72    #[error("Unsupported schema version: {0}")]
73    UnsupportedSchemaVersion(String),
74
75    /// Failed to parse v1 or v2.
76    #[error("Failed to parse v1 or v2 format")]
77    InvalidFormat,
78}
79
80impl<'a> From<winnow::error::ParseError<&'a str, winnow::error::ContextError>> for Error {
81    /// Converts a [`winnow::error::ParseError`] into an [`Error::ParseError`].
82    fn from(value: winnow::error::ParseError<&'a str, winnow::error::ContextError>) -> Self {
83        Self::ParseError(value.to_string())
84    }
85}