1use std::{ffi::NulError, fmt::Display, num::TryFromIntError};
3
4#[derive(Debug, Clone)]
6pub enum Error {
7 NullPointer,
9 TypeConversion(&'static str),
11 FileDoesNotExist(String),
13}
14
15pub type Result<T> = std::result::Result<T, Error>;
16
17impl Display for Error {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 match self {
20 Error::NullPointer => write!(f, "Received unexpected NULL from the FFI"),
21 Error::TypeConversion(msg) => write!(f, "Type conversion error: {msg}"),
22 Error::FileDoesNotExist(path) => write!(f, "Path doesn't exist: {path}"),
23 }
24 }
25}
26
27impl std::error::Error for Error {}
28
29impl From<NulError> for Error {
30 fn from(_: NulError) -> Self {
31 Self::TypeConversion("Missing null byte in CString conversion")
32 }
33}
34
35impl From<TryFromIntError> for Error {
36 fn from(_: TryFromIntError) -> Self {
37 Self::TypeConversion("Out of range int conversion")
38 }
39}