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