use compact_encoding::EncodingError;
use thiserror::Error;
use crate::Store;
#[derive(Error, Debug)]
pub enum HypercoreError {
#[error("Bad argument. {context}")]
BadArgument {
context: String,
},
#[error("Hypercore not writable")]
NotWritable,
#[error("Given signature was invalid. {context}")]
InvalidSignature {
context: String,
},
#[error("Invalid checksum. {context}")]
InvalidChecksum {
context: String,
},
#[error("Empty storage: {store}.")]
EmptyStorage {
store: Store,
},
#[error("Corrupt storage: {store}.{}",
.context.as_ref().map_or_else(String::new, |ctx| format!(" Context: {ctx}.")))]
CorruptStorage {
store: Store,
context: Option<String>,
},
#[error("Invalid operation. {context}")]
InvalidOperation {
context: String,
},
#[error("Unrecoverable input/output error occured.{}",
.context.as_ref().map_or_else(String::new, |ctx| format!(" {ctx}.")))]
IO {
context: Option<String>,
#[source]
source: std::io::Error,
},
}
impl From<std::io::Error> for HypercoreError {
fn from(err: std::io::Error) -> Self {
Self::IO {
context: None,
source: err,
}
}
}
impl From<EncodingError> for HypercoreError {
fn from(err: EncodingError) -> Self {
Self::InvalidOperation {
context: format!("Encoding failed: {err}"),
}
}
}