mc_repack_core/
errors.rs

1use std::{error::Error, fmt::Display, sync::Arc};
2
3pub(crate) type Error_ = anyhow::Error;
4
5/// A struct for collecting errors.
6pub struct ErrorCollector {
7    vec: Option<Vec<EntryRepackError>>,
8    name: Arc<str>,
9}
10impl ErrorCollector {
11    /// Creates a new `ErrorCollector` with a `silent` option.
12    #[must_use]
13    pub fn new(silent: bool) -> Self { Self { vec: (!silent).then(Vec::new), name: "".into() } }
14
15    /// Sets the new prefix name for collected entries. 
16    pub fn rename(&mut self, name: &str)  {
17        self.name = name.into();
18    }
19
20    /// Collects errors for files based on their name (path).
21    pub fn collect(&mut self, name: impl Into<Arc<str>>, e: Error_) {
22        if let Some(vec) = self.vec.as_mut() {
23            vec.push(EntryRepackError {
24                parent: self.name.clone(),
25                name: name.into(),
26                inner: e
27            });
28        }
29    }
30
31    /// Returns all currently gathered results.
32    #[must_use]
33    pub fn results(&self) -> &[EntryRepackError] {
34        self.vec.as_deref().unwrap_or(&[])
35    }
36}
37
38/// An error struct that wraps an inner error thrown while a file was processed. 
39#[derive(Debug)]
40pub struct EntryRepackError {
41    /// A parent path (directory or an archive).
42    pub parent: Arc<str>,
43    /// An associated file name from the error.
44    pub name: Arc<str>,
45    inner: Error_
46}
47impl EntryRepackError {
48    /// Returns the inner error.
49    #[must_use]
50    pub fn inner_error(&self) -> &dyn Error {
51        &*self.inner
52    }
53}
54impl Error for EntryRepackError {
55    fn source(&self) -> Option<&(dyn Error + 'static)> {
56        Some(&*self.inner)
57    }
58}
59impl Display for EntryRepackError {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        write!(f, "{} {}: {}", self.parent, self.name, self.inner)
62    }
63}
64
65/// An error indicating a reason why a file cannot be repacked
66#[derive(Debug, Clone)]
67pub enum FileIgnoreError {
68    /// A processed file is blacklisted.
69    Blacklisted,
70    /// A processed file contains SHA-256 hashes of zipped entries
71    Signfile
72}
73
74impl Error for FileIgnoreError {}
75impl Display for FileIgnoreError {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        f.write_str(match self {
78            Self::Blacklisted => "blacklisted",
79            Self::Signfile => "signfile contains SHA-256 hashes of zipped entries",
80        })
81    }
82}