1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Primary error type used by the library

use alloc::string::FromUtf8Error;
use alloc::string::String;
use alloc::borrow::ToOwned;
use alloc::string::ToString;

/// The error type used for errors in this library
#[derive(Debug)]
pub enum NP_Error {
    /// Read only error
    MemoryReadOnly,
    /// Use this instead of unreachable! which causes panic
    Unreachable,
    /// Too many recursive calls
    RecursionLimit,
    /// Custom error message
    Custom { 
        /// Error message
        message: String 
    }
}

impl NP_Error {
    /// Generate a new error with a specific message
    pub fn new<S: AsRef<str>>(message: S) -> Self {
        NP_Error::Custom { message: message.as_ref().to_owned() }
    }
    /// Convert an option to an error type
    pub fn unwrap<T>(value: Option<T>) -> Result<T, NP_Error> {
        match value {
            Some(x) => Ok(x),
            None => Err(NP_Error::new("Missing Value in option!"))
        }
    }
}

impl From<FromUtf8Error> for NP_Error {
    fn from(err: FromUtf8Error) -> NP_Error {
        NP_Error::new(err.to_string().as_str())
    }
}

impl From<core::num::ParseFloatError> for NP_Error {
    fn from(err: core::num::ParseFloatError) -> NP_Error {
        NP_Error::new(err.to_string().as_str())
    }
}

impl From<core::num::ParseIntError> for NP_Error {
    fn from(err: core::num::ParseIntError) -> NP_Error {
        NP_Error::new(err.to_string().as_str())
    }
}