gix_error/concrete/
message.rs

1use std::borrow::Cow;
2use std::fmt::{Debug, Display, Formatter};
3
4/// An error that is further described in a message.
5#[derive(Debug)]
6pub struct Message(
7    /// The error message.
8    pub(crate) Cow<'static, str>,
9);
10
11/// Lifecycle
12impl 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
19impl Display for Message {
20    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21        f.write_str(self.0.as_ref())
22    }
23}
24
25impl std::error::Error for Message {}
26
27impl From<String> for Message {
28    fn from(msg: String) -> Self {
29        Message::new(msg)
30    }
31}
32
33impl 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.
42pub fn message(message: &'static str) -> Message {
43    Message::new(message)
44}