toasty_core/error/
adhoc.rs

1use super::Error;
2
3/// An ad-hoc error created from a format string.
4#[derive(Debug)]
5pub(super) struct Adhoc {
6    pub(super) message: Box<str>,
7}
8
9impl Adhoc {
10    pub(super) fn from_args<'a>(message: core::fmt::Arguments<'a>) -> Adhoc {
11        use std::string::ToString;
12
13        let message = message.to_string().into_boxed_str();
14        Adhoc { message }
15    }
16}
17
18impl std::error::Error for Adhoc {}
19
20impl core::fmt::Display for Adhoc {
21    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
22        core::fmt::Display::fmt(&self.message, f)
23    }
24}
25
26impl Error {
27    /// Creates an error from a format string.
28    ///
29    /// # Examples
30    ///
31    /// ```
32    /// use toasty_core::Error;
33    ///
34    /// let err = Error::from_args(format_args!("value {} is invalid", "foo"));
35    /// ```
36    pub fn from_args<'a>(message: core::fmt::Arguments<'a>) -> Error {
37        Error::from(super::ErrorKind::Adhoc(Adhoc::from_args(message)))
38    }
39
40    /// Returns `true` if this error is an adhoc error.
41    pub fn is_adhoc(&self) -> bool {
42        matches!(self.kind(), super::ErrorKind::Adhoc(_))
43    }
44}