use super::NnErrno;
use core::fmt;
use core::num::NonZeroU16;
#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct Error {
code: NonZeroU16,
}
impl Error {
pub fn from_raw_error(error: NnErrno) -> Option<Error> {
Some(Error {
code: NonZeroU16::new(error)?,
})
}
pub fn raw_error(&self) -> u16 {
self.code.get()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (error {})", strerror(self.code.get()), self.code)?;
Ok(())
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Error")
.field("code", &self.code)
.field("message", &strerror(self.code.get()))
.finish()
}
}
fn strerror(code: u16) -> &'static str {
match code {
super::NN_ERRNO_SUCCESS => "No error occurred.",
super::NN_ERRNO_INVALID_ARGUMENT => "Caller module passed an invalid argument.",
super::NN_ERRNO_MISSING_MEMORY => "Caller module is missing a memory export.",
super::NN_ERRNO_BUSY => "Device or resource busy.",
_ => "Unknown error.",
}
}
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_from_success_code() {
assert_eq!(None, Error::from_raw_error(0));
}
#[test]
fn error_from_invalid_argument_code() {
assert_eq!(
"Caller module passed an invalid argument. (error 1)",
Error::from_raw_error(1).unwrap().to_string()
);
}
}