1use btree_indexmap::{Comparable, Equivalent};
2use rdf_syntax::{BlankId, BlankIdBuf, Id};
3use rdf_syntax::{Iri, IriBuf};
4use std::cmp::Ordering;
5use std::convert::TryFrom;
6
7use crate::{JsonLdLenient, JsonLdTerm, JsonLdValidate};
8
9impl JsonLdValidate for Id {
10 type Invalid = String;
11}
12
13impl JsonLdLenient<Id> {
14 pub fn iri(iri: IriBuf) -> Self {
15 Self::Valid(Id::Iri(iri))
16 }
17
18 pub fn blank(b: BlankIdBuf) -> Self {
19 Self::Valid(Id::BlankId(b))
20 }
21
22 pub fn into_blank(self) -> Option<BlankIdBuf> {
23 match self {
24 Self::Valid(Id::BlankId(b)) => Some(b),
25 _ => None,
26 }
27 }
28
29 #[inline(always)]
30 pub fn is_blank(&self) -> bool {
31 matches!(self, Self::Valid(Id::BlankId(_)))
32 }
33
34 #[inline(always)]
35 pub fn as_blank(&self) -> Option<&BlankId> {
36 match self {
37 Self::Valid(Id::BlankId(k)) => Some(k),
38 _ => None,
39 }
40 }
41
42 #[inline(always)]
43 pub fn is_iri(&self) -> bool {
44 matches!(self, Self::Valid(Id::Iri(_)))
45 }
46
47 #[inline(always)]
48 pub fn as_iri(&self) -> Option<&Iri> {
49 match self {
50 Self::Valid(Id::Iri(k)) => Some(k),
51 _ => None,
52 }
53 }
54
55 #[inline(always)]
56 pub fn into_term(self) -> JsonLdTerm {
57 JsonLdTerm::Id(self)
58 }
59}
60
61impl Equivalent<JsonLdLenient<Id>> for Id {
62 fn equivalent(&self, key: &JsonLdLenient<Id>) -> bool {
63 match key {
64 JsonLdLenient::Valid(id) => self == id,
65 _ => false,
66 }
67 }
68}
69
70impl Comparable<JsonLdLenient<Id>> for Id {
71 fn compare(&self, key: &JsonLdLenient<Id>) -> Ordering {
72 match key {
73 JsonLdLenient::Valid(id) => self.cmp(id),
74 JsonLdLenient::Invalid(_) => Ordering::Less, }
76 }
77}
78
79impl Equivalent<JsonLdLenient<Id>> for &Iri {
80 fn equivalent(&self, key: &JsonLdLenient<Id>) -> bool {
81 match key {
82 JsonLdLenient::Valid(Id::Iri(iri)) => *self == iri,
83 _ => false,
84 }
85 }
86}
87
88impl Comparable<JsonLdLenient<Id>> for &Iri {
89 fn compare(&self, key: &JsonLdLenient<Id>) -> Ordering {
90 match key {
91 JsonLdLenient::Valid(Id::Iri(iri)) => (*self).cmp(iri.as_iri()),
92 JsonLdLenient::Valid(Id::BlankId(_)) => Ordering::Greater, JsonLdLenient::Invalid(_) => Ordering::Less,
94 }
95 }
96}
97
98impl Equivalent<JsonLdLenient<Id>> for rdf_syntax::IriBuf {
99 fn equivalent(&self, key: &JsonLdLenient<Id>) -> bool {
100 match key {
101 JsonLdLenient::Valid(Id::Iri(iri)) => self == iri,
102 _ => false,
103 }
104 }
105}
106
107impl Comparable<JsonLdLenient<Id>> for rdf_syntax::IriBuf {
108 fn compare(&self, key: &JsonLdLenient<Id>) -> Ordering {
109 match key {
110 JsonLdLenient::Valid(Id::Iri(iri)) => self.as_iri().cmp(iri.as_iri()),
111 JsonLdLenient::Valid(Id::BlankId(_)) => Ordering::Greater,
112 JsonLdLenient::Invalid(_) => Ordering::Less,
113 }
114 }
115}
116
117impl Equivalent<JsonLdLenient<Id>> for rdf_syntax::BlankId {
118 fn equivalent(&self, key: &JsonLdLenient<Id>) -> bool {
119 match key {
120 JsonLdLenient::Valid(Id::BlankId(b)) => self == b,
121 _ => false,
122 }
123 }
124}
125
126impl Comparable<JsonLdLenient<Id>> for rdf_syntax::BlankId {
127 fn compare(&self, key: &JsonLdLenient<Id>) -> Ordering {
128 match key {
129 JsonLdLenient::Valid(Id::BlankId(b)) => self.cmp(b),
130 JsonLdLenient::Valid(Id::Iri(_)) => Ordering::Less, JsonLdLenient::Invalid(_) => Ordering::Less,
132 }
133 }
134}
135
136impl Equivalent<JsonLdLenient<Id>> for rdf_syntax::BlankIdBuf {
137 fn equivalent(&self, key: &JsonLdLenient<Id>) -> bool {
138 match key {
139 JsonLdLenient::Valid(Id::BlankId(b)) => self == b,
140 _ => false,
141 }
142 }
143}
144
145impl Comparable<JsonLdLenient<Id>> for rdf_syntax::BlankIdBuf {
146 fn compare(&self, key: &JsonLdLenient<Id>) -> Ordering {
147 match key {
148 JsonLdLenient::Valid(Id::BlankId(b)) => self.as_blank_id().cmp(b),
149 JsonLdLenient::Valid(Id::Iri(_)) => Ordering::Less,
150 JsonLdLenient::Invalid(_) => Ordering::Less,
151 }
152 }
153}
154
155impl From<IriBuf> for JsonLdLenient<Id> {
156 #[inline(always)]
157 fn from(iri: IriBuf) -> Self {
158 Self::iri(iri)
159 }
160}
161
162impl PartialEq<Iri> for JsonLdLenient<Id> {
163 fn eq(&self, other: &Iri) -> bool {
164 self.as_iri() == Some(other)
165 }
166}
167
168impl PartialEq<JsonLdTerm> for Id {
169 #[inline]
170 fn eq(&self, term: &JsonLdTerm) -> bool {
171 match term {
172 JsonLdTerm::Id(JsonLdLenient::Valid(prop)) => self == prop,
173 _ => false,
174 }
175 }
176}
177
178impl PartialEq<JsonLdLenient<Id>> for JsonLdTerm {
179 #[inline]
180 fn eq(&self, r: &JsonLdLenient<Id>) -> bool {
181 match self {
182 JsonLdTerm::Id(prop) => prop == r,
183 _ => false,
184 }
185 }
186}
187
188impl TryFrom<JsonLdTerm> for JsonLdLenient<Id> {
189 type Error = JsonLdTerm;
190
191 #[inline]
192 fn try_from(term: JsonLdTerm) -> Result<Self, JsonLdTerm> {
193 match term {
194 JsonLdTerm::Id(prop) => Ok(prop),
195 term => Err(term),
196 }
197 }
198}
199
200impl TryFrom<JsonLdLenient<Id>> for Id {
201 type Error = String;
202
203 fn try_from(r: JsonLdLenient<Id>) -> Result<Self, Self::Error> {
204 match r {
205 JsonLdLenient::Valid(r) => Ok(r),
206 JsonLdLenient::Invalid(id) => Err(id),
207 }
208 }
209}
210
211impl<'a> TryFrom<&'a JsonLdLenient<Id>> for &'a Id {
212 type Error = &'a String;
213
214 fn try_from(r: &'a JsonLdLenient<Id>) -> Result<Self, Self::Error> {
215 match r {
216 JsonLdLenient::Valid(r) => Ok(r),
217 JsonLdLenient::Invalid(id) => Err(id),
218 }
219 }
220}
221
222impl<'a> TryFrom<&'a mut JsonLdLenient<Id>> for &'a mut Id {
223 type Error = &'a mut String;
224
225 fn try_from(r: &'a mut JsonLdLenient<Id>) -> Result<Self, Self::Error> {
226 match r {
227 JsonLdLenient::Valid(r) => Ok(r),
228 JsonLdLenient::Invalid(id) => Err(id),
229 }
230 }
231}
232
233impl Equivalent<JsonLdLenient<Id>> for Iri {
234 fn equivalent(&self, key: &JsonLdLenient<Id>) -> bool {
235 match key.as_iri() {
236 Some(iri) => self == iri,
237 None => false,
238 }
239 }
240}
241
242impl Comparable<JsonLdLenient<Id>> for Iri {
243 fn compare(&self, key: &JsonLdLenient<Id>) -> Ordering {
244 match key {
245 JsonLdLenient::Valid(Id::Iri(iri)) => self.cmp(iri.as_iri()),
246 JsonLdLenient::Valid(Id::BlankId(_)) => Ordering::Greater,
247 JsonLdLenient::Invalid(_) => Ordering::Less,
248 }
249 }
250}