jsonld_expansion/
error.rs1use jsonld_context_processing::algorithm::RejectVocab;
2use jsonld_syntax::ErrorCode;
3
4#[derive(Debug, thiserror::Error)]
5pub enum Error<E = std::convert::Infallible> {
7 #[error("Invalid context: {0}")]
8 ContextSyntax(#[from] jsonld_syntax::context::InvalidContext),
10
11 #[error("Context processing failed: {0}")]
12 ContextProcessing(jsonld_context_processing::Error<E>),
15
16 #[error("Invalid `@index` value")]
17 InvalidIndexValue,
19
20 #[error("Invalid set or list object")]
21 InvalidSetOrListObject,
24
25 #[error("Invalid `@reverse` property map")]
26 InvalidReversePropertyMap,
29
30 #[error("Invalid `@type` value")]
31 InvalidTypeValue,
34
35 #[error("Key `{0}` expansion failed")]
36 KeyExpansionFailed(String),
41
42 #[error("Invalid `@reverse` property value")]
43 InvalidReversePropertyValue,
46
47 #[error("Invalid `@language` map value")]
48 InvalidLanguageMapValue,
51
52 #[error("Colliding keywords")]
53 CollidingKeywords,
58
59 #[error("Invalid `@id` value")]
60 InvalidIdValue,
62
63 #[error("Invalid `@included` value")]
64 InvalidIncludedValue,
66
67 #[error("Invalid `@reverse` value")]
68 InvalidReverseValue,
70
71 #[error("Invalid `@nest` value")]
72 InvalidNestValue,
75
76 #[error("Duplicate key `{0}`")]
77 DuplicateKey(jstrict::object::Key),
79
80 #[error(transparent)]
81 Literal(crate::LiteralExpansionError),
84
85 #[error(transparent)]
86 Value(crate::InvalidValue),
89
90 #[error("Forbidden use of `@vocab`")]
91 ForbiddenVocab,
94
95 #[error("List of lists")]
96 ListOfLists,
100}
101
102impl<E> From<RejectVocab> for Error<E> {
103 fn from(_value: RejectVocab) -> Self {
104 Self::ForbiddenVocab
105 }
106}
107
108impl<E> Error<E> {
109 pub fn code(&self) -> ErrorCode {
114 match self {
115 Self::ContextSyntax(e) => e.code(),
116 Self::ContextProcessing(e) => e.code(),
117 Self::InvalidIndexValue => ErrorCode::InvalidIndexValue,
118 Self::InvalidSetOrListObject => ErrorCode::InvalidSetOrListObject,
119 Self::InvalidReversePropertyMap => ErrorCode::InvalidReversePropertyMap,
120 Self::InvalidTypeValue => ErrorCode::InvalidTypeValue,
121 Self::KeyExpansionFailed(_) => ErrorCode::KeyExpansionFailed,
122 Self::InvalidReversePropertyValue => ErrorCode::InvalidReversePropertyValue,
123 Self::InvalidLanguageMapValue => ErrorCode::InvalidLanguageMapValue,
124 Self::CollidingKeywords => ErrorCode::CollidingKeywords,
125 Self::InvalidIdValue => ErrorCode::InvalidIdValue,
126 Self::InvalidIncludedValue => ErrorCode::InvalidIncludedValue,
127 Self::InvalidReverseValue => ErrorCode::InvalidReverseValue,
128 Self::InvalidNestValue => ErrorCode::InvalidNestValue,
129 Self::DuplicateKey(_) => ErrorCode::DuplicateKey,
130 Self::Literal(e) => e.code(),
131 Self::Value(e) => e.code(),
132 Self::ForbiddenVocab => ErrorCode::InvalidVocabMapping,
133 Self::ListOfLists => ErrorCode::ListOfLists,
134 }
135 }
136}
137
138impl<E> Error<E> {
139 #[must_use]
142 pub fn duplicate_key_ref(jstrict::object::Duplicate(a, _b): jstrict::object::Duplicate<&jstrict::object::Entry>) -> Self {
143 Self::DuplicateKey(a.key.clone())
144 }
145}
146
147impl<E> From<jsonld_context_processing::Error<E>> for Error<E> {
148 fn from(e: jsonld_context_processing::Error<E>) -> Self {
149 Self::ContextProcessing(e)
150 }
151}
152
153impl<E> From<crate::LiteralExpansionError> for Error<E> {
154 fn from(e: crate::LiteralExpansionError) -> Self {
155 Self::Literal(e)
156 }
157}
158
159impl<E> From<crate::InvalidValue> for Error<E> {
160 fn from(e: crate::InvalidValue) -> Self {
161 Self::Value(e)
162 }
163}
164
165impl<E> From<crate::literal::NotALiteral> for Error<E> {
166 fn from(e: crate::literal::NotALiteral) -> Self {
167 Self::Literal(crate::LiteralExpansionError::NotALiteral(e))
168 }
169}