Skip to main content

gistyr_lib/
lib.rs

1//! ------ !//
2//! gistyr-lib
3//! ------ !//
4
5use std::fmt;
6
7pub const SUCCESS_CODE_U64: u64 = 0;
8pub const SUCCESS_CODE_U32: u32 = 0;
9pub const SUCCESS_CODE_U16: u16 = 0;
10pub const SUCCESS_CODE_U8: u8 = 0;
11
12pub const ERROR_CODE_U64: u64 = 1;
13pub const ERROR_CODE_U32: u32 = 1;
14pub const ERROR_CODE_U16: u16 = 1;
15pub const ERROR_CODE_U8: u8 = 1;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum Int {
19    U8(u8),
20    U16(u16),
21    U32(u32),
22    U64(u64),
23}
24impl fmt::Display for Int {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::U8(v)  => write!(f, "{}", v),
28            Self::U16(v) => write!(f, "{}", v),
29            Self::U32(v) => write!(f, "{}", v),
30            Self::U64(v) => write!(f, "{}", v),
31        }
32    }
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum OpCode {
37    Null,
38    Authenticated,
39    NotAuthenticated,
40    Continue,
41    Finished,
42    Early,
43    Deinitialized,
44    Fallback,
45    Refresh,
46    Zero,
47    OneInt(Int),
48    TwoInts(Int, Int),
49    ThreeInts(Int, Int, Int),
50    FourInts(Int, Int, Int, Int),
51    FiveInts(Int, Int, Int, Int, Int),
52    SixInts(Int, Int, Int, Int, Int, Int),
53}
54impl fmt::Display for OpCode {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self {
57            Self::Null => write!(f, "Null"),
58            Self::Authenticated => write!(f, "Authenticated"),
59            Self::NotAuthenticated => write!(f, "NotAuthenticated"),
60            Self::Continue => write!(f, "Continue"),
61            Self::Finished => write!(f, "Finished"),
62            Self::Early => write!(f, "Early"),
63            Self::Deinitialized => write!(f, "Deinitialized"),
64            Self::Fallback => write!(f, "Fallback"),
65            Self::Refresh => write!(f, "Refresh"),
66            Self::Zero => write!(f, "Zero"),
67            Self::OneInt(a) => write!(f, "OneInt({})", a),
68            Self::TwoInts(a, b) => write!(f, "TwoInts({}, {})", a, b),
69            Self::ThreeInts(a, b, c) => write!(f, "ThreeInts({}, {}, {})", a, b, c),
70            Self::FourInts(a, b, c, d) => write!(f, "FourInts({}, {}, {}, {})", a, b, c, d),
71            Self::FiveInts(a, b, c, d, e) => write!(f, "FiveInts({}, {}, {}, {}, {})", a, b, c, d, e),
72            Self::SixInts(a, b, c, d, e, g) => write!(f, "SixInts({}, {}, {}, {}, {}, {})", a, b, c, d, e, g),
73        }
74    }
75}
76
77#[derive(Debug, Clone, PartialEq)]
78pub enum Gistyr {
79    Success(OpCode),
80    Error(String, String, String),
81}
82impl fmt::Display for Gistyr {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        match self {
85            Self::Success(op) => write!(f, "Success({})", op),
86            Self::Error(lib, func, msg) => write!(f, "Error({}, {}, {})", lib, func, msg),
87        }
88    }
89}
90impl Gistyr {
91    pub fn error<L: Into<String>, F: Into<String>, M: Into<String>>(lib: L, func: F, msg: M) -> Self {
92        return Self::Error(lib.into(), func.into(), msg.into());
93    }
94
95    pub fn get_error_message(&self) -> Option<String> {
96        match self {
97            Self::Success(_) => return None,
98            Self::Error(_, _, msg) => return Some(msg.clone()),
99        }
100    }
101}
102
103// -------- //
104// end of file
105// -------- //