#[cfg(feature = "std")]
use std::sync::Arc;
use thiserror::Error;
use crate::state_tracker;
#[derive(Debug, Clone, Error)]
#[non_exhaustive]
pub enum Error {
#[cfg(feature = "std")]
#[error("malformed content discovered: {source}")]
MalformedContent {
source: Arc<dyn std::error::Error + Send + Sync>,
},
#[cfg(not(feature = "std"))]
#[error("malformed content discovered")]
MalformedContent,
#[error("bencode encoding corrupted")]
StructureError {
source: state_tracker::StructureError,
},
}
impl Error {
#[cfg(feature = "std")]
pub fn malformed_content<SourceT>(source: SourceT) -> Self
where
SourceT: std::error::Error + Send + Sync + 'static,
{
let error = Arc::new(source);
Error::MalformedContent { source: error }
}
#[cfg(not(feature = "std"))]
pub fn malformed_content<T>(_cause: T) -> Self {
Error::MalformedContent
}
}
impl From<state_tracker::StructureError> for Error {
fn from(error: state_tracker::StructureError) -> Self {
Error::StructureError { source: error }
}
}
#[test]
fn encoding_errors_are_sync_send() {
use crate::encoding::error::Error;
fn is_send<T: Send>() {}
fn is_sync<T: Sync>() {}
is_send::<Error>();
is_sync::<Error>();
}