use std::{self, error, fmt};
pub type NcIntResult = i32;
pub const NCRESULT_OK: i32 = 0;
pub const NCRESULT_ERR: i32 = -1;
pub const NCRESULT_MAX: i32 = i32::MAX;
#[derive(Debug, Clone, Default)]
pub struct NcError {
pub int: i32,
pub msg: String,
}
impl fmt::Display for NcError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "NcError {}: {}", self.int, self.msg)
}
}
impl error::Error for NcError {
fn description(&self) -> &str {
&self.msg
}
}
impl NcError {
pub fn new() -> Self {
Self {
int: NCRESULT_ERR,
..Default::default()
}
}
pub fn new_err(int: NcIntResult) -> Self {
Self {
int,
..Default::default()
}
}
pub fn new_msg(msg: &str) -> Self {
Self {
int: NCRESULT_ERR,
msg: msg.to_string(),
}
}
pub fn with_msg(int: NcIntResult, msg: &str) -> Self {
Self {
int,
msg: msg.to_string(),
}
}
}
pub type NcResult<T> = Result<T, NcError>;