#![ allow (dead_code) ] use crate::prelude::*; pub(crate) mod exports { #[ cfg (feature = "hss-errors") ] pub use super::{ ServerError, ServerResult, ResultExtPanic, ErrorExtPanic, ResultExtWrap, ResultExtWrapFrom, ErrorExtWrap, ErrorExtWrapFrom, error_with_format, error_with_message, error_with_code, error_wrap, panic_with_format, panic_with_message, panic_with_code, }; } pub type ServerError = io::Error; pub type ServerResult = Result; pub trait ResultExtPanic : Sized { fn or_panic (self, _code : u32) -> V; fn infallible (self, _code : u32) -> V; } impl ResultExtPanic for Result { fn or_panic (self, _code : u32) -> V { match self { Ok (_value) => _value, Err (_error) => _error.panic (_code), } } fn infallible (self, _code : u32) -> V { match self { Ok (_value) => _value, Err (_error) => _error.panic (_code), } } } impl ResultExtPanic for Option { fn or_panic (self, _code : u32) -> V { match self { Some (_value) => _value, None => panic_with_code (_code), } } fn infallible (self, _code : u32) -> V { match self { Some (_value) => _value, None => panic_with_code (_code), } } } pub trait ErrorExtPanic : Sized { fn panic (self, _code : u32) -> !; } impl ErrorExtPanic for E { fn panic (self, _code : u32) -> ! { panic! ("[{:08x}] unexpected error encountered! // {}", _code, self); } } pub trait ResultExtWrap : Sized { fn or_wrap (self, _code : u32) -> Result; } impl ResultExtWrap for Result { fn or_wrap (self, _code : u32) -> Result { match self { Ok (_value) => Ok (_value), Err (_error) => Err (io::Error::wrap_from (_code, _error)), } } } impl ResultExtWrap for Option { fn or_wrap (self, _code : u32) -> Result { if let Some (_value) = self { Ok (_value) } else { Err (error_with_code (_code)) } } } pub trait ResultExtWrapFrom : Sized { fn or_wrap_from (_code : u32, _result : Result) -> Self; } impl > ResultExtWrapFrom for Result { fn or_wrap_from (_code : u32, _result : Result) -> Result { match _result { Ok (_value) => Ok (_value), Err (_error) => Err (EX::wrap_from (_code, _error)), } } } pub trait ErrorExtWrapFrom : Sized { fn wrap_from (_code : u32, _error : E) -> Self; } impl ErrorExtWrapFrom for io::Error { fn wrap_from (_code : u32, _error : E) -> Self { io::Error::new (io::ErrorKind::Other, format! ("[{:08x}] {}", _code, _error)) } } pub trait ErrorExtWrap : Sized { fn wrap (self, _code : u32) -> E; } impl > ErrorExtWrap for EI { fn wrap (self, _code : u32) -> EO { EO::wrap_from (_code, self) } } pub fn error_with_format (_code : u32, _message : fmt::Arguments<'_>) -> io::Error { io::Error::new (io::ErrorKind::Other, format! ("[{:08x}] {}", _code, _message)) } pub fn error_with_message (_code : u32, _message : &str) -> io::Error { if ! _message.is_empty () { io::Error::new (io::ErrorKind::Other, format! ("[{:08x}] {}", _code, _message)) } else { error_with_code (_code) } } pub fn error_with_code (_code : u32) -> io::Error { io::Error::new (io::ErrorKind::Other, format! ("[{:08x}] unexpected error encountered!", _code)) } pub fn error_wrap (_code : u32, _error : E) -> io::Error { io::Error::wrap_from (_code, _error) } pub fn panic_with_format (_code : u32, _message : fmt::Arguments<'_>) -> ! { panic! ("[{:08x}] {}", _code, _message) } pub fn panic_with_message (_code : u32, _message : &str) -> ! { if ! _message.is_empty () { panic! ("[{:08x}] {}", _code, _message) } else { panic_with_code (_code) } } pub fn panic_with_code (_code : u32) -> ! { panic! ("[{:08x}] unexpected error encountered!", _code) } #[ cfg (feature = "hss-errors") ] #[ macro_export ] macro_rules! fail_with_format { ( $_code : literal, $_format : literal, $( $_argument : tt )* ) => { return ::std::result::Result::Err ($crate::error_with_format ($_code, ::std::format_args! ($_format, $( $_argument )* ))) } } #[ cfg (feature = "hss-errors") ] #[ macro_export ] macro_rules! fail_with_message { ( $_code : literal, $_message : literal ) => { return ::std::result::Result::Err ($crate::error_with_message ($_code, $_message)) }; } #[ cfg (feature = "hss-errors") ] #[ macro_export ] macro_rules! fail_with_code { ( $_code : literal ) => { return ::std::result::Result::Err ($crate::error_with_code ($_code)) }; } #[ cfg (feature = "hss-errors") ] #[ macro_export ] macro_rules! fail_wrap { ( $_code : literal, $_error : expr ) => { return ::std::result::Result::Err ($crate::error_wrap ($_code, $_error)) }; }