Skip to main content

jsonld_syntax/
error.rs

1use std::{convert::TryFrom, fmt};
2
3/// JSON-LD error code.
4///
5/// One variant per error code of the JSON-LD 1.1 API specification, plus a few
6/// codes specific to this implementation (noted on the variants concerned).
7#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
8pub enum ErrorCode {
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    /// The maximum number of remote `@context` URLs has been 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 an IRI, blank node identifier or keyword
150    /// using the current key expansion policy.
151    ///
152    /// Specific to this implementation; not defined by the JSON-LD API
153    /// specification.
154    KeyExpansionFailed,
155
156    /// A keyword redefinition has been detected.
157    KeywordRedefinition,
158
159    /// The document could not be loaded or parsed as JSON.
160    LoadingDocumentFailed,
161
162    /// There was a problem encountered loading a remote context.
163    LoadingRemoteContextFailed,
164
165    /// Multiple HTTP Link Headers [RFC8288](https://tools.ietf.org/html/rfc8288) using the <http://www.w3.org/ns/json-ld#context> link
166    /// relation have been detected.
167    MultipleContextLinkHeaders,
168
169    /// An attempt was made to change the processing mode which is incompatible with the previous
170    /// specified version.
171    ProcessingModeConflict,
172
173    /// An attempt was made to redefine a protected term.
174    ProtectedTermRedefinition,
175
176    /// A cycle in remote context inclusions has been detected.
177    ///
178    /// JSON-LD 1.0 only: 1.1 replaced this with [`Self::ContextOverflow`] so
179    /// that scoped contexts may legally reload a context.
180    RecursiveContextInclusion,
181
182    /// A list of lists was detected.
183    ///
184    /// JSON-LD 1.0 only: 1.1 allows lists of lists.
185    ListOfLists,
186
187    /// The same key appeared twice in a JSON object.
188    ///
189    /// Specific to this implementation; not defined by the JSON-LD API
190    /// specification.
191    DuplicateKey,
192}
193
194impl ErrorCode {
195    /// Returns the name of this error code, as spelled by the JSON-LD API
196    /// specification: for instance `"colliding keywords"`.
197    ///
198    /// [`TryFrom<&str>`](TryFrom) parses these names back into error codes.
199    #[must_use]
200    pub fn as_str(&self) -> &str {
201        use ErrorCode::{
202            CollidingKeywords,
203            ConflictingIndexes,
204            ContextOverflow,
205            CyclicIriMapping,
206            DuplicateKey,
207            InvalidBaseDirection,
208            InvalidBaseIri,
209            InvalidContainerMapping,
210            InvalidContextEntry,
211            InvalidContextNullification,
212            InvalidDefaultLanguage,
213            InvalidIdValue,
214            InvalidImportValue,
215            InvalidIncludedValue,
216            InvalidIndexValue,
217            InvalidIriMapping,
218            InvalidJsonLiteral,
219            InvalidKeywordAlias,
220            InvalidLanguageMapValue,
221            InvalidLanguageMapping,
222            InvalidLanguageTaggedString,
223            InvalidLanguageTaggedValue,
224            InvalidLocalContext,
225            InvalidNestValue,
226            InvalidPrefixValue,
227            InvalidPropagateValue,
228            InvalidProtectedValue,
229            InvalidRemoteContext,
230            InvalidReverseProperty,
231            InvalidReversePropertyMap,
232            InvalidReversePropertyValue,
233            InvalidReverseValue,
234            InvalidScopedContext,
235            InvalidScriptElement,
236            InvalidSetOrListObject,
237            InvalidTermDefinition,
238            InvalidTypeMapping,
239            InvalidTypeValue,
240            InvalidTypedValue,
241            InvalidValueObject,
242            InvalidValueObjectValue,
243            InvalidVersionValue,
244            InvalidVocabMapping,
245            IriConfusedWithPrefix,
246            KeyExpansionFailed,
247            KeywordRedefinition,
248            ListOfLists,
249            LoadingDocumentFailed,
250            LoadingRemoteContextFailed,
251            MultipleContextLinkHeaders,
252            ProcessingModeConflict,
253            ProtectedTermRedefinition,
254            RecursiveContextInclusion,
255        };
256
257        match self {
258            CollidingKeywords => "colliding keywords",
259            ConflictingIndexes => "conflicting indexes",
260            ContextOverflow => "context overflow",
261            CyclicIriMapping => "cyclic IRI mapping",
262            InvalidIdValue => "invalid @id value",
263            InvalidImportValue => "invalid @import value",
264            InvalidIncludedValue => "invalid @included value",
265            InvalidIndexValue => "invalid @index value",
266            InvalidNestValue => "invalid @nest value",
267            InvalidPrefixValue => "invalid @prefix value",
268            InvalidPropagateValue => "invalid @propagate value",
269            InvalidProtectedValue => "invalid @protected value",
270            InvalidReverseValue => "invalid @reverse value",
271            InvalidVersionValue => "invalid @version value",
272            InvalidBaseDirection => "invalid base direction",
273            InvalidBaseIri => "invalid base IRI",
274            InvalidContainerMapping => "invalid container mapping",
275            InvalidContextEntry => "invalid context entry",
276            InvalidContextNullification => "invalid context nullification",
277            InvalidDefaultLanguage => "invalid default language",
278            InvalidIriMapping => "invalid IRI mapping",
279            InvalidJsonLiteral => "invalid JSON literal",
280            InvalidKeywordAlias => "invalid keyword alias",
281            InvalidLanguageMapValue => "invalid language map value",
282            InvalidLanguageMapping => "invalid language mapping",
283            InvalidLanguageTaggedString => "invalid language-tagged string",
284            InvalidLanguageTaggedValue => "invalid language-tagged value",
285            InvalidLocalContext => "invalid local context",
286            InvalidRemoteContext => "invalid remote context",
287            InvalidReverseProperty => "invalid reverse property",
288            InvalidReversePropertyMap => "invalid reverse property map",
289            InvalidReversePropertyValue => "invalid reverse property value",
290            InvalidScopedContext => "invalid scoped context",
291            InvalidScriptElement => "invalid script element",
292            InvalidSetOrListObject => "invalid set or list object",
293            InvalidTermDefinition => "invalid term definition",
294            InvalidTypeMapping => "invalid type mapping",
295            InvalidTypeValue => "invalid type value",
296            InvalidTypedValue => "invalid typed value",
297            InvalidValueObject => "invalid value object",
298            InvalidValueObjectValue => "invalid value object value",
299            InvalidVocabMapping => "invalid vocab mapping",
300            IriConfusedWithPrefix => "IRI confused with prefix",
301            KeyExpansionFailed => "key expansion failed",
302            KeywordRedefinition => "keyword redefinition",
303            LoadingDocumentFailed => "loading document failed",
304            LoadingRemoteContextFailed => "loading remote context failed",
305            MultipleContextLinkHeaders => "multiple context link headers",
306            ProcessingModeConflict => "processing mode conflict",
307            ProtectedTermRedefinition => "protected term redefinition",
308            RecursiveContextInclusion => "recursive context inclusion",
309            ListOfLists => "list of lists",
310            DuplicateKey => "duplicate key",
311        }
312    }
313}
314
315impl<'a> TryFrom<&'a str> for ErrorCode {
316    type Error = ();
317
318    fn try_from(name: &'a str) -> Result<ErrorCode, ()> {
319        use ErrorCode::{
320            CollidingKeywords,
321            ConflictingIndexes,
322            ContextOverflow,
323            CyclicIriMapping,
324            DuplicateKey,
325            InvalidBaseDirection,
326            InvalidBaseIri,
327            InvalidContainerMapping,
328            InvalidContextEntry,
329            InvalidContextNullification,
330            InvalidDefaultLanguage,
331            InvalidIdValue,
332            InvalidImportValue,
333            InvalidIncludedValue,
334            InvalidIndexValue,
335            InvalidIriMapping,
336            InvalidJsonLiteral,
337            InvalidKeywordAlias,
338            InvalidLanguageMapValue,
339            InvalidLanguageMapping,
340            InvalidLanguageTaggedString,
341            InvalidLanguageTaggedValue,
342            InvalidLocalContext,
343            InvalidNestValue,
344            InvalidPrefixValue,
345            InvalidPropagateValue,
346            InvalidProtectedValue,
347            InvalidRemoteContext,
348            InvalidReverseProperty,
349            InvalidReversePropertyMap,
350            InvalidReversePropertyValue,
351            InvalidReverseValue,
352            InvalidScopedContext,
353            InvalidScriptElement,
354            InvalidSetOrListObject,
355            InvalidTermDefinition,
356            InvalidTypeMapping,
357            InvalidTypeValue,
358            InvalidTypedValue,
359            InvalidValueObject,
360            InvalidValueObjectValue,
361            InvalidVersionValue,
362            InvalidVocabMapping,
363            IriConfusedWithPrefix,
364            KeyExpansionFailed,
365            KeywordRedefinition,
366            ListOfLists,
367            LoadingDocumentFailed,
368            LoadingRemoteContextFailed,
369            MultipleContextLinkHeaders,
370            ProcessingModeConflict,
371            ProtectedTermRedefinition,
372            RecursiveContextInclusion,
373        };
374        match name {
375            "colliding keywords" => Ok(CollidingKeywords),
376            "conflicting indexes" => Ok(ConflictingIndexes),
377            "context overflow" => Ok(ContextOverflow),
378            "cyclic IRI mapping" => Ok(CyclicIriMapping),
379            "invalid @id value" => Ok(InvalidIdValue),
380            "invalid @import value" => Ok(InvalidImportValue),
381            "invalid @included value" => Ok(InvalidIncludedValue),
382            "invalid @index value" => Ok(InvalidIndexValue),
383            "invalid @nest value" => Ok(InvalidNestValue),
384            "invalid @prefix value" => Ok(InvalidPrefixValue),
385            "invalid @propagate value" => Ok(InvalidPropagateValue),
386            "invalid @protected value" => Ok(InvalidProtectedValue),
387            "invalid @reverse value" => Ok(InvalidReverseValue),
388            "invalid @version value" => Ok(InvalidVersionValue),
389            "invalid base direction" => Ok(InvalidBaseDirection),
390            "invalid base IRI" => Ok(InvalidBaseIri),
391            "invalid container mapping" => Ok(InvalidContainerMapping),
392            "invalid context entry" => Ok(InvalidContextEntry),
393            "invalid context nullification" => Ok(InvalidContextNullification),
394            "invalid default language" => Ok(InvalidDefaultLanguage),
395            "invalid IRI mapping" => Ok(InvalidIriMapping),
396            "invalid JSON literal" => Ok(InvalidJsonLiteral),
397            "invalid keyword alias" => Ok(InvalidKeywordAlias),
398            "invalid language map value" => Ok(InvalidLanguageMapValue),
399            "invalid language mapping" => Ok(InvalidLanguageMapping),
400            "invalid language-tagged string" => Ok(InvalidLanguageTaggedString),
401            "invalid language-tagged value" => Ok(InvalidLanguageTaggedValue),
402            "invalid local context" => Ok(InvalidLocalContext),
403            "invalid remote context" => Ok(InvalidRemoteContext),
404            "invalid reverse property" => Ok(InvalidReverseProperty),
405            "invalid reverse property map" => Ok(InvalidReversePropertyMap),
406            "invalid reverse property value" => Ok(InvalidReversePropertyValue),
407            "invalid scoped context" => Ok(InvalidScopedContext),
408            "invalid script element" => Ok(InvalidScriptElement),
409            "invalid set or list object" => Ok(InvalidSetOrListObject),
410            "invalid term definition" => Ok(InvalidTermDefinition),
411            "invalid type mapping" => Ok(InvalidTypeMapping),
412            "invalid type value" => Ok(InvalidTypeValue),
413            "invalid typed value" => Ok(InvalidTypedValue),
414            "invalid value object" => Ok(InvalidValueObject),
415            "invalid value object value" => Ok(InvalidValueObjectValue),
416            "invalid vocab mapping" => Ok(InvalidVocabMapping),
417            "IRI confused with prefix" => Ok(IriConfusedWithPrefix),
418            "key expansion failed" => Ok(KeyExpansionFailed),
419            "keyword redefinition" => Ok(KeywordRedefinition),
420            "loading document failed" => Ok(LoadingDocumentFailed),
421            "loading remote context failed" => Ok(LoadingRemoteContextFailed),
422            "multiple context link headers" => Ok(MultipleContextLinkHeaders),
423            "processing mode conflict" => Ok(ProcessingModeConflict),
424            "protected term redefinition" => Ok(ProtectedTermRedefinition),
425            "recursive context inclusion" => Ok(RecursiveContextInclusion),
426            "list of lists" => Ok(ListOfLists),
427            "duplicate key" => Ok(DuplicateKey),
428            _ => Err(()),
429        }
430    }
431}
432
433impl fmt::Display for ErrorCode {
434    #[inline(always)]
435    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
436        write!(f, "{}", self.as_str())
437    }
438}