clap_builder/util/
id.rs

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