Skip to main content

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