safa_abi/
errors.rs

1#[allow(dead_code)]
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3#[repr(u16)]
4pub enum ErrorStatus {
5    // use when no ErrorStatus is avalible for xyz and you cannot add a new one
6    Generic = 1,
7    OperationNotSupported = 2,
8    // for example an elf class is not supported, there is a difference between NotSupported and
9    // OperationNotSupported
10    NotSupported = 3,
11    // for example a magic value is invaild
12    Corrupted = 4,
13    InvaildSyscall = 5,
14    InvaildResource = 6,
15    InvaildPid = 7,
16    InvaildOffset = 8,
17    // instead of panicking syscalls will return this on null and unaligned pointers
18    InvaildPtr = 9,
19    // for operations that requires a vaild utf8 str...
20    InvaildStr = 0xA,
21    // for operations that requires a str that doesn't exceed a max length such as
22    // file names (128 bytes)
23    StrTooLong = 0xB,
24    InvaildPath = 0xC,
25    NoSuchAFileOrDirectory = 0xD,
26    NotAFile = 0xE,
27    NotADirectory = 0xF,
28    AlreadyExists = 0x10,
29    NotExecutable = 0x11,
30    // would be useful when i add remove related operations to the vfs
31    DirectoryNotEmpty = 0x12,
32    // Generic premissions(protection) related error
33    MissingPermissions = 0x13,
34    // memory allocations and mapping error, most likely that memory is full
35    MMapError = 0x14,
36    Busy = 0x15,
37    // errors sent by processes
38    NotEnoughArguments = 0x16,
39    OutOfMemory = 0x17,
40}
41
42impl ErrorStatus {
43    // update when a new error is added
44    const MAX: u16 = Self::OutOfMemory as u16;
45
46    #[inline(always)]
47    /// Gives a string description of the error
48    pub fn as_str(&self) -> &'static str {
49        use ErrorStatus::*;
50        match *self {
51            Generic => "Generic Error",
52            OperationNotSupported => "Operation Not Supported",
53            NotSupported => "Object Not Supported",
54            Corrupted => "Corrupted",
55            InvaildSyscall => "Invaild Syscall",
56            InvaildResource => "Invaild Resource",
57            InvaildPid => "Invaild PID",
58            InvaildOffset => "Invaild Offset",
59            InvaildPtr => "Invaild Ptr (not aligned or null)",
60            InvaildStr => "Invaild Str (not utf8)",
61            StrTooLong => "Str too Long",
62            InvaildPath => "Invaild Path",
63            NoSuchAFileOrDirectory => "No Such a File or Directory",
64            NotAFile => "Not a File",
65            NotADirectory => "Not a Directory",
66            AlreadyExists => "Already Exists",
67            NotExecutable => "Not Executable",
68            DirectoryNotEmpty => "Directory not Empty",
69            MissingPermissions => "Missing Permissions",
70            MMapError => "Memory Map Error (most likely out of memory)",
71            Busy => "Resource Busy",
72            NotEnoughArguments => "Not Enough Arguments",
73            OutOfMemory => "Out of Memory",
74        }
75    }
76}
77
78impl TryFrom<u16> for ErrorStatus {
79    type Error = ();
80
81    fn try_from(value: u16) -> Result<Self, Self::Error> {
82        if value > 0 && value <= Self::MAX {
83            Ok(unsafe { core::mem::transmute(value) })
84        } else {
85            Err(())
86        }
87    }
88}
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90pub enum SysResult {
91    Sucess,
92    Error(ErrorStatus),
93}
94
95impl From<ErrorStatus> for SysResult {
96    #[inline(always)]
97    fn from(value: ErrorStatus) -> Self {
98        SysResult::Error(value)
99    }
100}
101
102impl From<Result<(), ErrorStatus>> for SysResult {
103    #[inline(always)]
104    fn from(value: Result<(), ErrorStatus>) -> Self {
105        match value {
106            Ok(()) => SysResult::Sucess,
107            Err(err) => SysResult::Error(err),
108        }
109    }
110}
111
112impl TryFrom<u16> for SysResult {
113    type Error = ();
114    #[inline(always)]
115    fn try_from(value: u16) -> Result<Self, ()> {
116        match value {
117            0 => Ok(SysResult::Sucess),
118            other => {
119                let err = ErrorStatus::try_from(other).map_err(|_| ())?;
120                Ok(SysResult::Error(err))
121            }
122        }
123    }
124}
125
126impl From<SysResult> for Result<(), ErrorStatus> {
127    #[inline(always)]
128    fn from(value: SysResult) -> Self {
129        match value {
130            SysResult::Sucess => Ok(()),
131            SysResult::Error(err) => Err(err),
132        }
133    }
134}
135
136impl Into<u16> for SysResult {
137    #[inline(always)]
138    fn into(self) -> u16 {
139        match self {
140            SysResult::Sucess => 0,
141            SysResult::Error(err) => err as u16,
142        }
143    }
144}
145
146pub trait IntoErr {
147    fn into_err(self) -> ErrorStatus;
148}
149
150impl<T: IntoErr> From<T> for ErrorStatus {
151    fn from(value: T) -> Self {
152        value.into_err()
153    }
154}