1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::convert::TryFrom;
use std::fmt;

/// Error code.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum ErrorCode {
	/// Two properties which expand to the same keyword have been detected.
	/// This might occur if a keyword and an alias thereof are used at the same time.
	CollidingKeywords,

	/// Multiple conflicting indexes have been found for the same node.
	ConflictingIndexes,

	/// maximum number of `@context` URLs exceeded.
	ContextOverflow,

	/// A cycle in IRI mappings has been detected.
	CyclicIriMapping,

	/// An `@id` entry was encountered whose value was not a string.
	InvalidIdValue,

	/// An invalid value for `@import` has been found.
	InvalidImportValue,

	/// An included block contains an invalid value.
	InvalidIncludedValue,

	/// An `@index` entry was encountered whose value was not a string.
	InvalidIndexValue,

	/// An invalid value for `@nest` has been found.
	InvalidNestValue,

	/// An invalid value for `@prefix` has been found.
	InvalidPrefixValue,

	/// An invalid value for `@propagate` has been found.
	InvalidPropagateValue,

	/// An invalid value for `@protected` has been found.
	InvalidProtectedValue,

	/// An invalid value for an `@reverse` entry has been detected, i.e., the value was not a map.
	InvalidReverseValue,

	/// The `@version` entry was used in a context with an out of range value.
	InvalidVersionValue,

	/// The value of `@direction` is not "ltr", "rtl", or null and thus invalid.
	InvalidBaseDirection,

	/// An invalid base IRI has been detected, i.e., it is neither an IRI nor null.
	InvalidBaseIri,

	/// An `@container` entry was encountered whose value was not one of the following strings:
	/// `@list`, `@set`, or `@index`.
	InvalidContainerMapping,

	/// An entry in a context is invalid due to processing mode incompatibility.
	InvalidContextEntry,

	/// An attempt was made to nullify a context containing protected term definitions.
	InvalidContextNullification,

	/// The value of the default language is not a string or null and thus invalid.
	InvalidDefaultLanguage,

	/// A local context contains a term that has an invalid or missing IRI mapping.
	InvalidIriMapping,

	/// An invalid JSON literal was detected.
	InvalidJsonLiteral,

	/// An invalid keyword alias definition has been encountered.
	InvalidKeywordAlias,

	/// An invalid value in a language map has been detected. It MUST be a string or an array of
	/// strings.
	InvalidLanguageMapValue,

	/// An `@language` entry in a term definition was encountered whose value was neither a string
	/// nor null and thus invalid.
	InvalidLanguageMapping,

	/// A language-tagged string with an invalid language value was detected.
	InvalidLanguageTaggedString,

	/// A number, true, or false with an associated language tag was detected.
	InvalidLanguageTaggedValue,

	/// An invalid local context was detected.
	InvalidLocalContext,

	/// No valid context document has been found for a referenced remote context.
	InvalidRemoteContext,

	/// An invalid reverse property definition has been detected.
	InvalidReverseProperty,

	/// An invalid reverse property map has been detected. No keywords apart from `@context` are
	/// allowed in reverse property maps.
	InvalidReversePropertyMap,

	/// An invalid value for a reverse property has been detected. The value of an inverse property
	/// must be a node object.
	InvalidReversePropertyValue,

	/// The local context defined within a term definition is invalid.
	InvalidScopedContext,

	/// A script element in HTML input which is the target of a fragment identifier does not have
	/// an appropriate type attribute.
	InvalidScriptElement,

	/// A set object or list object with disallowed entries has been detected.
	InvalidSetOrListObject,

	/// An invalid term definition has been detected.
	InvalidTermDefinition,

	/// An `@type` entry in a term definition was encountered whose value could not be expanded to an
	/// IRI.
	InvalidTypeMapping,

	/// An invalid value for an `@type` entry has been detected, i.e., the value was neither a string
	/// nor an array of strings.
	InvalidTypeValue,

	/// A typed value with an invalid type was detected.
	InvalidTypedValue,

	/// A value object with disallowed entries has been detected.
	InvalidValueObject,

	/// An invalid value for the `@value` entry of a value object has been detected, i.e., it is
	/// neither a scalar nor null.
	InvalidValueObjectValue,

	/// An invalid vocabulary mapping has been detected, i.e., it is neither an IRI nor null.
	InvalidVocabMapping,

	/// When compacting an IRI would result in an IRI which could be confused with a compact IRI
	/// (because its IRI scheme matches a term definition and it has no IRI authority).
	IriConfusedWithPrefix,

	/// Unable to expand a key into a IRI, blank node identifier or keyword
	/// using the current key expansion policy.
	/// Note: this error is not defined in the JSON-LD API specification.
	KeyExpansionFailed,

	/// A keyword redefinition has been detected.
	KeywordRedefinition,

	/// The document could not be loaded or parsed as JSON.
	LoadingDocumentFailed,

	/// There was a problem encountered loading a remote context.
	LoadingRemoteContextFailed,

	/// Multiple HTTP Link Headers [RFC8288](https://tools.ietf.org/html/rfc8288) using the <http://www.w3.org/ns/json-ld#context> link
	/// relation have been detected.
	MultipleContextLinkHeaders,

	/// An attempt was made to change the processing mode which is incompatible with the previous
	/// specified version.
	ProcessingModeConflict,

	/// An attempt was made to redefine a protected term.
	ProtectedTermRedefinition,

	/// Duplicate key in JSON object.
	DuplicateKey,
}

impl ErrorCode {
	/// Get the error message corresponding to the error code.
	pub fn as_str(&self) -> &str {
		use ErrorCode::*;

		match self {
			CollidingKeywords => "colliding keywords",
			ConflictingIndexes => "conflicting indexes",
			ContextOverflow => "context overflow",
			CyclicIriMapping => "cyclic IRI mapping",
			InvalidIdValue => "invalid @id value",
			InvalidImportValue => "invalid @import value",
			InvalidIncludedValue => "invalid @included value",
			InvalidIndexValue => "invalid @index value",
			InvalidNestValue => "invalid @nest value",
			InvalidPrefixValue => "invalid @prefix value",
			InvalidPropagateValue => "invalid @propagate value",
			InvalidProtectedValue => "invalid @protected value",
			InvalidReverseValue => "invalid @reverse value",
			InvalidVersionValue => "invalid @version value",
			InvalidBaseDirection => "invalid base direction",
			InvalidBaseIri => "invalid base IRI",
			InvalidContainerMapping => "invalid container mapping",
			InvalidContextEntry => "invalid context entry",
			InvalidContextNullification => "invalid context nullification",
			InvalidDefaultLanguage => "invalid default language",
			InvalidIriMapping => "invalid IRI mapping",
			InvalidJsonLiteral => "invalid JSON literal",
			InvalidKeywordAlias => "invalid keyword alias",
			InvalidLanguageMapValue => "invalid language map value",
			InvalidLanguageMapping => "invalid language mapping",
			InvalidLanguageTaggedString => "invalid language-tagged string",
			InvalidLanguageTaggedValue => "invalid language-tagged value",
			InvalidLocalContext => "invalid local context",
			InvalidRemoteContext => "invalid remote context",
			InvalidReverseProperty => "invalid reverse property",
			InvalidReversePropertyMap => "invalid reverse property map",
			InvalidReversePropertyValue => "invalid reverse property value",
			InvalidScopedContext => "invalid scoped context",
			InvalidScriptElement => "invalid script element",
			InvalidSetOrListObject => "invalid set or list object",
			InvalidTermDefinition => "invalid term definition",
			InvalidTypeMapping => "invalid type mapping",
			InvalidTypeValue => "invalid type value",
			InvalidTypedValue => "invalid typed value",
			InvalidValueObject => "invalid value object",
			InvalidValueObjectValue => "invalid value object value",
			InvalidVocabMapping => "invalid vocab mapping",
			IriConfusedWithPrefix => "IRI confused with prefix",
			KeyExpansionFailed => "key expansion failed",
			KeywordRedefinition => "keyword redefinition",
			LoadingDocumentFailed => "loading document failed",
			LoadingRemoteContextFailed => "loading remote context failed",
			MultipleContextLinkHeaders => "multiple context link headers",
			ProcessingModeConflict => "processing mode conflict",
			ProtectedTermRedefinition => "protected term redefinition",
			DuplicateKey => "duplicate key",
		}
	}
}

impl<'a> TryFrom<&'a str> for ErrorCode {
	type Error = ();

	fn try_from(name: &'a str) -> Result<ErrorCode, ()> {
		use ErrorCode::*;
		match name {
			"colliding keywords" => Ok(CollidingKeywords),
			"conflicting indexes" => Ok(ConflictingIndexes),
			"context overflow" => Ok(ContextOverflow),
			"cyclic IRI mapping" => Ok(CyclicIriMapping),
			"invalid @id value" => Ok(InvalidIdValue),
			"invalid @import value" => Ok(InvalidImportValue),
			"invalid @included value" => Ok(InvalidIncludedValue),
			"invalid @index value" => Ok(InvalidIndexValue),
			"invalid @nest value" => Ok(InvalidNestValue),
			"invalid @prefix value" => Ok(InvalidPrefixValue),
			"invalid @propagate value" => Ok(InvalidPropagateValue),
			"invalid @protected value" => Ok(InvalidProtectedValue),
			"invalid @reverse value" => Ok(InvalidReverseValue),
			"invalid @version value" => Ok(InvalidVersionValue),
			"invalid base direction" => Ok(InvalidBaseDirection),
			"invalid base IRI" => Ok(InvalidBaseIri),
			"invalid container mapping" => Ok(InvalidContainerMapping),
			"invalid context entry" => Ok(InvalidContextEntry),
			"invalid context nullification" => Ok(InvalidContextNullification),
			"invalid default language" => Ok(InvalidDefaultLanguage),
			"invalid IRI mapping" => Ok(InvalidIriMapping),
			"invalid JSON literal" => Ok(InvalidJsonLiteral),
			"invalid keyword alias" => Ok(InvalidKeywordAlias),
			"invalid language map value" => Ok(InvalidLanguageMapValue),
			"invalid language mapping" => Ok(InvalidLanguageMapping),
			"invalid language-tagged string" => Ok(InvalidLanguageTaggedString),
			"invalid language-tagged value" => Ok(InvalidLanguageTaggedValue),
			"invalid local context" => Ok(InvalidLocalContext),
			"invalid remote context" => Ok(InvalidRemoteContext),
			"invalid reverse property" => Ok(InvalidReverseProperty),
			"invalid reverse property map" => Ok(InvalidReversePropertyMap),
			"invalid reverse property value" => Ok(InvalidReversePropertyValue),
			"invalid scoped context" => Ok(InvalidScopedContext),
			"invalid script element" => Ok(InvalidScriptElement),
			"invalid set or list object" => Ok(InvalidSetOrListObject),
			"invalid term definition" => Ok(InvalidTermDefinition),
			"invalid type mapping" => Ok(InvalidTypeMapping),
			"invalid type value" => Ok(InvalidTypeValue),
			"invalid typed value" => Ok(InvalidTypedValue),
			"invalid value object" => Ok(InvalidValueObject),
			"invalid value object value" => Ok(InvalidValueObjectValue),
			"invalid vocab mapping" => Ok(InvalidVocabMapping),
			"IRI confused with prefix" => Ok(IriConfusedWithPrefix),
			"key expansion failed" => Ok(KeyExpansionFailed),
			"keyword redefinition" => Ok(KeywordRedefinition),
			"loading document failed" => Ok(LoadingDocumentFailed),
			"loading remote context failed" => Ok(LoadingRemoteContextFailed),
			"multiple context link headers" => Ok(MultipleContextLinkHeaders),
			"processing mode conflict" => Ok(ProcessingModeConflict),
			"protected term redefinition" => Ok(ProtectedTermRedefinition),
			_ => Err(()),
		}
	}
}

impl fmt::Display for ErrorCode {
	#[inline(always)]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{}", self.as_str())
	}
}