Debian Codegraph @

src/concrete/message.rs

raw symbols SCIP
1 use std::borrow::Cow;
2 use std::fmt::{Debug, Display, Formatter};
3
4 /// An error that is further described in a message.
5 #[derive(Debug)]
6 pub struct Message(
7 /// The error message.
8 pub(crate) Cow<'static, str>,
9 );
10
11 /// Lifecycle
12 impl Message {
13 /// Create a new instance that displays the given `message`.
14 pub fn new(message: impl Into<Cow<'static, str>>) -> Self {
15 Message(message.into())
16 }
17 }
18
19 impl Display for Message {
20 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21 f.write_str(self.0.as_ref())
22 }
23 }
24
25 impl std::error::Error for Message {}
26
27 impl From<String> for Message {
28 fn from(msg: String) -> Self {
29 Message::new(msg)
30 }
31 }
32
33 impl From<&'static str> for Message {
34 fn from(msg: &'static str) -> Self {
35 Message::new(msg)
36 }
37 }
38
39 /// Return a new statically allocated `message`.
40 ///
41 /// Use the [message!-macro](crate::message!) for convenient `format!`-like behavior.
42 pub fn message(message: &'static str) -> Message {
43 Message::new(message)
44 }