use std::{io, net::Ipv4Addr};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum MctxError {
#[error("MCTX: invalid destination port")]
InvalidDestinationPort,
#[error("MCTX: group must be a multicast IPv4 address")]
InvalidMulticastGroup,
#[error("MCTX: invalid source port")]
InvalidSourcePort,
#[error("MCTX: invalid source address")]
InvalidSourceAddress,
#[error("MCTX: invalid interface address")]
InvalidInterfaceAddress,
#[error("MCTX: publication already exists")]
DuplicatePublication,
#[error("MCTX: publication not found")]
PublicationNotFound,
#[error("MCTX: failed to create UDP socket: {0}")]
SocketCreateFailed(io::Error),
#[error("MCTX: failed to set socket option: {0}")]
SocketOptionFailed(io::Error),
#[error("MCTX: failed to bind UDP socket: {0}")]
SocketBindFailed(io::Error),
#[error("MCTX: failed to connect UDP socket: {0}")]
SocketConnectFailed(io::Error),
#[error("MCTX: failed to read local address from socket: {0}")]
SocketLocalAddrFailed(io::Error),
#[error("MCTX: existing socket must be an IPv4 UDP socket")]
ExistingSocketMustBeIpv4,
#[error("MCTX: existing socket is bound to UDP port {actual}, expected {expected}")]
ExistingSocketPortMismatch { expected: u16, actual: u16 },
#[error("MCTX: existing socket is bound to local IPv4 address {actual}, expected {expected}")]
ExistingSocketAddressMismatch {
expected: Ipv4Addr,
actual: Ipv4Addr,
},
#[error("MCTX: send failed: {0}")]
SendFailed(io::Error),
}
impl MctxError {
#[cfg(feature = "tokio")]
pub(crate) fn is_would_block(&self) -> bool {
matches!(self, Self::SendFailed(error) if error.kind() == io::ErrorKind::WouldBlock)
}
}