hcl_primitives/
error.rs

1#[cfg(not(feature = "std"))]
2use alloc::string::{String, ToString};
3use core::fmt;
4
5/// The error type used by this crate.
6#[derive(Debug)]
7pub struct Error(String);
8
9impl Error {
10    pub(crate) fn new<T: fmt::Display>(msg: T) -> Error {
11        Error(msg.to_string())
12    }
13}
14
15impl fmt::Display for Error {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        f.write_str(&self.0)
18    }
19}
20
21#[cfg(feature = "std")]
22impl std::error::Error for Error {}
23
24#[cfg(feature = "serde")]
25impl serde::ser::Error for Error {
26    fn custom<T: fmt::Display>(msg: T) -> Self {
27        Error::new(msg)
28    }
29}
30
31#[cfg(feature = "serde")]
32impl serde::de::Error for Error {
33    fn custom<T: fmt::Display>(msg: T) -> Self {
34        Error::new(msg)
35    }
36}