json_ld_syntax_next/context/definition/
key.rs

1use crate::{CompactIri, Keyword};
2use iref::Iri;
3use rdf_types::BlankId;
4use std::borrow::Borrow;
5use std::fmt;
6use std::hash::Hash;
7
8/// Context key.
9#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[cfg_attr(feature = "serde", serde(transparent))]
12pub struct Key(String);
13
14impl Key {
15	pub fn as_iri(&self) -> Option<&Iri> {
16		Iri::new(&self.0).ok()
17	}
18
19	pub fn as_compact_iri(&self) -> Option<&CompactIri> {
20		CompactIri::new(&self.0).ok()
21	}
22
23	pub fn as_blank_id(&self) -> Option<&BlankId> {
24		BlankId::new(&self.0).ok()
25	}
26
27	pub fn as_str(&self) -> &str {
28		&self.0
29	}
30
31	pub fn len(&self) -> usize {
32		self.0.len()
33	}
34
35	pub fn is_empty(&self) -> bool {
36		self.0.is_empty()
37	}
38
39	pub fn into_string(self) -> String {
40		self.0
41	}
42
43	pub fn is_keyword_like(&self) -> bool {
44		crate::is_keyword_like(self.as_str())
45	}
46}
47
48impl From<json_syntax::object::Key> for Key {
49	fn from(k: json_syntax::object::Key) -> Self {
50		Self::from(k.into_string())
51	}
52}
53
54#[allow(clippy::derived_hash_with_manual_eq)]
55impl Hash for Key {
56	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
57		self.as_str().hash(state)
58	}
59}
60
61impl From<String> for Key {
62	fn from(k: String) -> Self {
63		Self(k)
64	}
65}
66
67impl<'a> From<&'a str> for Key {
68	fn from(value: &'a str) -> Self {
69		Self(value.to_owned())
70	}
71}
72
73impl fmt::Display for Key {
74	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75		self.0.fmt(f)
76	}
77}
78
79impl Borrow<str> for Key {
80	fn borrow(&self) -> &str {
81		self.as_str()
82	}
83}
84
85#[derive(Clone, Copy, PartialEq, Eq, Debug)]
86pub struct KeyRef<'a>(&'a str);
87
88impl<'a> KeyRef<'a> {
89	pub fn is_empty(&self) -> bool {
90		self.0.is_empty()
91	}
92
93	pub fn is_keyword_like(&self) -> bool {
94		crate::is_keyword_like(self.as_str())
95	}
96
97	pub fn as_str(&self) -> &'a str {
98		self.0
99	}
100
101	pub fn to_owned(self) -> Key {
102		Key(self.0.to_owned())
103	}
104}
105
106impl<'a> From<&'a str> for KeyRef<'a> {
107	fn from(s: &'a str) -> Self {
108		Self(s)
109	}
110}
111
112impl<'a> From<&'a Key> for KeyRef<'a> {
113	fn from(k: &'a Key) -> Self {
114		Self(&k.0)
115	}
116}
117
118impl fmt::Display for KeyRef<'_> {
119	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120		self.0.fmt(f)
121	}
122}
123
124#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
125pub enum KeyOrKeyword {
126	Keyword(Keyword),
127	Key(Key),
128}
129
130impl KeyOrKeyword {
131	pub fn is_empty(&self) -> bool {
132		match self {
133			Self::Keyword(_) => false,
134			Self::Key(k) => k.is_empty(),
135		}
136	}
137
138	pub fn into_keyword(self) -> Option<Keyword> {
139		match self {
140			Self::Keyword(k) => Some(k),
141			Self::Key(_) => None,
142		}
143	}
144
145	pub fn into_key(self) -> Option<Key> {
146		match self {
147			Self::Keyword(_) => None,
148			Self::Key(k) => Some(k),
149		}
150	}
151
152	pub fn as_keyword(&self) -> Option<Keyword> {
153		match self {
154			Self::Keyword(k) => Some(*k),
155			Self::Key(_) => None,
156		}
157	}
158
159	pub fn as_key(&self) -> Option<&Key> {
160		match self {
161			Self::Keyword(_) => None,
162			Self::Key(k) => Some(k),
163		}
164	}
165
166	pub fn as_str(&self) -> &str {
167		match self {
168			Self::Keyword(k) => k.into_str(),
169			Self::Key(k) => k.as_str(),
170		}
171	}
172}
173
174#[allow(clippy::derived_hash_with_manual_eq)]
175impl Hash for KeyOrKeyword {
176	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
177		self.as_str().hash(state)
178	}
179}
180
181impl fmt::Display for KeyOrKeyword {
182	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183		match self {
184			Self::Key(k) => k.fmt(f),
185			Self::Keyword(k) => k.fmt(f),
186		}
187	}
188}
189
190#[derive(Clone, Copy, PartialEq, Eq)]
191pub enum KeyOrKeywordRef<'a> {
192	Keyword(Keyword),
193	Key(KeyRef<'a>),
194}
195
196impl<'a> KeyOrKeywordRef<'a> {
197	pub fn to_owned(self) -> KeyOrKeyword {
198		match self {
199			Self::Keyword(k) => KeyOrKeyword::Keyword(k),
200			Self::Key(k) => KeyOrKeyword::Key(k.to_owned()),
201		}
202	}
203
204	pub fn as_str(&self) -> &'a str {
205		match self {
206			Self::Keyword(k) => k.into_str(),
207			Self::Key(k) => k.as_str(),
208		}
209	}
210}
211
212impl<'a> From<&'a str> for KeyOrKeywordRef<'a> {
213	fn from(s: &'a str) -> Self {
214		match Keyword::try_from(s) {
215			Ok(k) => Self::Keyword(k),
216			Err(_) => Self::Key(s.into()),
217		}
218	}
219}
220
221impl<'a> From<&'a KeyOrKeyword> for KeyOrKeywordRef<'a> {
222	fn from(k: &'a KeyOrKeyword) -> Self {
223		match k {
224			KeyOrKeyword::Keyword(k) => Self::Keyword(*k),
225			KeyOrKeyword::Key(k) => Self::Key(k.into()),
226		}
227	}
228}
229
230impl<'a> From<KeyRef<'a>> for KeyOrKeywordRef<'a> {
231	fn from(k: KeyRef<'a>) -> Self {
232		Self::Key(k)
233	}
234}
235
236impl<'a> From<&'a Key> for KeyOrKeywordRef<'a> {
237	fn from(k: &'a Key) -> Self {
238		Self::Key(k.into())
239	}
240}
241
242pub enum KeyOrType {
243	Key(Key),
244	Type,
245}
246
247impl KeyOrType {
248	pub fn as_str(&self) -> &str {
249		match self {
250			Self::Key(k) => k.as_str(),
251			Self::Type => "@type",
252		}
253	}
254}
255
256impl fmt::Display for KeyOrType {
257	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
258		self.as_str().fmt(f)
259	}
260}