kutil_std/error/
message.rs1#[macro_export]
3macro_rules! message_error {
4 ( $type:ident, $message:literal ) => {
5 #[derive(Clone, Debug, Default)]
7 pub struct $type(::std::option::Option<::std::string::String>);
8
9 impl $type {
10 pub fn new() -> Self {
12 Self(::std::option::Option::None)
13 }
14
15 pub fn new_from<DisplayT>(display: DisplayT) -> Self
17 where
18 DisplayT: ::std::fmt::Display,
19 {
20 display.to_string().into()
21 }
22 }
23
24 impl ::std::convert::From<::std::string::String> for $type {
25 fn from(message: ::std::string::String) -> Self {
26 Self(::std::option::Option::Some(message))
27 }
28 }
29
30 impl ::std::convert::From<&str> for $type {
31 fn from(message: &str) -> Self {
32 Self(::std::option::Option::Some(message.into()))
33 }
34 }
35
36 impl ::std::fmt::Display for $type {
37 fn fmt(&self, formatter: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
38 match &self.0 {
39 ::std::option::Option::Some(message) => ::std::write!(formatter, "{}: {}", $message, message),
40 ::std::option::Option::None => ::std::fmt::Display::fmt($message, formatter),
41 }
42 }
43 }
44
45 impl ::std::error::Error for $type {}
46 };
47}