1use crate::nxt_unit;
2
3#[derive(Debug, Clone, Copy)]
5pub struct UnitInitError;
6
7pub struct UnitError(pub(crate) i32);
9
10impl UnitError {
11 pub fn error() -> Self {
12 Self(nxt_unit::NXT_UNIT_ERROR as i32)
13 }
14}
15
16pub type UnitResult<T> = Result<T, UnitError>;
19
20pub(crate) trait IntoUnitResult {
21 fn into_unit_result(self) -> UnitResult<()>;
22}
23
24impl IntoUnitResult for i32 {
25 fn into_unit_result(self) -> UnitResult<()> {
26 if self == 0 {
27 Ok(())
28 } else {
29 Err(UnitError(self))
30 }
31 }
32}
33
34impl std::error::Error for UnitError {}
35
36impl std::fmt::Debug for UnitError {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 let mut debug_tuple = f.debug_tuple("UnitError");
39
40 debug_tuple.field(&match self.0 as u32 {
41 nxt_unit::NXT_UNIT_OK => "Successful",
42 nxt_unit::NXT_UNIT_AGAIN => "Again",
43 nxt_unit::NXT_UNIT_CANCELLED => "Cancelled",
44 nxt_unit::NXT_UNIT_ERROR => "Error",
45 _ => "Unknown",
46 });
47 debug_tuple.finish()
48 }
49}
50
51impl std::fmt::Display for UnitError {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 match self.0 as u32 {
54 nxt_unit::NXT_UNIT_OK => "Unit error code: Successful.".fmt(f),
55 nxt_unit::NXT_UNIT_AGAIN => "Unit error code: Not yet available, try again.".fmt(f),
56 nxt_unit::NXT_UNIT_CANCELLED => "Unit error code: Cancelled.".fmt(f),
57 nxt_unit::NXT_UNIT_ERROR => "Unit error code: General error.".fmt(f),
58 _ => write!(f, "Unknown Unit error code: {}.", self.0),
59 }
60 }
61}