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
use std::{ffi::NulError, fmt::Display, num::TryFromIntError};
#[derive(Debug, Clone)]
pub enum Error {
NullPointer,
TypeConversion(&'static str),
FileDoesNotExist(String),
}
pub type Result<T> = std::result::Result<T, Error>;
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::NullPointer => write!(f, "Received unexpected NULL from the FFI"),
Error::TypeConversion(msg) => write!(f, "Type conversion error: {msg}"),
Error::FileDoesNotExist(path) => write!(f, "Path doesn't exist: {path}"),
}
}
}
impl std::error::Error for Error {}
impl From<NulError> for Error {
fn from(_: NulError) -> Self {
Self::TypeConversion("Missing null byte in CString conversion")
}
}
impl From<TryFromIntError> for Error {
fn from(_: TryFromIntError) -> Self {
Self::TypeConversion("Out of range int conversion")
}
}