17e2e9c0cSopenharmony_ciuse crate::lib::{Debug, Display};
27e2e9c0cSopenharmony_ci
37e2e9c0cSopenharmony_ci/// Either a re-export of std::error::Error or a new identical trait, depending
47e2e9c0cSopenharmony_ci/// on whether Serde's "std" feature is enabled.
57e2e9c0cSopenharmony_ci///
67e2e9c0cSopenharmony_ci/// Serde's error traits [`serde::ser::Error`] and [`serde::de::Error`] require
77e2e9c0cSopenharmony_ci/// [`std::error::Error`] as a supertrait, but only when Serde is built with
87e2e9c0cSopenharmony_ci/// "std" enabled. Data formats that don't care about no\_std support should
97e2e9c0cSopenharmony_ci/// generally provide their error types with a `std::error::Error` impl
107e2e9c0cSopenharmony_ci/// directly:
117e2e9c0cSopenharmony_ci///
127e2e9c0cSopenharmony_ci/// ```edition2021
137e2e9c0cSopenharmony_ci/// #[derive(Debug)]
147e2e9c0cSopenharmony_ci/// struct MySerError {...}
157e2e9c0cSopenharmony_ci///
167e2e9c0cSopenharmony_ci/// impl serde::ser::Error for MySerError {...}
177e2e9c0cSopenharmony_ci///
187e2e9c0cSopenharmony_ci/// impl std::fmt::Display for MySerError {...}
197e2e9c0cSopenharmony_ci///
207e2e9c0cSopenharmony_ci/// // We don't support no_std!
217e2e9c0cSopenharmony_ci/// impl std::error::Error for MySerError {}
227e2e9c0cSopenharmony_ci/// ```
237e2e9c0cSopenharmony_ci///
247e2e9c0cSopenharmony_ci/// Data formats that *do* support no\_std may either have a "std" feature of
257e2e9c0cSopenharmony_ci/// their own:
267e2e9c0cSopenharmony_ci///
277e2e9c0cSopenharmony_ci/// ```toml
287e2e9c0cSopenharmony_ci/// [features]
297e2e9c0cSopenharmony_ci/// std = ["serde/std"]
307e2e9c0cSopenharmony_ci/// ```
317e2e9c0cSopenharmony_ci///
327e2e9c0cSopenharmony_ci/// ```edition2021
337e2e9c0cSopenharmony_ci/// #[cfg(feature = "std")]
347e2e9c0cSopenharmony_ci/// impl std::error::Error for MySerError {}
357e2e9c0cSopenharmony_ci/// ```
367e2e9c0cSopenharmony_ci///
377e2e9c0cSopenharmony_ci/// ... or else provide the std Error impl unconditionally via Serde's
387e2e9c0cSopenharmony_ci/// re-export:
397e2e9c0cSopenharmony_ci///
407e2e9c0cSopenharmony_ci/// ```edition2021
417e2e9c0cSopenharmony_ci/// impl serde::ser::StdError for MySerError {}
427e2e9c0cSopenharmony_ci/// ```
437e2e9c0cSopenharmony_cipub trait Error: Debug + Display {
447e2e9c0cSopenharmony_ci    /// The underlying cause of this error, if any.
457e2e9c0cSopenharmony_ci    fn source(&self) -> Option<&(Error + 'static)> {
467e2e9c0cSopenharmony_ci        None
477e2e9c0cSopenharmony_ci    }
487e2e9c0cSopenharmony_ci}
49