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 #[error("Processing error: {0}")]
18 ProcessingError(String),
19}
20
21impl From<JsonLdParseError> for io::Error {
22 #[inline]
23 fn from(error: JsonLdParseError) -> Self {
24 match error {
25 JsonLdParseError::Io(error) => error,
26 JsonLdParseError::Syntax(error) => error.into(),
27 JsonLdParseError::ProcessingError(msg) => io::Error::new(io::ErrorKind::Other, msg),
28 }
29 }
30}
31
32#[doc(hidden)]
33impl From<JsonParseError> for JsonLdParseError {
34 #[inline]
35 fn from(error: JsonParseError) -> Self {
36 match error {
37 JsonParseError::Io(error) => Self::Io(error),
38 JsonParseError::Syntax(error) => Self::Syntax(error.into()),
39 }
40 }
41}
42
43impl From<crate::OxirsError> for JsonLdParseError {
44 fn from(err: crate::OxirsError) -> Self {
45 JsonLdParseError::ProcessingError(err.to_string())
46 }
47}
48
49#[derive(Debug, thiserror::Error)]
51#[error(transparent)]
52pub struct JsonLdSyntaxError(#[from] SyntaxErrorKind);
53
54#[derive(Debug, thiserror::Error)]
55enum SyntaxErrorKind {
56 #[error(transparent)]
57 Json(#[from] JsonSyntaxError),
58 #[error("{msg}")]
59 Msg {
60 msg: String,
61 code: Option<JsonLdErrorCode>,
62 },
63}
64
65impl JsonLdSyntaxError {
66 pub fn code(&self) -> Option<JsonLdErrorCode> {
68 match &self.0 {
69 SyntaxErrorKind::Json(_) => None,
70 SyntaxErrorKind::Msg { code, .. } => *code,
71 }
72 }
73
74 pub fn location(&self) -> Option<Range<TextPosition>> {
76 match &self.0 {
77 SyntaxErrorKind::Json(e) => Some(e.location()),
78 SyntaxErrorKind::Msg { .. } => None,
79 }
80 }
81
82 pub(crate) fn msg(msg: impl Into<String>) -> Self {
84 Self(SyntaxErrorKind::Msg {
85 msg: msg.into(),
86 code: None,
87 })
88 }
89
90 pub(crate) fn msg_and_code(msg: impl Into<String>, code: JsonLdErrorCode) -> Self {
92 Self(SyntaxErrorKind::Msg {
93 msg: msg.into(),
94 code: Some(code),
95 })
96 }
97}
98
99impl From<JsonLdSyntaxError> for io::Error {
100 #[inline]
101 fn from(error: JsonLdSyntaxError) -> Self {
102 match error.0 {
103 SyntaxErrorKind::Json(error) => error.into(),
104 SyntaxErrorKind::Msg { msg, .. } => Self::new(io::ErrorKind::InvalidData, msg),
105 }
106 }
107}
108
109#[doc(hidden)]
110impl From<JsonSyntaxError> for JsonLdSyntaxError {
111 #[inline]
112 fn from(error: JsonSyntaxError) -> Self {
113 Self(SyntaxErrorKind::Json(error))
114 }
115}
116
117#[derive(Debug, Clone, Copy)]
119#[non_exhaustive]
120pub enum JsonLdErrorCode {
121 CollidingKeywords,
124 ConflictingIndexes,
126 ContextOverflow,
128 CyclicIriMapping,
130 InvalidIdValue,
132 InvalidImportValue,
134 InvalidIncludedValue,
136 InvalidIndexValue,
138 InvalidNestValue,
140 InvalidPrefixValue,
142 InvalidPropagateValue,
144 InvalidProtectedValue,
146 InvalidReverseValue,
148 InvalidVersionValue,
150 InvalidBaseDirection,
152 InvalidBaseIri,
154 InvalidContainerMapping,
157 InvalidContextEntry,
159 InvalidContextNullification,
161 InvalidDefaultLanguage,
163 InvalidIriMapping,
165 InvalidJsonLiteral,
167 InvalidKeywordAlias,
169 InvalidLanguageMapValue,
172 InvalidLanguageMapping,
175 InvalidLanguageTaggedString,
177 InvalidLanguageTaggedValue,
179 InvalidLocalContext,
181 InvalidRemoteContext,
183 InvalidReverseProperty,
185 InvalidReversePropertyMap,
188 InvalidReversePropertyValue,
191 InvalidScopedContext,
193 InvalidSetOrListObject,
195 InvalidStreamingKeyOrder,
197 InvalidTermDefinition,
199 InvalidTypeMapping,
201 InvalidTypeValue,
204 InvalidTypedValue,
206 InvalidValueObject,
208 InvalidValueObjectValue,
211 InvalidVocabMapping,
213 IriConfusedWithPrefix,
216 KeywordRedefinition,
218 LoadingDocumentFailed,
220 LoadingRemoteContextFailed,
222 ProcessingModeConflict,
224 ProtectedTermRedefinition,
226}
227
228impl fmt::Display for JsonLdErrorCode {
229 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
230 f.write_str(match self {
231 Self::CollidingKeywords => "colliding keywords",
232 Self::ConflictingIndexes => "conflicting indexes",
233 Self::ContextOverflow => "context overflow",
234 Self::CyclicIriMapping => "cyclic IRI mapping",
235 Self::InvalidIdValue => "invalid @id value",
236 Self::InvalidImportValue => "invalid @import value",
237 Self::InvalidIncludedValue => "invalid @included value",
238 Self::InvalidIndexValue => "invalid @index value",
239 Self::InvalidNestValue => "invalid @nest value",
240 Self::InvalidPrefixValue => "invalid @prefix value",
241 Self::InvalidPropagateValue => "invalid @propagate value",
242 Self::InvalidProtectedValue => "invalid @protected value",
243 Self::InvalidReverseValue => "invalid @reverse value",
244 Self::InvalidVersionValue => "invalid @version value",
245 Self::InvalidBaseDirection => "invalid base direction",
246 Self::InvalidBaseIri => "invalid base IRI",
247 Self::InvalidContainerMapping => "invalid container mapping",
248 Self::InvalidContextEntry => "invalid context entry",
249 Self::InvalidContextNullification => "invalid context nullification",
250 Self::InvalidDefaultLanguage => "invalid default language",
251 Self::InvalidIriMapping => "invalid IRI mapping",
252 Self::InvalidJsonLiteral => "invalid JSON literal",
253 Self::InvalidKeywordAlias => "invalid keyword alias",
254 Self::InvalidLanguageMapValue => "invalid language map value",
255 Self::InvalidLanguageMapping => "invalid language mapping",
256 Self::InvalidLanguageTaggedString => "invalid language-tagged string",
257 Self::InvalidLanguageTaggedValue => "invalid language-tagged value",
258 Self::InvalidLocalContext => "invalid local context",
259 Self::InvalidRemoteContext => "invalid remote context",
260 Self::InvalidReverseProperty => "invalid reverse property",
261 Self::InvalidReversePropertyMap => "invalid reverse property map",
262 Self::InvalidReversePropertyValue => "invalid reverse property value",
263 Self::InvalidScopedContext => "invalid scoped context",
264 Self::InvalidSetOrListObject => "invalid set or list object",
265 Self::InvalidStreamingKeyOrder => "invalid streaming key order",
266 Self::InvalidTermDefinition => "invalid term definition",
267 Self::InvalidTypeMapping => "invalid type mapping",
268 Self::InvalidTypeValue => "invalid type value",
269 Self::InvalidTypedValue => "invalid typed value",
270 Self::InvalidValueObject => "invalid value object",
271 Self::InvalidValueObjectValue => "invalid value object value",
272 Self::InvalidVocabMapping => "invalid vocab mapping",
273 Self::IriConfusedWithPrefix => "IRI confused with prefix",
274 Self::KeywordRedefinition => "keyword redefinition",
275 Self::LoadingDocumentFailed => "loading document failed",
276 Self::LoadingRemoteContextFailed => "loading remote context failed",
277 Self::ProcessingModeConflict => "processing mode conflict",
278 Self::ProtectedTermRedefinition => "protected term redefinition",
279 })
280 }
281}