1use std::fmt;
2use std::ops;
3
4#[derive(Clone, Eq)]
5pub enum StringOrStatic {
6 String(String),
7 Static(&'static str),
8}
9
10impl From<&str> for StringOrStatic {
11 fn from(s: &str) -> Self {
12 StringOrStatic::String(s.to_owned())
13 }
14}
15
16impl From<String> for StringOrStatic {
17 fn from(s: String) -> Self {
18 StringOrStatic::String(s)
19 }
20}
21
22impl StringOrStatic {
23 pub fn as_str(&self) -> &str {
24 match self {
25 StringOrStatic::String(s) => &s,
26 StringOrStatic::Static(s) => s,
27 }
28 }
29
30 pub fn to_string(&self) -> String {
31 format!("{}", self)
32 }
33}
34
35impl ops::Deref for StringOrStatic {
36 type Target = str;
37
38 fn deref(&self) -> &str {
39 self.as_str()
40 }
41}
42
43impl PartialEq<StringOrStatic> for StringOrStatic {
44 fn eq(&self, other: &StringOrStatic) -> bool {
45 self.as_str() == other.as_str()
46 }
47}
48
49impl PartialEq<str> for StringOrStatic {
50 fn eq(&self, other: &str) -> bool {
51 self.as_str() == other
52 }
53}
54
55impl PartialEq<&str> for StringOrStatic {
56 fn eq(&self, other: &&str) -> bool {
57 self.as_str() == *other
58 }
59}
60
61impl PartialEq<StringOrStatic> for str {
62 fn eq(&self, other: &StringOrStatic) -> bool {
63 self == other.as_str()
64 }
65}
66
67impl PartialEq<StringOrStatic> for &str {
68 fn eq(&self, other: &StringOrStatic) -> bool {
69 *self == other.as_str()
70 }
71}
72
73impl fmt::Display for StringOrStatic {
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 match self {
76 StringOrStatic::String(s) => fmt::Display::fmt(s, f),
77 StringOrStatic::Static(s) => fmt::Display::fmt(s, f),
78 }
79 }
80}