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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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>;