use std::io;
use std::net::AddrParseError;
use std::num::ParseIntError;
use std::string::{FromUtf16Error, FromUtf8Error};
use thiserror::Error;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("Address parse error: {0}")]
AddrParse(#[from] AddrParseError),
#[error("Integer parse error: {0}")]
ParseInt(#[from] ParseIntError),
#[error("UTF-8 parse error: {0}")]
Utf8Parse(#[from] FromUtf8Error),
#[error("UTF-16 parse error: {0}")]
Utf16Parse(#[from] FromUtf16Error),
#[error("System call '{operation}' failed with code {code}")]
SystemCall { operation: String, code: i32 },
#[error("Feature '{feature}' not supported on {platform}")]
UnsupportedPlatform { feature: String, platform: String },
#[error("Failed to access {resource}: {reason}")]
ResourceAccess { resource: String, reason: String },
#[error("Invalid data format in {0}: {1}")]
InvalidFormat(String, String),
#[error("Permission denied accessing {resource}. Try running with elevated privileges.")]
PermissionDenied { resource: String },
#[error("Network interface '{name}' not found")]
InterfaceNotFound { name: String },
#[error("Socket not found: {details}")]
SocketNotFound { details: String },
#[error("Configuration error: {details}")]
ConfigError { details: String },
}
impl Error {
pub fn system_call(operation: impl Into<String>, code: i32) -> Self {
Self::SystemCall {
operation: operation.into(),
code,
}
}
pub fn unsupported_platform(feature: impl Into<String>) -> Self {
Self::UnsupportedPlatform {
feature: feature.into(),
platform: std::env::consts::OS.to_string(),
}
}
pub fn resource_access(resource: impl Into<String>, reason: impl Into<String>) -> Self {
Self::ResourceAccess {
resource: resource.into(),
reason: reason.into(),
}
}
pub fn invalid_format(source: impl Into<String>, details: impl Into<String>) -> Self {
Self::InvalidFormat(source.into(), details.into())
}
pub fn permission_denied(resource: impl Into<String>) -> Self {
Self::PermissionDenied {
resource: resource.into(),
}
}
pub fn interface_not_found(name: impl Into<String>) -> Self {
Self::InterfaceNotFound { name: name.into() }
}
pub fn socket_not_found(details: impl Into<String>) -> Self {
Self::SocketNotFound {
details: details.into(),
}
}
pub fn config_error(details: impl Into<String>) -> Self {
Self::ConfigError {
details: details.into(),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum NetworkError {
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("Parse error: {0}")]
Parse(#[from] ParseIntError),
#[error("Platform error: {0}")]
Platform(String),
#[error("Invalid IP address: {0}")]
InvalidIpAddress(String),
#[error("Invalid MAC address: {0}")]
InvalidMacAddress(String),
#[error("Unsupported platform")]
UnsupportedPlatform,
#[error("Invalid data")]
InvalidData,
#[error("OS error: {0}")]
OsError(i32),
#[error("Interface not found: {0}")]
InterfaceNotFound(String),
#[error("DNS error: {0}")]
Dns(String),
#[error("Not implemented: {0}")]
NotImplemented(String),
}
impl From<NetworkError> for Error {
fn from(err: NetworkError) -> Self {
match err {
NetworkError::Io(e) => Self::Io(e),
NetworkError::Parse(e) => Self::ParseInt(e),
NetworkError::Platform(msg) => Self::SystemCall {
operation: msg,
code: -1,
},
NetworkError::InvalidIpAddress(msg) => {
Self::InvalidFormat("IP address".to_string(), msg)
}
NetworkError::InvalidMacAddress(msg) => {
Self::InvalidFormat("MAC address".to_string(), msg)
}
NetworkError::UnsupportedPlatform => Self::unsupported_platform("socket operation"),
NetworkError::InvalidData => {
Self::InvalidFormat("socket data".to_string(), "invalid format".to_string())
}
NetworkError::OsError(code) => Self::SystemCall {
operation: "socket operation".to_string(),
code,
},
NetworkError::InterfaceNotFound(name) => Self::InterfaceNotFound { name },
NetworkError::Dns(msg) => Self::ConfigError { details: msg },
NetworkError::NotImplemented(msg) => Self::UnsupportedPlatform {
feature: msg,
platform: std::env::consts::OS.to_string(),
},
}
}
}
impl From<Error> for NetworkError {
fn from(err: Error) -> Self {
match err {
Error::Io(e) => Self::Io(e),
Error::ParseInt(e) => Self::Parse(e),
Error::SystemCall { operation, .. } => Self::Platform(operation),
Error::UnsupportedPlatform { .. } => Self::UnsupportedPlatform,
Error::InvalidFormat(_, _) => Self::InvalidData,
Error::InterfaceNotFound { name } => Self::InterfaceNotFound(name),
_ => Self::Platform(err.to_string()),
}
}
}