musli/context/
context_error.rs

1//! Trait governing what error types associated with the encoding framework must
2//! support.
3//!
4//! The most important component in here is `Error::custom` which allows custom
5//! encoding implementations to raise custom errors based on types that
6//! implement [Display][core::fmt::Display].
7
8use core::error::Error;
9use core::fmt;
10
11#[cfg(feature = "alloc")]
12use rust_alloc::string::{String, ToString};
13
14/// Trait governing errors raised during encodeing or decoding.
15#[diagnostic::on_unimplemented(
16    message = "`ContextError` must be implemented for `{Self}`, or any error type captured by custom contexts",
17    note = "use `musli::context::ErrorMarker` to ignore errors",
18    note = "use `std::io::Error` and `std::string::String`, if the `std` or `alloc` features are enabled for `musli`"
19)]
20pub trait ContextError<A> {
21    /// Construct a custom error.
22    fn custom<T>(alloc: A, error: T) -> Self
23    where
24        T: 'static + Send + Sync + Error;
25
26    /// Collect an error from something that can be displayed.
27    ///
28    /// This is made available to format custom error messages in `no_std`
29    /// environments. The error message is to be collected by formatting `T`.
30    fn message<T>(alloc: A, message: T) -> Self
31    where
32        T: fmt::Display;
33}
34
35#[cfg(feature = "std")]
36impl<A> ContextError<A> for std::io::Error {
37    #[inline]
38    fn custom<T>(_: A, message: T) -> Self
39    where
40        T: 'static + Send + Sync + Error,
41    {
42        std::io::Error::new(std::io::ErrorKind::Other, message)
43    }
44
45    #[inline]
46    fn message<T>(_: A, message: T) -> Self
47    where
48        T: fmt::Display,
49    {
50        std::io::Error::new(std::io::ErrorKind::Other, std::format!("{message}"))
51    }
52}
53
54#[cfg(feature = "alloc")]
55impl<A> ContextError<A> for String {
56    #[inline]
57    fn custom<T>(_: A, message: T) -> Self
58    where
59        T: fmt::Display,
60    {
61        message.to_string()
62    }
63
64    #[inline]
65    fn message<T>(_: A, message: T) -> Self
66    where
67        T: fmt::Display,
68    {
69        message.to_string()
70    }
71}