flm_core/progress/
kind.rs1use std::borrow::Borrow;
4use std::fmt;
5use std::hash::{Hash, Hasher};
6use std::ops::Deref;
7use std::sync::Arc;
8
9#[derive(Clone)]
18pub struct SharedText(Repr);
19
20#[derive(Clone)]
21enum Repr {
22 Static(&'static str),
23 Shared(Arc<str>),
24}
25
26impl SharedText {
27 pub const fn from_static(text: &'static str) -> SharedText {
29 SharedText(Repr::Static(text))
30 }
31
32 pub fn as_str(&self) -> &str {
34 match &self.0 {
35 Repr::Static(text) => text,
36 Repr::Shared(text) => text,
37 }
38 }
39}
40
41impl Deref for SharedText {
42 type Target = str;
43
44 fn deref(&self) -> &str {
45 self.as_str()
46 }
47}
48
49impl AsRef<str> for SharedText {
50 fn as_ref(&self) -> &str {
51 self.as_str()
52 }
53}
54
55impl Borrow<str> for SharedText {
56 fn borrow(&self) -> &str {
57 self.as_str()
58 }
59}
60
61impl From<&'static str> for SharedText {
62 fn from(text: &'static str) -> SharedText {
63 SharedText::from_static(text)
64 }
65}
66
67impl From<String> for SharedText {
68 fn from(text: String) -> SharedText {
69 SharedText(Repr::Shared(text.into()))
70 }
71}
72
73impl From<Arc<str>> for SharedText {
74 fn from(text: Arc<str>) -> SharedText {
75 SharedText(Repr::Shared(text))
76 }
77}
78
79impl PartialEq for SharedText {
80 fn eq(&self, other: &SharedText) -> bool {
81 self.as_str() == other.as_str()
82 }
83}
84
85impl Eq for SharedText {}
86
87impl PartialEq<str> for SharedText {
88 fn eq(&self, other: &str) -> bool {
89 self.as_str() == other
90 }
91}
92
93impl PartialEq<&str> for SharedText {
94 fn eq(&self, other: &&str) -> bool {
95 self.as_str() == *other
96 }
97}
98
99impl PartialOrd for SharedText {
100 fn partial_cmp(&self, other: &SharedText) -> Option<std::cmp::Ordering> {
101 Some(self.cmp(other))
102 }
103}
104
105impl Ord for SharedText {
106 fn cmp(&self, other: &SharedText) -> std::cmp::Ordering {
107 self.as_str().cmp(other.as_str())
108 }
109}
110
111impl Hash for SharedText {
112 fn hash<H: Hasher>(&self, state: &mut H) {
113 self.as_str().hash(state)
114 }
115}
116
117impl fmt::Display for SharedText {
118 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119 f.write_str(self.as_str())
120 }
121}
122
123impl fmt::Debug for SharedText {
124 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125 fmt::Debug::fmt(self.as_str(), f)
126 }
127}
128
129impl serde::Serialize for SharedText {
130 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
131 serializer.serialize_str(self.as_str())
132 }
133}
134
135impl<'de> serde::Deserialize<'de> for SharedText {
136 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<SharedText, D::Error> {
137 String::deserialize(deserializer).map(SharedText::from)
138 }
139}
140
141#[derive(Clone, Debug, PartialEq, Eq)]
167pub struct Kind {
168 name: SharedText,
169 title: Option<SharedText>,
170}
171
172impl Kind {
173 pub const fn new(name: &'static str) -> Kind {
175 Kind { name: SharedText::from_static(name), title: None }
176 }
177
178 pub const fn titled(name: &'static str, title: &'static str) -> Kind {
180 Kind { name: SharedText::from_static(name), title: Some(SharedText::from_static(title)) }
181 }
182
183 pub fn shared(name: impl Into<Arc<str>>) -> Kind {
186 Kind { name: SharedText::from(name.into()), title: None }
187 }
188
189 pub fn with_title(self, title: impl Into<Arc<str>>) -> Kind {
193 Kind { title: Some(SharedText::from(title.into())), ..self }
194 }
195
196 pub fn name(&self) -> &str {
198 self.name.as_str()
199 }
200
201 pub fn title(&self) -> Option<&str> {
203 self.title.as_deref()
204 }
205
206 pub(super) fn shared_name(&self) -> &SharedText {
208 &self.name
209 }
210
211 pub(super) fn shared_title(&self) -> Option<&SharedText> {
213 self.title.as_ref()
214 }
215}