json_ld_syntax/
error.rs

1use std::convert::TryFrom;
2use std::fmt;
3
4/// Error code.
5#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
6pub enum ErrorCode {
7	/// Two properties which expand to the same keyword have been detected.
8	/// This might occur if a keyword and an alias thereof are used at the same time.
9	CollidingKeywords,
10
11	/// Multiple conflicting indexes have been found for the same node.
12	ConflictingIndexes,
13
14	/// maximum number of `@context` URLs exceeded.
15	ContextOverflow,
16
17	/// A cycle in IRI mappings has been detected.
18	CyclicIriMapping,
19
20	/// An `@id` entry was encountered whose value was not a string.
21	InvalidIdValue,
22
23	/// An invalid value for `@import` has been found.
24	InvalidImportValue,
25
26	/// An included block contains an invalid value.
27	InvalidIncludedValue,
28
29	/// An `@index` entry was encountered whose value was not a string.
30	InvalidIndexValue,
31
32	/// An invalid value for `@nest` has been found.
33	InvalidNestValue,
34
35	/// An invalid value for `@prefix` has been found.
36	InvalidPrefixValue,
37
38	/// An invalid value for `@propagate` has been found.
39	InvalidPropagateValue,
40
41	/// An invalid value for `@protected` has been found.
42	InvalidProtectedValue,
43
44	/// An invalid value for an `@reverse` entry has been detected, i.e., the value was not a map.
45	InvalidReverseValue,
46
47	/// The `@version` entry was used in a context with an out of range value.
48	InvalidVersionValue,
49
50	/// The value of `@direction` is not "ltr", "rtl", or null and thus invalid.
51	InvalidBaseDirection,
52
53	/// An invalid base IRI has been detected, i.e., it is neither an IRI nor null.
54	InvalidBaseIri,
55
56	/// An `@container` entry was encountered whose value was not one of the following strings:
57	/// `@list`, `@set`, or `@index`.
58	InvalidContainerMapping,
59
60	/// An entry in a context is invalid due to processing mode incompatibility.
61	InvalidContextEntry,
62
63	/// An attempt was made to nullify a context containing protected term definitions.
64	InvalidContextNullification,
65
66	/// The value of the default language is not a string or null and thus invalid.
67	InvalidDefaultLanguage,
68
69	/// A local context contains a term that has an invalid or missing IRI mapping.
70	InvalidIriMapping,
71
72	/// An invalid JSON literal was detected.
73	InvalidJsonLiteral,
74
75	/// An invalid keyword alias definition has been encountered.
76	InvalidKeywordAlias,
77
78	/// An invalid value in a language map has been detected. It MUST be a string or an array of
79	/// strings.
80	InvalidLanguageMapValue,
81
82	/// An `@language` entry in a term definition was encountered whose value was neither a string
83	/// nor null and thus invalid.
84	InvalidLanguageMapping,
85
86	/// A language-tagged string with an invalid language value was detected.
87	InvalidLanguageTaggedString,
88
89	/// A number, true, or false with an associated language tag was detected.
90	InvalidLanguageTaggedValue,
91
92	/// An invalid local context was detected.
93	InvalidLocalContext,
94
95	/// No valid context document has been found for a referenced remote context.
96	InvalidRemoteContext,
97
98	/// An invalid reverse property definition has been detected.
99	InvalidReverseProperty,
100
101	/// An invalid reverse property map has been detected. No keywords apart from `@context` are
102	/// allowed in reverse property maps.
103	InvalidReversePropertyMap,
104
105	/// An invalid value for a reverse property has been detected. The value of an inverse property
106	/// must be a node object.
107	InvalidReversePropertyValue,
108
109	/// The local context defined within a term definition is invalid.
110	InvalidScopedContext,
111
112	/// A script element in HTML input which is the target of a fragment identifier does not have
113	/// an appropriate type attribute.
114	InvalidScriptElement,
115
116	/// A set object or list object with disallowed entries has been detected.
117	InvalidSetOrListObject,
118
119	/// An invalid term definition has been detected.
120	InvalidTermDefinition,
121
122	/// An `@type` entry in a term definition was encountered whose value could not be expanded to an
123	/// IRI.
124	InvalidTypeMapping,
125
126	/// An invalid value for an `@type` entry has been detected, i.e., the value was neither a string
127	/// nor an array of strings.
128	InvalidTypeValue,
129
130	/// A typed value with an invalid type was detected.
131	InvalidTypedValue,
132
133	/// A value object with disallowed entries has been detected.
134	InvalidValueObject,
135
136	/// An invalid value for the `@value` entry of a value object has been detected, i.e., it is
137	/// neither a scalar nor null.
138	InvalidValueObjectValue,
139
140	/// An invalid vocabulary mapping has been detected, i.e., it is neither an IRI nor null.
141	InvalidVocabMapping,
142
143	/// When compacting an IRI would result in an IRI which could be confused with a compact IRI
144	/// (because its IRI scheme matches a term definition and it has no IRI authority).
145	IriConfusedWithPrefix,
146
147	/// Unable to expand a key into a IRI, blank node identifier or keyword
148	/// using the current key expansion policy.
149	/// Note: this error is not defined in the JSON-LD API specification.
150	KeyExpansionFailed,
151
152	/// A keyword redefinition has been detected.
153	KeywordRedefinition,
154
155	/// The document could not be loaded or parsed as JSON.
156	LoadingDocumentFailed,
157
158	/// There was a problem encountered loading a remote context.
159	LoadingRemoteContextFailed,
160
161	/// Multiple HTTP Link Headers [RFC8288](https://tools.ietf.org/html/rfc8288) using the <http://www.w3.org/ns/json-ld#context> link
162	/// relation have been detected.
163	MultipleContextLinkHeaders,
164
165	/// An attempt was made to change the processing mode which is incompatible with the previous
166	/// specified version.
167	ProcessingModeConflict,
168
169	/// An attempt was made to redefine a protected term.
170	ProtectedTermRedefinition,
171
172	/// Duplicate key in JSON object.
173	DuplicateKey,
174}
175
176impl ErrorCode {
177	/// Get the error message corresponding to the error code.
178	pub fn as_str(&self) -> &str {
179		use ErrorCode::*;
180
181		match self {
182			CollidingKeywords => "colliding keywords",
183			ConflictingIndexes => "conflicting indexes",
184			ContextOverflow => "context overflow",
185			CyclicIriMapping => "cyclic IRI mapping",
186			InvalidIdValue => "invalid @id value",
187			InvalidImportValue => "invalid @import value",
188			InvalidIncludedValue => "invalid @included value",
189			InvalidIndexValue => "invalid @index value",
190			InvalidNestValue => "invalid @nest value",
191			InvalidPrefixValue => "invalid @prefix value",
192			InvalidPropagateValue => "invalid @propagate value",
193			InvalidProtectedValue => "invalid @protected value",
194			InvalidReverseValue => "invalid @reverse value",
195			InvalidVersionValue => "invalid @version value",
196			InvalidBaseDirection => "invalid base direction",
197			InvalidBaseIri => "invalid base IRI",
198			InvalidContainerMapping => "invalid container mapping",
199			InvalidContextEntry => "invalid context entry",
200			InvalidContextNullification => "invalid context nullification",
201			InvalidDefaultLanguage => "invalid default language",
202			InvalidIriMapping => "invalid IRI mapping",
203			InvalidJsonLiteral => "invalid JSON literal",
204			InvalidKeywordAlias => "invalid keyword alias",
205			InvalidLanguageMapValue => "invalid language map value",
206			InvalidLanguageMapping => "invalid language mapping",
207			InvalidLanguageTaggedString => "invalid language-tagged string",
208			InvalidLanguageTaggedValue => "invalid language-tagged value",
209			InvalidLocalContext => "invalid local context",
210			InvalidRemoteContext => "invalid remote context",
211			InvalidReverseProperty => "invalid reverse property",
212			InvalidReversePropertyMap => "invalid reverse property map",
213			InvalidReversePropertyValue => "invalid reverse property value",
214			InvalidScopedContext => "invalid scoped context",
215			InvalidScriptElement => "invalid script element",
216			InvalidSetOrListObject => "invalid set or list object",
217			InvalidTermDefinition => "invalid term definition",
218			InvalidTypeMapping => "invalid type mapping",
219			InvalidTypeValue => "invalid type value",
220			InvalidTypedValue => "invalid typed value",
221			InvalidValueObject => "invalid value object",
222			InvalidValueObjectValue => "invalid value object value",
223			InvalidVocabMapping => "invalid vocab mapping",
224			IriConfusedWithPrefix => "IRI confused with prefix",
225			KeyExpansionFailed => "key expansion failed",
226			KeywordRedefinition => "keyword redefinition",
227			LoadingDocumentFailed => "loading document failed",
228			LoadingRemoteContextFailed => "loading remote context failed",
229			MultipleContextLinkHeaders => "multiple context link headers",
230			ProcessingModeConflict => "processing mode conflict",
231			ProtectedTermRedefinition => "protected term redefinition",
232			DuplicateKey => "duplicate key",
233		}
234	}
235}
236
237impl<'a> TryFrom<&'a str> for ErrorCode {
238	type Error = ();
239
240	fn try_from(name: &'a str) -> Result<ErrorCode, ()> {
241		use ErrorCode::*;
242		match name {
243			"colliding keywords" => Ok(CollidingKeywords),
244			"conflicting indexes" => Ok(ConflictingIndexes),
245			"context overflow" => Ok(ContextOverflow),
246			"cyclic IRI mapping" => Ok(CyclicIriMapping),
247			"invalid @id value" => Ok(InvalidIdValue),
248			"invalid @import value" => Ok(InvalidImportValue),
249			"invalid @included value" => Ok(InvalidIncludedValue),
250			"invalid @index value" => Ok(InvalidIndexValue),
251			"invalid @nest value" => Ok(InvalidNestValue),
252			"invalid @prefix value" => Ok(InvalidPrefixValue),
253			"invalid @propagate value" => Ok(InvalidPropagateValue),
254			"invalid @protected value" => Ok(InvalidProtectedValue),
255			"invalid @reverse value" => Ok(InvalidReverseValue),
256			"invalid @version value" => Ok(InvalidVersionValue),
257			"invalid base direction" => Ok(InvalidBaseDirection),
258			"invalid base IRI" => Ok(InvalidBaseIri),
259			"invalid container mapping" => Ok(InvalidContainerMapping),
260			"invalid context entry" => Ok(InvalidContextEntry),
261			"invalid context nullification" => Ok(InvalidContextNullification),
262			"invalid default language" => Ok(InvalidDefaultLanguage),
263			"invalid IRI mapping" => Ok(InvalidIriMapping),
264			"invalid JSON literal" => Ok(InvalidJsonLiteral),
265			"invalid keyword alias" => Ok(InvalidKeywordAlias),
266			"invalid language map value" => Ok(InvalidLanguageMapValue),
267			"invalid language mapping" => Ok(InvalidLanguageMapping),
268			"invalid language-tagged string" => Ok(InvalidLanguageTaggedString),
269			"invalid language-tagged value" => Ok(InvalidLanguageTaggedValue),
270			"invalid local context" => Ok(InvalidLocalContext),
271			"invalid remote context" => Ok(InvalidRemoteContext),
272			"invalid reverse property" => Ok(InvalidReverseProperty),
273			"invalid reverse property map" => Ok(InvalidReversePropertyMap),
274			"invalid reverse property value" => Ok(InvalidReversePropertyValue),
275			"invalid scoped context" => Ok(InvalidScopedContext),
276			"invalid script element" => Ok(InvalidScriptElement),
277			"invalid set or list object" => Ok(InvalidSetOrListObject),
278			"invalid term definition" => Ok(InvalidTermDefinition),
279			"invalid type mapping" => Ok(InvalidTypeMapping),
280			"invalid type value" => Ok(InvalidTypeValue),
281			"invalid typed value" => Ok(InvalidTypedValue),
282			"invalid value object" => Ok(InvalidValueObject),
283			"invalid value object value" => Ok(InvalidValueObjectValue),
284			"invalid vocab mapping" => Ok(InvalidVocabMapping),
285			"IRI confused with prefix" => Ok(IriConfusedWithPrefix),
286			"key expansion failed" => Ok(KeyExpansionFailed),
287			"keyword redefinition" => Ok(KeywordRedefinition),
288			"loading document failed" => Ok(LoadingDocumentFailed),
289			"loading remote context failed" => Ok(LoadingRemoteContextFailed),
290			"multiple context link headers" => Ok(MultipleContextLinkHeaders),
291			"processing mode conflict" => Ok(ProcessingModeConflict),
292			"protected term redefinition" => Ok(ProtectedTermRedefinition),
293			_ => Err(()),
294		}
295	}
296}
297
298impl fmt::Display for ErrorCode {
299	#[inline(always)]
300	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
301		write!(f, "{}", self.as_str())
302	}
303}