json_ld_expansion/
error.rs

1use json_ld_context_processing::algorithm::RejectVocab;
2use json_ld_syntax::ErrorCode;
3
4#[derive(Debug, thiserror::Error)]
5pub enum Error {
6	#[error("Invalid context: {0}")]
7	ContextSyntax(#[from] json_ld_syntax::context::InvalidContext),
8
9	#[error("Context processing failed: {0}")]
10	ContextProcessing(json_ld_context_processing::Error),
11
12	#[error("Invalid `@index` value")]
13	InvalidIndexValue,
14
15	#[error("Invalid set or list object")]
16	InvalidSetOrListObject,
17
18	#[error("Invalid `@reverse` property map")]
19	InvalidReversePropertyMap,
20
21	#[error("Invalid `@type` value")]
22	InvalidTypeValue,
23
24	#[error("Key `{0}` expansion failed")]
25	KeyExpansionFailed(String),
26
27	#[error("Invalid `@reverse` property value")]
28	InvalidReversePropertyValue,
29
30	#[error("Invalid `@language` map value")]
31	InvalidLanguageMapValue,
32
33	#[error("Colliding keywords")]
34	CollidingKeywords,
35
36	#[error("Invalid `@id` value")]
37	InvalidIdValue,
38
39	#[error("Invalid `@included` value")]
40	InvalidIncludedValue,
41
42	#[error("Invalid `@reverse` value")]
43	InvalidReverseValue,
44
45	#[error("Invalid `@nest` value")]
46	InvalidNestValue,
47
48	#[error("Duplicate key `{0}`")]
49	DuplicateKey(json_syntax::object::Key),
50
51	#[error(transparent)]
52	Literal(crate::LiteralExpansionError),
53
54	#[error(transparent)]
55	Value(crate::InvalidValue),
56
57	#[error("Forbidden use of `@vocab`")]
58	ForbiddenVocab,
59}
60
61impl From<RejectVocab> for Error {
62	fn from(_value: RejectVocab) -> Self {
63		Self::ForbiddenVocab
64	}
65}
66
67impl Error {
68	pub fn code(&self) -> ErrorCode {
69		match self {
70			Self::ContextSyntax(e) => e.code(),
71			Self::ContextProcessing(e) => e.code(),
72			Self::InvalidIndexValue => ErrorCode::InvalidIndexValue,
73			Self::InvalidSetOrListObject => ErrorCode::InvalidSetOrListObject,
74			Self::InvalidReversePropertyMap => ErrorCode::InvalidReversePropertyMap,
75			Self::InvalidTypeValue => ErrorCode::InvalidTypeValue,
76			Self::KeyExpansionFailed(_) => ErrorCode::KeyExpansionFailed,
77			Self::InvalidReversePropertyValue => ErrorCode::InvalidReversePropertyValue,
78			Self::InvalidLanguageMapValue => ErrorCode::InvalidLanguageMapValue,
79			Self::CollidingKeywords => ErrorCode::CollidingKeywords,
80			Self::InvalidIdValue => ErrorCode::InvalidIdValue,
81			Self::InvalidIncludedValue => ErrorCode::InvalidIncludedValue,
82			Self::InvalidReverseValue => ErrorCode::InvalidReverseValue,
83			Self::InvalidNestValue => ErrorCode::InvalidNestValue,
84			Self::DuplicateKey(_) => ErrorCode::DuplicateKey,
85			Self::Literal(e) => e.code(),
86			Self::Value(e) => e.code(),
87			Self::ForbiddenVocab => ErrorCode::InvalidVocabMapping,
88		}
89	}
90}
91
92impl Error {
93	pub fn duplicate_key_ref(
94		json_syntax::object::Duplicate(a, _b): json_syntax::object::Duplicate<
95			&json_syntax::object::Entry,
96		>,
97	) -> Self {
98		Self::DuplicateKey(a.key.clone())
99	}
100}
101
102impl From<json_ld_context_processing::Error> for Error {
103	fn from(e: json_ld_context_processing::Error) -> Self {
104		Self::ContextProcessing(e)
105	}
106}
107
108impl From<crate::LiteralExpansionError> for Error {
109	fn from(e: crate::LiteralExpansionError) -> Self {
110		Self::Literal(e)
111	}
112}
113
114impl From<crate::InvalidValue> for Error {
115	fn from(e: crate::InvalidValue) -> Self {
116		Self::Value(e)
117	}
118}