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