Skip to main content

alpm_mtree/
error.rs

1use std::{collections::HashSet, path::PathBuf, string::FromUtf8Error};
2
3use fluent_i18n::t;
4
5#[cfg(doc)]
6use crate::Mtree;
7use crate::mtree::path_validation_error::PathValidationErrors;
8
9/// The Error that can occur when working with ALPM-MTREE.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13    /// An alpm-common error.
14    #[error(transparent)]
15    AlpmCommon(#[from] alpm_common::Error),
16
17    /// The ALPM-MTREE data is not gzip compressed.
18    #[error("{msg}", msg = t!("error-data-is-not-gzip"))]
19    DataIsNotGzip,
20
21    /// There are duplicate paths.
22    #[error("{msg}", msg = t!("error-duplicate-paths", {
23        "paths" => paths.iter()
24            .map(|p| format!("{p:?}"))
25            .collect::<Vec<_>>()
26            .join("\n")
27    }))]
28    DuplicatePaths {
29        /// The set of file system paths that are duplicates.
30        paths: HashSet<PathBuf>,
31    },
32
33    /// File creation error.
34    #[cfg(feature = "creation")]
35    #[error("{msg}", msg = t!("error-file-creation", { "source" => .0.to_string() }))]
36    File(#[from] crate::CreationError),
37
38    /// IO error.
39    #[error("{msg}", msg = t!("error-io", {
40        "context" => context,
41        "source" => source.to_string()
42    }))]
43    Io {
44        /// The context of the error.
45        context: String,
46        /// The underlying IO error.
47        source: std::io::Error,
48    },
49
50    /// IO error with additional path info for more context.
51    #[error("{msg}", msg = t!("error-io-path", {
52        "path" => path,
53        "context" => context,
54        "source" => source.to_string()
55    }))]
56    IoPath {
57        /// The path related to the error.
58        path: PathBuf,
59        /// The context of the error.
60        context: String,
61        /// The underlying IO error.
62        source: std::io::Error,
63    },
64
65    /// UTF-8 parse error.
66    #[error(transparent)]
67    InvalidUTF8(#[from] FromUtf8Error),
68
69    /// An error occurred while unpacking a gzip file.
70    #[error("{msg}", msg = t!("error-invalid-gzip", { "source" => .0.to_string() }))]
71    InvalidGzip(std::io::Error),
72
73    /// Validating paths in a base directory using [`Mtree`] data led to one or more errors.
74    #[error(transparent)]
75    PathValidation(#[from] PathValidationErrors),
76
77    /// A parsing error that occurred during the winnow file parsing.
78    #[error("{msg}", msg = t!("error-parse", { "source" => .0 }))]
79    Parse(String),
80
81    /// An error occurred during the interpretation phase of the language.
82    #[error("{msg}", msg = t!("error-interpreter", {
83        "line_number" => .0.to_string(),
84        "line" => .1,
85        "reason" => .2,
86    }))]
87    InterpreterError(usize, String, String),
88
89    /// Unsupported schema version
90    #[error("Unsupported schema version: {0}")]
91    UnsupportedSchemaVersion(String),
92}