libdd_library_config/
io.rs1use alloc::string::String;
14use core::fmt;
15
16pub type Result<T> = core::result::Result<T, Error>;
17
18#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
20#[non_exhaustive]
21pub enum ErrorKind {
22 NotFound,
23 PermissionDenied,
24 InvalidData,
25 FileTooLarge,
26 Other,
27}
28
29impl fmt::Display for ErrorKind {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 f.write_str(match self {
32 ErrorKind::NotFound => "entity not found",
33 ErrorKind::PermissionDenied => "permission denied",
34 ErrorKind::InvalidData => "invalid data",
35 ErrorKind::FileTooLarge => "file too large",
36 ErrorKind::Other => "other error",
37 })
38 }
39}
40
41#[derive(Debug)]
42pub struct Error {
43 kind: ErrorKind,
44 message: Option<String>,
46}
47
48impl Error {
49 pub fn kind(&self) -> ErrorKind {
50 self.kind
51 }
52}
53
54impl From<ErrorKind> for Error {
55 fn from(kind: ErrorKind) -> Self {
56 Self {
57 kind,
58 message: None,
59 }
60 }
61}
62
63impl fmt::Display for Error {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 match &self.message {
66 Some(message) => f.write_str(message),
67 None => self.kind.fmt(f),
68 }
69 }
70}
71
72impl core::error::Error for Error {}
73
74#[cfg(feature = "std")]
75impl From<std::io::Error> for Error {
76 fn from(err: std::io::Error) -> Self {
77 let kind = match err.kind() {
78 std::io::ErrorKind::NotFound => ErrorKind::NotFound,
79 std::io::ErrorKind::PermissionDenied => ErrorKind::PermissionDenied,
80 std::io::ErrorKind::InvalidData => ErrorKind::InvalidData,
81 std::io::ErrorKind::FileTooLarge => ErrorKind::FileTooLarge,
82 _ => ErrorKind::Other,
83 };
84 Self {
85 kind,
86 message: Some(err.to_string()),
87 }
88 }
89}