• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
from_static_ref(name: &'static str) -> Self16     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`
as_str(&self) -> &str21     pub fn as_str(&self) -> &str {
22         self.0.as_str()
23     }
24 
as_internal_str(&self) -> &Str25     pub(crate) fn as_internal_str(&self) -> &Str {
26         &self.0
27     }
28 }
29 
30 impl From<&'_ Id> for Id {
from(id: &'_ Id) -> Self31     fn from(id: &'_ Id) -> Self {
32         id.clone()
33     }
34 }
35 
36 impl From<Str> for Id {
from(name: Str) -> Self37     fn from(name: Str) -> Self {
38         Self(name)
39     }
40 }
41 
42 impl From<&'_ Str> for Id {
from(name: &'_ Str) -> Self43     fn from(name: &'_ Str) -> Self {
44         Self(name.into())
45     }
46 }
47 
48 #[cfg(feature = "string")]
49 impl From<std::string::String> for Id {
from(name: std::string::String) -> Self50     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 {
from(name: &'_ std::string::String) -> Self57     fn from(name: &'_ std::string::String) -> Self {
58         Self(name.into())
59     }
60 }
61 
62 impl From<&'static str> for Id {
from(name: &'static str) -> Self63     fn from(name: &'static str) -> Self {
64         Self(name.into())
65     }
66 }
67 
68 impl From<&'_ &'static str> for Id {
from(name: &'_ &'static str) -> Self69     fn from(name: &'_ &'static str) -> Self {
70         Self(name.into())
71     }
72 }
73 
74 impl From<Id> for Str {
from(name: Id) -> Self75     fn from(name: Id) -> Self {
76         name.0
77     }
78 }
79 
80 impl From<Id> for String {
from(name: Id) -> Self81     fn from(name: Id) -> Self {
82         Str::from(name).into()
83     }
84 }
85 
86 impl std::fmt::Display for Id {
87     #[inline]
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result88     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]
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result95     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]
as_ref(&self) -> &str102     fn as_ref(&self) -> &str {
103         self.as_str()
104     }
105 }
106 
107 impl std::borrow::Borrow<str> for Id {
108     #[inline]
borrow(&self) -> &str109     fn borrow(&self) -> &str {
110         self.as_str()
111     }
112 }
113 
114 impl PartialEq<str> for Id {
115     #[inline]
eq(&self, other: &str) -> bool116     fn eq(&self, other: &str) -> bool {
117         PartialEq::eq(self.as_str(), other)
118     }
119 }
120 impl PartialEq<Id> for str {
121     #[inline]
eq(&self, other: &Id) -> bool122     fn eq(&self, other: &Id) -> bool {
123         PartialEq::eq(self, other.as_str())
124     }
125 }
126 
127 impl PartialEq<&'_ str> for Id {
128     #[inline]
eq(&self, other: &&str) -> bool129     fn eq(&self, other: &&str) -> bool {
130         PartialEq::eq(self.as_str(), *other)
131     }
132 }
133 impl PartialEq<Id> for &'_ str {
134     #[inline]
eq(&self, other: &Id) -> bool135     fn eq(&self, other: &Id) -> bool {
136         PartialEq::eq(*self, other.as_str())
137     }
138 }
139 
140 impl PartialEq<Str> for Id {
141     #[inline]
eq(&self, other: &Str) -> bool142     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]
eq(&self, other: &Id) -> bool148     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]
eq(&self, other: &std::string::String) -> bool155     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]
eq(&self, other: &Id) -> bool161     fn eq(&self, other: &Id) -> bool {
162         PartialEq::eq(other, self)
163     }
164 }
165