1 | use crate::builder::Str; |
2 | |
3 | /// [`Arg`][crate::Arg] or [`ArgGroup`][crate::ArgGroup] identifier |
4 | /// |
5 | /// This is used for accessing the value in [`ArgMatches`][crate::ArgMatches] or defining |
6 | /// relationships between `Arg`s and `ArgGroup`s with functions like |
7 | /// [`Arg::conflicts_with`][crate::Arg::conflicts_with]. |
8 | #[derive(Default, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] |
9 | pub struct Id(Str); |
10 | |
11 | impl Id { |
12 | pub(crate) const HELP: &'static str = "help" ; |
13 | pub(crate) const VERSION: &'static str = "version" ; |
14 | pub(crate) const EXTERNAL: &'static str = "" ; |
15 | |
16 | pub(crate) fn from_static_ref(name: &'static str) -> Self { |
17 | Self(Str::from_static_ref(name)) |
18 | } |
19 | |
20 | /// Get the raw string of the `Id` |
21 | pub fn as_str(&self) -> &str { |
22 | self.0.as_str() |
23 | } |
24 | |
25 | pub(crate) fn as_internal_str(&self) -> &Str { |
26 | &self.0 |
27 | } |
28 | } |
29 | |
30 | impl From<&'_ Id> for Id { |
31 | fn from(id: &'_ Id) -> Self { |
32 | id.clone() |
33 | } |
34 | } |
35 | |
36 | impl From<Str> for Id { |
37 | fn from(name: Str) -> Self { |
38 | Self(name) |
39 | } |
40 | } |
41 | |
42 | impl From<&'_ Str> for Id { |
43 | fn from(name: &'_ Str) -> Self { |
44 | Self(name.into()) |
45 | } |
46 | } |
47 | |
48 | #[cfg (feature = "string" )] |
49 | impl From<std::string::String> for Id { |
50 | fn from(name: std::string::String) -> Self { |
51 | Self(name.into()) |
52 | } |
53 | } |
54 | |
55 | #[cfg (feature = "string" )] |
56 | impl From<&'_ std::string::String> for Id { |
57 | fn from(name: &'_ std::string::String) -> Self { |
58 | Self(name.into()) |
59 | } |
60 | } |
61 | |
62 | impl From<&'static str> for Id { |
63 | fn from(name: &'static str) -> Self { |
64 | Self(name.into()) |
65 | } |
66 | } |
67 | |
68 | impl From<&'_ &'static str> for Id { |
69 | fn from(name: &'_ &'static str) -> Self { |
70 | Self(name.into()) |
71 | } |
72 | } |
73 | |
74 | impl From<Id> for Str { |
75 | fn from(name: Id) -> Self { |
76 | name.0 |
77 | } |
78 | } |
79 | |
80 | impl From<Id> for String { |
81 | fn from(name: Id) -> Self { |
82 | Str::from(name).into() |
83 | } |
84 | } |
85 | |
86 | impl std::fmt::Display for Id { |
87 | #[inline ] |
88 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
89 | std::fmt::Display::fmt(self.as_str(), f) |
90 | } |
91 | } |
92 | |
93 | impl std::fmt::Debug for Id { |
94 | #[inline ] |
95 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
96 | std::fmt::Debug::fmt(self.as_str(), f) |
97 | } |
98 | } |
99 | |
100 | impl AsRef<str> for Id { |
101 | #[inline ] |
102 | fn as_ref(&self) -> &str { |
103 | self.as_str() |
104 | } |
105 | } |
106 | |
107 | impl std::borrow::Borrow<str> for Id { |
108 | #[inline ] |
109 | fn borrow(&self) -> &str { |
110 | self.as_str() |
111 | } |
112 | } |
113 | |
114 | impl PartialEq<str> for Id { |
115 | #[inline ] |
116 | fn eq(&self, other: &str) -> bool { |
117 | PartialEq::eq(self.as_str(), other) |
118 | } |
119 | } |
120 | impl PartialEq<Id> for str { |
121 | #[inline ] |
122 | fn eq(&self, other: &Id) -> bool { |
123 | PartialEq::eq(self, other.as_str()) |
124 | } |
125 | } |
126 | |
127 | impl PartialEq<&'_ str> for Id { |
128 | #[inline ] |
129 | fn eq(&self, other: &&str) -> bool { |
130 | PartialEq::eq(self.as_str(), *other) |
131 | } |
132 | } |
133 | impl PartialEq<Id> for &'_ str { |
134 | #[inline ] |
135 | fn eq(&self, other: &Id) -> bool { |
136 | PartialEq::eq(*self, other.as_str()) |
137 | } |
138 | } |
139 | |
140 | impl PartialEq<Str> for Id { |
141 | #[inline ] |
142 | fn eq(&self, other: &Str) -> bool { |
143 | PartialEq::eq(self.as_str(), other.as_str()) |
144 | } |
145 | } |
146 | impl PartialEq<Id> for Str { |
147 | #[inline ] |
148 | fn eq(&self, other: &Id) -> bool { |
149 | PartialEq::eq(self.as_str(), other.as_str()) |
150 | } |
151 | } |
152 | |
153 | impl PartialEq<std::string::String> for Id { |
154 | #[inline ] |
155 | fn eq(&self, other: &std::string::String) -> bool { |
156 | PartialEq::eq(self.as_str(), other.as_str()) |
157 | } |
158 | } |
159 | impl PartialEq<Id> for std::string::String { |
160 | #[inline ] |
161 | fn eq(&self, other: &Id) -> bool { |
162 | PartialEq::eq(other, self) |
163 | } |
164 | } |
165 | |