1use alloc::borrow::Cow;
2use core::fmt;
3#[cfg(feature = "std")]
4use std::io::Error as IoError;
5
6#[cfg(not(feature = "std"))]
7#[derive(Clone, PartialEq, Eq, Debug)]
8pub(crate) struct IoError(Cow<'static, str>);
9
10#[cfg(not(feature = "std"))]
11impl fmt::Display for IoError {
12 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13 fmt::Display::fmt(&self.0, f)
14 }
15}
16
17#[derive(Debug)]
19pub struct Error(IoError);
20
21impl fmt::Display for Error {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 fmt::Display::fmt(&self.0, f)
24 }
25}
26
27#[cfg(feature = "std")]
28impl std::error::Error for Error {}
29#[cfg(not(feature = "std"))]
30impl core::error::Error for Error {}
31
32#[allow(dead_code)]
33impl Error {
34 pub(crate) fn new(message: &'static str) -> Self {
35 #[cfg(not(feature = "std"))]
36 {
37 Self::from_io(IoError(message.into()))
38 }
39
40 #[cfg(feature = "std")]
41 {
42 Self::from_io(IoError::new(std::io::ErrorKind::NotFound, message))
43 }
44 }
45
46 pub(crate) fn with_invalid_data(
47 message: impl Into<Cow<'static, str>>,
48 ) -> Self {
49 let message = message.into();
50
51 #[cfg(not(feature = "std"))]
52 {
53 Self::from_io(IoError(message))
54 }
55
56 #[cfg(feature = "std")]
57 {
58 Self::from_io(IoError::new(
59 std::io::ErrorKind::InvalidData,
60 message,
61 ))
62 }
63 }
64
65 pub(crate) fn from_io(err: IoError) -> Self {
66 Self(err)
67 }
68
69 pub(crate) fn missing_record() -> Self {
70 Self::new("Missing record")
71 }
72
73 pub(crate) fn null_record() -> Self {
74 Self::new("Null record")
75 }
76
77 pub(crate) fn empty_record() -> Self {
78 Self::new("Empty record")
79 }
80
81 pub(crate) fn permission_denied() -> Self {
82 #[cfg(not(feature = "std"))]
83 {
84 Self::from_io(IoError("Permission denied".into()))
85 }
86
87 #[cfg(feature = "std")]
88 {
89 Self::from_io(IoError::new(
90 std::io::ErrorKind::PermissionDenied,
91 "Permission denied",
92 ))
93 }
94 }
95}
96
97impl From<Error> for IoError {
98 fn from(err: Error) -> Self {
99 err.0
100 }
101}