use crate::{discovery, groups, streams};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Missing network ID")]
MissingNetworkId,
#[error("Bind error: {0}")]
Bind(#[from] iroh::endpoint::BindError),
#[error("Invalid address: {0}")]
InvalidAddress(#[from] InvalidSocketAddr),
#[error("Discovery config error: {0}")]
DiscoveryConfig(#[from] discovery::ConfigBuilderError),
#[error("Streams config error: {0}")]
StreamsConfig(#[from] streams::ConfigBuilderError),
#[error("Groups config error: {0}")]
GroupsConfig(#[from] groups::ConfigBuilderError),
}
pub trait CloseReason:
std::error::Error
+ Into<iroh::endpoint::ApplicationClose>
+ PartialEq<iroh::endpoint::ApplicationClose>
+ Clone
+ Sized
+ Send
+ Sync
+ 'static
{
}
macro_rules! make_close_reason {
($(#[$meta:meta])* $vis:vis struct $name:ident, $code:expr) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, thiserror::Error)]
#[error("{}", stringify!($name))]
#[allow(clippy::derive_partial_eq_without_eq)]
$vis struct $name;
const _: () = {
#[automatically_derived]
impl From<$name> for iroh::endpoint::ApplicationClose {
fn from(_: $name) -> Self {
iroh::endpoint::ApplicationClose {
error_code: iroh::endpoint::VarInt::from($code as u32),
reason: stringify!($name).into(),
}
}
}
#[automatically_derived]
impl PartialEq<iroh::endpoint::ApplicationClose> for $name {
fn eq(&self, other: &iroh::endpoint::ApplicationClose) -> bool {
other.error_code == iroh::endpoint::VarInt::from($code as u32)
}
}
#[automatically_derived]
impl PartialEq<$name> for iroh::endpoint::ApplicationClose {
fn eq(&self, _: &$name) -> bool {
self.error_code == iroh::endpoint::VarInt::from($code as u32)
}
}
#[automatically_derived]
impl PartialEq<$name> for &iroh::endpoint::ApplicationClose {
fn eq(&self, _: &$name) -> bool {
self.error_code == iroh::endpoint::VarInt::from($code as u32)
}
}
#[automatically_derived]
impl crate::network::error::CloseReason for $name { }
};
};
}
use iroh::endpoint::InvalidSocketAddr;
pub(crate) use make_close_reason;
make_close_reason!(
pub(crate) struct Success, 200);
make_close_reason!(
pub(crate) struct GracefulShutdown, 204);
make_close_reason!(
pub(crate) struct InvalidAlpn, 100);
make_close_reason!(
pub(crate) struct DifferentNetwork, 101);
make_close_reason!(
pub(crate) struct Cancelled, 102);
make_close_reason!(
pub(crate) struct UnexpectedClose, 103);
make_close_reason!(
pub(crate) struct ProtocolViolation, 400);
make_close_reason!(
pub(crate) struct UnknownPeer, 401);