clap_builder/builder/
str.rs1#[cfg(feature = "string")]
2use std::borrow::Cow;
3
4#[derive(Default, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
13pub struct Str {
14 name: Inner,
15}
16
17impl Str {
18 #[cfg(feature = "string")]
19 pub(crate) fn from_string(name: String) -> Self {
20 Self {
21 name: Inner::from_string(name),
22 }
23 }
24
25 #[cfg(feature = "string")]
26 pub(crate) fn from_ref(name: &str) -> Self {
27 Self {
28 name: Inner::from_ref(name),
29 }
30 }
31
32 pub(crate) fn from_static_ref(name: &'static str) -> Self {
33 Self {
34 name: Inner::from_static_ref(name),
35 }
36 }
37
38 pub(crate) fn into_inner(self) -> Inner {
39 self.name
40 }
41
42 pub fn as_str(&self) -> &str {
44 self.name.as_str()
45 }
46}
47
48impl From<&'_ Str> for Str {
49 fn from(id: &'_ Str) -> Self {
50 id.clone()
51 }
52}
53
54#[cfg(feature = "string")]
55impl From<String> for Str {
56 fn from(name: String) -> Self {
57 Self::from_string(name)
58 }
59}
60
61#[cfg(feature = "string")]
62impl From<&'_ String> for Str {
63 fn from(name: &'_ String) -> Self {
64 Self::from_ref(name.as_str())
65 }
66}
67
68impl From<&'static str> for Str {
69 fn from(name: &'static str) -> Self {
70 Self::from_static_ref(name)
71 }
72}
73
74impl From<&'_ &'static str> for Str {
75 fn from(name: &'_ &'static str) -> Self {
76 Self::from_static_ref(name)
77 }
78}
79
80#[cfg(feature = "string")]
81impl From<Cow<'static, str>> for Str {
82 fn from(cow: Cow<'static, str>) -> Self {
83 match cow {
84 Cow::Borrowed(s) => Self::from(s),
85 Cow::Owned(s) => Self::from(s),
86 }
87 }
88}
89
90impl From<Str> for String {
91 fn from(name: Str) -> Self {
92 name.name.into_string()
93 }
94}
95
96impl From<Str> for Vec<u8> {
97 fn from(name: Str) -> Self {
98 String::from(name).into()
99 }
100}
101
102impl From<Str> for std::ffi::OsString {
103 fn from(name: Str) -> Self {
104 String::from(name).into()
105 }
106}
107
108impl From<Str> for std::path::PathBuf {
109 fn from(name: Str) -> Self {
110 String::from(name).into()
111 }
112}
113
114impl std::fmt::Display for Str {
115 #[inline]
116 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117 std::fmt::Display::fmt(self.as_str(), f)
118 }
119}
120
121impl std::fmt::Debug for Str {
122 #[inline]
123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124 std::fmt::Debug::fmt(self.as_str(), f)
125 }
126}
127
128impl std::ops::Deref for Str {
129 type Target = str;
130
131 #[inline]
132 fn deref(&self) -> &str {
133 self.as_str()
134 }
135}
136
137impl AsRef<str> for Str {
138 #[inline]
139 fn as_ref(&self) -> &str {
140 self.as_str()
141 }
142}
143
144impl AsRef<[u8]> for Str {
145 #[inline]
146 fn as_ref(&self) -> &[u8] {
147 self.as_bytes()
148 }
149}
150
151impl AsRef<std::ffi::OsStr> for Str {
152 #[inline]
153 fn as_ref(&self) -> &std::ffi::OsStr {
154 (**self).as_ref()
155 }
156}
157
158impl AsRef<std::path::Path> for Str {
159 #[inline]
160 fn as_ref(&self) -> &std::path::Path {
161 std::path::Path::new(self)
162 }
163}
164
165impl std::borrow::Borrow<str> for Str {
166 #[inline]
167 fn borrow(&self) -> &str {
168 self.as_str()
169 }
170}
171
172impl PartialEq<str> for Str {
173 #[inline]
174 fn eq(&self, other: &str) -> bool {
175 PartialEq::eq(self.as_str(), other)
176 }
177}
178impl PartialEq<Str> for str {
179 #[inline]
180 fn eq(&self, other: &Str) -> bool {
181 PartialEq::eq(self, other.as_str())
182 }
183}
184
185impl PartialEq<&'_ str> for Str {
186 #[inline]
187 fn eq(&self, other: &&str) -> bool {
188 PartialEq::eq(self.as_str(), *other)
189 }
190}
191impl PartialEq<Str> for &'_ str {
192 #[inline]
193 fn eq(&self, other: &Str) -> bool {
194 PartialEq::eq(*self, other.as_str())
195 }
196}
197
198impl PartialEq<std::ffi::OsStr> for Str {
199 #[inline]
200 fn eq(&self, other: &std::ffi::OsStr) -> bool {
201 PartialEq::eq(self.as_str(), other)
202 }
203}
204impl PartialEq<Str> for std::ffi::OsStr {
205 #[inline]
206 fn eq(&self, other: &Str) -> bool {
207 PartialEq::eq(self, other.as_str())
208 }
209}
210
211impl PartialEq<&'_ std::ffi::OsStr> for Str {
212 #[inline]
213 fn eq(&self, other: &&std::ffi::OsStr) -> bool {
214 PartialEq::eq(self.as_str(), *other)
215 }
216}
217impl PartialEq<Str> for &'_ std::ffi::OsStr {
218 #[inline]
219 fn eq(&self, other: &Str) -> bool {
220 PartialEq::eq(*self, other.as_str())
221 }
222}
223
224impl PartialEq<String> for Str {
225 #[inline]
226 fn eq(&self, other: &String) -> bool {
227 PartialEq::eq(self.as_str(), other.as_str())
228 }
229}
230impl PartialEq<Str> for String {
231 #[inline]
232 fn eq(&self, other: &Str) -> bool {
233 PartialEq::eq(self.as_str(), other.as_str())
234 }
235}
236
237#[cfg(feature = "string")]
238pub(crate) mod inner {
239 #[derive(Clone)]
240 pub(crate) enum Inner {
241 Static(&'static str),
242 Owned(Box<str>),
243 }
244
245 impl Inner {
246 pub(crate) fn from_string(name: String) -> Self {
247 Self::Owned(name.into_boxed_str())
248 }
249
250 pub(crate) fn from_ref(name: &str) -> Self {
251 Self::Owned(Box::from(name))
252 }
253
254 pub(crate) fn from_static_ref(name: &'static str) -> Self {
255 Self::Static(name)
256 }
257
258 pub(crate) fn as_str(&self) -> &str {
259 match self {
260 Self::Static(s) => s,
261 Self::Owned(s) => s.as_ref(),
262 }
263 }
264
265 pub(crate) fn into_string(self) -> String {
266 match self {
267 Self::Static(s) => s.to_owned(),
268 Self::Owned(s) => s.into(),
269 }
270 }
271 }
272}
273
274#[cfg(not(feature = "string"))]
275pub(crate) mod inner {
276 #[derive(Clone)]
277 pub(crate) struct Inner(pub(crate) &'static str);
278
279 impl Inner {
280 pub(crate) fn from_static_ref(name: &'static str) -> Self {
281 Self(name)
282 }
283
284 pub(crate) fn as_str(&self) -> &str {
285 self.0
286 }
287
288 pub(crate) fn into_string(self) -> String {
289 self.as_str().to_owned()
290 }
291 }
292}
293
294pub(crate) use inner::Inner;
295
296impl Default for Inner {
297 fn default() -> Self {
298 Self::from_static_ref("")
299 }
300}
301
302impl PartialEq for Inner {
303 fn eq(&self, other: &Inner) -> bool {
304 self.as_str() == other.as_str()
305 }
306}
307
308impl PartialOrd for Inner {
309 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
310 Some(self.cmp(other))
311 }
312}
313
314impl Ord for Inner {
315 fn cmp(&self, other: &Inner) -> std::cmp::Ordering {
316 self.as_str().cmp(other.as_str())
317 }
318}
319
320impl Eq for Inner {}
321
322impl std::hash::Hash for Inner {
323 #[inline]
324 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
325 self.as_str().hash(state);
326 }
327}
328
329#[cfg(test)]
330#[cfg(feature = "string")]
331mod tests {
332 use super::*;
333
334 #[test]
335 #[cfg(feature = "string")]
336 fn from_cow_borrowed() {
337 let cow = Cow::Borrowed("hello");
338 let str = Str::from(cow);
339 assert_eq!(str, Str::from("hello"));
340 }
341
342 #[test]
343 #[cfg(feature = "string")]
344 fn from_cow_owned() {
345 let cow = Cow::Owned("world".to_string());
346 let str = Str::from(cow);
347 assert_eq!(str, Str::from("world"));
348 }
349}