libcryptsetup_rs/
err.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use std::{
6    error::Error,
7    ffi::NulError,
8    fmt::{self, Display},
9    io,
10    str::Utf8Error,
11};
12
13#[derive(Debug)]
14/// Error returned from any libcryptsetup-rs function
15pub enum LibcryptErr {
16    /// Wrapper for `io::Error`
17    IOError(io::Error),
18    /// Wrapper for `uuid::parser::ParseError`
19    UuidError(uuid::Error),
20    /// Wrapper for `ffi::NulError`
21    NullError(NulError),
22    /// Wrapper for `str::Utf8Error`
23    Utf8Error(Utf8Error),
24    /// Wrapper for `serde_json::Error`
25    JsonError(serde_json::Error),
26    /// Indicates that a Rust/C conversion was unsuccessful
27    InvalidConversion,
28    /// Indicates that a pointer returned was null signifying an error
29    NullPtr,
30    /// Indicates that a `&'static str` was not created with `c_str!()` macro
31    NoNull(&'static str),
32    /// Custom message
33    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 {}