klauthed_error/code/
error_code.rs1use std::borrow::Cow;
2use std::fmt;
3
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[cfg_attr(feature = "serde", serde(transparent))]
12pub struct ErrorCode(Cow<'static, str>);
13
14impl ErrorCode {
15 pub const fn new(code: &'static str) -> Self {
17 ErrorCode(Cow::Borrowed(code))
18 }
19
20 pub fn from_string(code: String) -> Self {
22 ErrorCode(Cow::Owned(code))
23 }
24
25 pub fn as_str(&self) -> &str {
27 &self.0
28 }
29}
30
31impl fmt::Display for ErrorCode {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 f.write_str(&self.0)
34 }
35}
36
37impl From<&'static str> for ErrorCode {
38 fn from(code: &'static str) -> Self {
39 ErrorCode::new(code)
40 }
41}
42
43impl From<String> for ErrorCode {
44 fn from(code: String) -> Self {
45 ErrorCode::from_string(code)
46 }
47}