1use alloc::string::String;
2use core::{fmt, result};
3#[cfg(feature = "std")]
4use std::error;
5
6use crate::{read, write};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct Error(pub(super) String);
11
12impl Error {
13 pub(super) fn new(message: impl Into<String>) -> Self {
14 Error(message.into())
15 }
16}
17
18impl fmt::Display for Error {
19 #[inline]
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 f.write_str(&self.0)
22 }
23}
24
25#[cfg(feature = "std")]
26impl error::Error for Error {}
27#[cfg(all(not(feature = "std"), core_error))]
28impl core::error::Error for Error {}
29
30impl From<read::Error> for Error {
31 fn from(error: read::Error) -> Error {
32 Error(format!("{}", error))
33 }
34}
35
36impl From<write::Error> for Error {
37 fn from(error: write::Error) -> Error {
38 Error(error.0)
39 }
40}
41
42pub type Result<T> = result::Result<T, Error>;