use crate::error_event::ErrorEventData; use thiserror::Error; use tokio::sync::oneshot; #[cfg(feature = "encryption_layer")] use x25519_dalek::PublicKey; pub type Result = std::result::Result; #[derive(Error, Debug)] pub enum Error { #[error(transparent)] IoError(#[from] tokio::io::Error), #[cfg(feature = "serialize")] #[error("failed to serialize event: {0}")] Serialization(#[from] crate::payload::SerializationError), #[error("build Error: {0}")] BuildError(String), #[error("{0}")] Message(String), #[error("channel Error: {0}")] ReceiveError(#[from] oneshot::error::RecvError), #[error("the received event was corrupted")] CorruptedEvent, #[error("send Error")] SendError, #[error("received error response: {0}")] ErrorEvent(#[from] ErrorEventData), #[error("timed out")] Timeout, #[error("Unsupported API Version {0}")] UnsupportedVersion(String), #[error("Invalid state")] InvalidState, #[cfg(feature = "encryption_layer")] #[error("Connection of unknown peer with key {0:?} refused")] UnknownPeer(PublicKey), } impl Error { pub fn unsupported_version_vec(version: Vec) -> Self { let mut version_string = version .into_iter() .fold(String::new(), |acc, val| format!("{}{}.", acc, val)); version_string.pop(); Self::UnsupportedVersion(version_string) } } impl From for Error { fn from(s: String) -> Self { Error::Message(s) } } impl From<&str> for Error { fn from(s: &str) -> Self { Error::Message(s.to_string()) } }