1use std::{
6 error::Error,
7 ffi::NulError,
8 fmt::{self, Display},
9 io,
10 str::Utf8Error,
11};
12
13#[derive(Debug)]
14pub enum LibcryptErr {
16 IOError(io::Error),
18 UuidError(uuid::Error),
20 NullError(NulError),
22 Utf8Error(Utf8Error),
24 JsonError(serde_json::Error),
26 InvalidConversion,
28 NullPtr,
30 NoNull(&'static str),
32 Other(String),
34}
35
36impl Display for LibcryptErr {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 match *self {
39 LibcryptErr::IOError(ref e) => write!(f, "IO error occurred: {e}"),
40 LibcryptErr::UuidError(ref e) => write!(f, "Failed to parse UUID from C string: {e}"),
41 LibcryptErr::NullError(ref e) => {
42 write!(f, "Null error occurred when handling &str conversion: {e}")
43 }
44 LibcryptErr::Utf8Error(ref e) => {
45 write!(f, "UTF8 error occurred when handling &str conversion: {e}")
46 }
47 LibcryptErr::JsonError(ref e) => {
48 write!(f, "Failed to parse the provided string into JSON: {e}")
49 }
50 LibcryptErr::InvalidConversion => {
51 write!(f, "Failed to perform the specified conversion")
52 }
53 LibcryptErr::NullPtr => write!(f, "Cryptsetup returned a null pointer"),
54 LibcryptErr::NoNull(s) => {
55 write!(f, "Static string {s} was not created with c_str!() macro")
56 }
57 LibcryptErr::Other(ref s) => write!(f, "Failed with error: {s}"),
58 }
59 }
60}
61
62impl Error for LibcryptErr {}