1#![deny(warnings)]
3
4#[derive(Copy, Clone, Debug, defmt::Format)]
5pub struct Error {
6 pub ebytes: [u8; 8],
7}
8
9#[derive(Copy, Clone, Debug, PartialEq, defmt::Format)]
10#[repr(u8)]
11pub enum ErrorCode {
12 CheckPoint = 0, NotEnoughBytes = 1,
14 NameTableFull = 2,
15 SearchFail = 3,
16 NoExist = 4,
17 NotResolved = 5,
18 NotLocal = 6,
19 NoSpace = 7,
20 NotEnabled = 8,
21 InvalidId = 9,
22}
23
24pub fn mkerr_base(file_code: u8, code: u8, line: u32, detail: u32) -> crate::error::Error {
25 let mut ebytes = [0u8; 8];
26 ebytes[0] = file_code;
27 ebytes[1] = code as u8;
28 ebytes[2] = (line & 0xFF) as u8;
29 ebytes[3] = ((line >> 8) & 0xFF) as u8;
30 ebytes[4] = (detail & 0xFF) as u8;
31 ebytes[5] = ((detail >> 8) & 0xFF) as u8;
32 ebytes[6] = ((detail >> 16) & 0xFF) as u8;
33 ebytes[7] = ((detail >> 24) & 0xFF) as u8;
34 crate::error::Error { ebytes }
35}
36
37pub fn mkerr_ptr(
38 file_code: u8,
39 code: u8,
40 line: u32,
41 detail_ptr: *const u8,
42 detail_size: usize,
43) -> crate::error::Error {
44 let mut ebytes = [0u8; 8];
45 ebytes[0] = file_code;
46 ebytes[1] = code as u8;
47 ebytes[2] = (line & 0xFF) as u8;
48 ebytes[3] = ((line >> 8) & 0xFF) as u8;
49 unsafe {
50 for i in 0..4 {
51 ebytes[4 + i] = if i < detail_size {
52 *(detail_ptr.add(i))
53 } else {
54 0
55 };
56 }
57 }
58
59 crate::error::Error { ebytes }
60}
61
62pub fn mkerr_generic<D>(file_code: u8, code: u8, line: u32, detail: D) -> Error {
63 mkerr_ptr(
64 file_code,
65 code,
66 line,
67 &detail as *const D as *const u8,
68 core::mem::size_of::<D>(),
69 )
70}
71
72pub fn convert_if_err<R, E>(
73 result: Result<R, E>,
74 file_code: u8,
75 code: u8,
76 line: u32,
77) -> Result<R, crate::error::Error> {
78 match result {
79 Ok(res) => Ok(res),
80
81 Err(_) => Err(mkerr_generic(file_code, code, line, 0)),
82 }
83}
84
85pub fn mkerr(file_code: u8, code: crate::error::ErrorCode, line: u32) -> crate::error::Error {
86 mkerr_base(file_code, code as u8, line, 0)
87}
88
89pub fn mkerr_u32(
90 file_code: u8,
91 code: crate::error::ErrorCode,
92 line: u32,
93 detail: u32,
94) -> crate::error::Error {
95 mkerr_base(file_code, code as u8, line, detail)
96}
97
98pub trait SendError {
99 fn send(&mut self, error: &Error);
100
101 fn report(&mut self, file_code: u8, code: u8, line: u32) {
102 self.send(&mkerr_base(file_code, code as u8, line, 0));
103 }
104 fn reportd(&mut self, file_code: u8, code: u8, line: u32, detail: u32) {
105 self.send(&mkerr_base(file_code, code as u8, line, detail));
106 }
107}