Skip to main content

json_ld/syntax/context/definition/term/
mod.rs

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