| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | |
| 3 | use alloc::string::{String, ToString}; |
| 4 | use core::fmt::{Debug, Display, Formatter, Result}; |
| 5 | |
| 6 | use serde::ser::{Error as SerError, StdError}; |
| 7 | |
| 8 | /// An error occurred during serialization |
| 9 | #[derive(Debug)] |
| 10 | pub enum Error<T> { |
| 11 | /// An error occurred while writing bytes |
| 12 | /// |
| 13 | /// Contains the underlying error reaturned while writing. |
| 14 | Io(T), |
| 15 | |
| 16 | /// An error indicating a value that cannot be serialized |
| 17 | /// |
| 18 | /// Contains a description of the problem. |
| 19 | Value(String), |
| 20 | } |
| 21 | |
| 22 | impl<T> From<T> for Error<T> { |
| 23 | #[inline ] |
| 24 | fn from(value: T) -> Self { |
| 25 | Error::Io(value) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl<T: Debug> Display for Error<T> { |
| 30 | #[inline ] |
| 31 | fn fmt(&self, f: &mut Formatter<'_>) -> Result { |
| 32 | write!(f, "{:?}" , self) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl<T: Debug> StdError for Error<T> {} |
| 37 | |
| 38 | impl<T: Debug> SerError for Error<T> { |
| 39 | fn custom<U: Display>(msg: U) -> Self { |
| 40 | Error::Value(msg.to_string()) |
| 41 | } |
| 42 | } |
| 43 | |