1pub use json_event_parser::TextPosition;
2use json_event_parser::{JsonParseError, JsonSyntaxError};
3use std::fmt::Formatter;
4use std::ops::Range;
5use std::{fmt, io};
6
7#[derive(Debug, thiserror::Error)]
9pub enum JsonLdParseError {
10 #[error(transparent)]
12 Io(#[from] io::Error),
13 #[error(transparent)]
15 Syntax(#[from] JsonLdSyntaxError),
16}
17
18impl From<JsonLdParseError> for io::Error {
19 #[inline]
20 fn from(error: JsonLdParseError) -> Self {
21 match error {
22 JsonLdParseError::Io(error) => error,
23 JsonLdParseError::Syntax(error) => error.into(),
24 }
25 }
26}
27
28#[doc(hidden)]
29impl From<JsonParseError> for JsonLdParseError {
30 #[inline]
31 fn from(error: JsonParseError) -> Self {
32 match error {
33 JsonParseError::Io(error) => Self::Io(error),
34 JsonParseError::Syntax(error) => Self::Syntax(error.into()),
35 }
36 }
37}
38
39#[derive(Debug, thiserror::Error)]
41#[error(transparent)]
42pub struct JsonLdSyntaxError(#[from] SyntaxErrorKind);
43
44#[derive(Debug, thiserror::Error)]
45enum SyntaxErrorKind {
46 #[error(transparent)]
47 Json(#[from] JsonSyntaxError),
48 #[error("{msg}")]
49 Msg {
50 msg: String,
51 code: Option<JsonLdErrorCode>,
52 },
53}
54
55impl JsonLdSyntaxError {
56 pub fn code(&self) -> Option<JsonLdErrorCode> {
58 match &self.0 {
59 SyntaxErrorKind::Json(_) => None,
60 SyntaxErrorKind::Msg { code, .. } => *code,
61 }
62 }
63
64 pub fn location(&self) -> Option<Range<TextPosition>> {
66 match &self.0 {
67 SyntaxErrorKind::Json(e) => Some(e.location()),
68 SyntaxErrorKind::Msg { .. } => None,
69 }
70 }
71
72 pub(crate) fn msg(msg: impl Into<String>) -> Self {
74 Self(SyntaxErrorKind::Msg {
75 msg: msg.into(),
76 code: None,
77 })
78 }
79
80 pub(crate) fn msg_and_code(msg: impl Into<String>, code: JsonLdErrorCode) -> Self {
82 Self(SyntaxErrorKind::Msg {
83 msg: msg.into(),
84 code: Some(code),
85 })
86 }
87}
88
89impl From<JsonLdSyntaxError> for io::Error {
90 #[inline]
91 fn from(error: JsonLdSyntaxError) -> Self {
92 match error.0 {
93 SyntaxErrorKind::Json(error) => error.into(),
94 SyntaxErrorKind::Msg { msg, .. } => Self::new(io::ErrorKind::InvalidData, msg),
95 }
96 }
97}
98
99#[doc(hidden)]
100impl From<JsonSyntaxError> for JsonLdSyntaxError {
101 #[inline]
102 fn from(error: JsonSyntaxError) -> Self {
103 Self(SyntaxErrorKind::Json(error))
104 }
105}
106
107#[derive(Debug, Clone, Copy)]
109#[non_exhaustive]
110pub enum JsonLdErrorCode {
111 CollidingKeywords,
114 ConflictingIndexes,
116 ContextOverflow,
118 CyclicIriMapping,
120 InvalidIdValue,
122 InvalidImportValue,
124 InvalidIncludedValue,
126 InvalidIndexValue,
128 InvalidNestValue,
130 InvalidPrefixValue,
132 InvalidPropagateValue,
134 InvalidProtectedValue,
136 InvalidReverseValue,
138 InvalidVersionValue,
140 InvalidBaseDirection,
142 InvalidBaseIri,
144 InvalidContainerMapping,
147 InvalidContextEntry,
149 InvalidContextNullification,
151 InvalidDefaultLanguage,
153 InvalidIriMapping,
155 InvalidJsonLiteral,
157 InvalidKeywordAlias,
159 InvalidLanguageMapValue,
162 InvalidLanguageMapping,
165 InvalidLanguageTaggedString,
167 InvalidLanguageTaggedValue,
169 InvalidLocalContext,
171 InvalidRemoteContext,
173 InvalidReverseProperty,
175 InvalidReversePropertyMap,
178 InvalidReversePropertyValue,
181 InvalidScopedContext,
183 InvalidSetOrListObject,
185 InvalidStreamingKeyOrder,
187 InvalidTermDefinition,
189 InvalidTypeMapping,
191 InvalidTypeValue,
194 InvalidTypedValue,
196 InvalidValueObject,
198 InvalidValueObjectValue,
201 InvalidVocabMapping,
203 IriConfusedWithPrefix,
206 KeywordRedefinition,
208 LoadingDocumentFailed,
210 LoadingRemoteContextFailed,
212 ProcessingModeConflict,
214 ProtectedTermRedefinition,
216}
217
218impl fmt::Display for JsonLdErrorCode {
219 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
220 f.write_str(match self {
221 Self::CollidingKeywords => "colliding keywords",
222 Self::ConflictingIndexes => "conflicting indexes",
223 Self::ContextOverflow => "context overflow",
224 Self::CyclicIriMapping => "cyclic IRI mapping",
225 Self::InvalidIdValue => "invalid @id value",
226 Self::InvalidImportValue => "invalid @import value",
227 Self::InvalidIncludedValue => "invalid @included value",
228 Self::InvalidIndexValue => "invalid @index value",
229 Self::InvalidNestValue => "invalid @nest value",
230 Self::InvalidPrefixValue => "invalid @prefix value",
231 Self::InvalidPropagateValue => "invalid @propagate value",
232 Self::InvalidProtectedValue => "invalid @protected value",
233 Self::InvalidReverseValue => "invalid @reverse value",
234 Self::InvalidVersionValue => "invalid @version value",
235 Self::InvalidBaseDirection => "invalid base direction",
236 Self::InvalidBaseIri => "invalid base IRI",
237 Self::InvalidContainerMapping => "invalid container mapping",
238 Self::InvalidContextEntry => "invalid context entry",
239 Self::InvalidContextNullification => "invalid context nullification",
240 Self::InvalidDefaultLanguage => "invalid default language",
241 Self::InvalidIriMapping => "invalid IRI mapping",
242 Self::InvalidJsonLiteral => "invalid JSON literal",
243 Self::InvalidKeywordAlias => "invalid keyword alias",
244 Self::InvalidLanguageMapValue => "invalid language map value",
245 Self::InvalidLanguageMapping => "invalid language mapping",
246 Self::InvalidLanguageTaggedString => "invalid language-tagged string",
247 Self::InvalidLanguageTaggedValue => "invalid language-tagged value",
248 Self::InvalidLocalContext => "invalid local context",
249 Self::InvalidRemoteContext => "invalid remote context",
250 Self::InvalidReverseProperty => "invalid reverse property",
251 Self::InvalidReversePropertyMap => "invalid reverse property map",
252 Self::InvalidReversePropertyValue => "invalid reverse property value",
253 Self::InvalidScopedContext => "invalid scoped context",
254 Self::InvalidSetOrListObject => "invalid set or list object",
255 Self::InvalidStreamingKeyOrder => "invalid streaming key order",
256 Self::InvalidTermDefinition => "invalid term definition",
257 Self::InvalidTypeMapping => "invalid type mapping",
258 Self::InvalidTypeValue => "invalid type value",
259 Self::InvalidTypedValue => "invalid typed value",
260 Self::InvalidValueObject => "invalid value object",
261 Self::InvalidValueObjectValue => "invalid value object value",
262 Self::InvalidVocabMapping => "invalid vocab mapping",
263 Self::IriConfusedWithPrefix => "IRI confused with prefix",
264 Self::KeywordRedefinition => "keyword redefinition",
265 Self::LoadingDocumentFailed => "loading document failed",
266 Self::LoadingRemoteContextFailed => "loading remote context failed",
267 Self::ProcessingModeConflict => "processing mode conflict",
268 Self::ProtectedTermRedefinition => "protected term redefinition",
269 })
270 }
271}