use std::{error::Error, fmt::Display, sync::Arc};
pub(crate) type Error_ = anyhow::Error;
pub struct ErrorCollector {
vec: Option<Vec<EntryRepackError>>,
name: Arc<str>,
}
impl ErrorCollector {
#[must_use]
pub fn new(silent: bool) -> Self { Self { vec: (!silent).then(Vec::new), name: "".into() } }
pub fn rename(&mut self, name: &str) {
self.name = name.into();
}
pub fn collect(&mut self, name: impl Into<Arc<str>>, e: Error_) {
if let Some(vec) = self.vec.as_mut() {
vec.push(EntryRepackError {
parent: self.name.clone(),
name: name.into(),
inner: e
});
}
}
#[must_use]
pub fn results(&self) -> &[EntryRepackError] {
self.vec.as_deref().unwrap_or(&[])
}
}
#[derive(Debug)]
pub struct EntryRepackError {
pub parent: Arc<str>,
pub name: Arc<str>,
inner: Error_
}
impl EntryRepackError {
#[must_use]
pub fn inner_error(&self) -> &dyn Error {
&*self.inner
}
}
impl Error for EntryRepackError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&*self.inner)
}
}
impl Display for EntryRepackError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}: {}", self.parent, self.name, self.inner)
}
}
#[derive(Debug, Clone)]
pub enum FileIgnoreError {
Blacklisted,
Signfile
}
impl Error for FileIgnoreError {}
impl Display for FileIgnoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Blacklisted => "blacklisted",
Self::Signfile => "signfile contains SHA-256 hashes of zipped entries",
})
}
}