netstack: Merge the redox_netstack lib into smolnetd
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
use std::convert;
|
||||
use std::fmt;
|
||||
use std::io::Error as IOError;
|
||||
use std::result;
|
||||
use syscall::error::Error as SyscallError;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ErrorType {
|
||||
Syscall(SyscallError),
|
||||
IOError(IOError),
|
||||
Other,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
error_type: ErrorType,
|
||||
descr: String,
|
||||
}
|
||||
|
||||
pub trait ResultExt {
|
||||
type T;
|
||||
fn context(self, ctxt: impl Into<String>) -> std::result::Result<Self::T, Error>;
|
||||
}
|
||||
impl<T> ResultExt for std::result::Result<T, SyscallError> {
|
||||
type T = T;
|
||||
fn context(self, ctxt: impl Into<String>) -> std::result::Result<T, Error> {
|
||||
self.map_err(|err| Error::from_syscall_error(err, ctxt))
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn from_syscall_error<S: Into<String>>(syscall_error: SyscallError, descr: S) -> Error {
|
||||
Error {
|
||||
error_type: ErrorType::Syscall(syscall_error),
|
||||
descr: descr.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_io_error<S: Into<String>>(io_error: IOError, descr: S) -> Error {
|
||||
Error {
|
||||
error_type: ErrorType::IOError(io_error),
|
||||
descr: descr.into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn other_error<S: Into<String>>(descr: S) -> Error {
|
||||
Error {
|
||||
error_type: ErrorType::Other,
|
||||
descr: descr.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
|
||||
match self.error_type {
|
||||
ErrorType::Syscall(ref syscall_error) => {
|
||||
write!(f, "{}: syscall error: {}", self.descr, syscall_error)
|
||||
}
|
||||
ErrorType::IOError(ref io_error) => {
|
||||
write!(f, "{} : io error : {}", self.descr, io_error)
|
||||
}
|
||||
ErrorType::Other => {
|
||||
write!(f, "{}", self.descr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl convert::From<IOError> for Error {
|
||||
fn from(e: IOError) -> Self {
|
||||
Error::from_io_error(e, "")
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<T> = result::Result<T, Error>;
|
||||
Reference in New Issue
Block a user