1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use json_ld_syntax::ErrorCode;
use locspan::Meta;
use std::fmt;

#[derive(Debug)]
pub enum Error<M, E> {
	ContextSyntax(json_ld_syntax::context::InvalidContext),
	ContextProcessing(json_ld_context_processing::Error<E>),
	InvalidIndexValue,
	InvalidSetOrListObject,
	InvalidReversePropertyMap,
	InvalidTypeValue,
	KeyExpansionFailed,
	InvalidReversePropertyValue,
	InvalidLanguageMapValue,
	CollidingKeywords,
	InvalidIdValue,
	InvalidIncludedValue,
	InvalidReverseValue,
	InvalidNestValue,
	DuplicateKey(Meta<json_syntax::object::Key, M>),
	Literal(crate::LiteralExpansionError),
	Value(crate::InvalidValue),
}

impl<M, E> Error<M, E> {
	pub fn code(&self) -> ErrorCode {
		match self {
			Self::ContextSyntax(e) => e.code(),
			Self::ContextProcessing(e) => e.code(),
			Self::InvalidIndexValue => ErrorCode::InvalidIndexValue,
			Self::InvalidSetOrListObject => ErrorCode::InvalidSetOrListObject,
			Self::InvalidReversePropertyMap => ErrorCode::InvalidReversePropertyMap,
			Self::InvalidTypeValue => ErrorCode::InvalidTypeValue,
			Self::KeyExpansionFailed => ErrorCode::KeyExpansionFailed,
			Self::InvalidReversePropertyValue => ErrorCode::InvalidReversePropertyValue,
			Self::InvalidLanguageMapValue => ErrorCode::InvalidLanguageMapValue,
			Self::CollidingKeywords => ErrorCode::CollidingKeywords,
			Self::InvalidIdValue => ErrorCode::InvalidIdValue,
			Self::InvalidIncludedValue => ErrorCode::InvalidIncludedValue,
			Self::InvalidReverseValue => ErrorCode::InvalidReverseValue,
			Self::InvalidNestValue => ErrorCode::InvalidNestValue,
			Self::DuplicateKey(_) => ErrorCode::DuplicateKey,
			Self::Literal(e) => e.code(),
			Self::Value(e) => e.code(),
		}
	}
}

impl<M: Clone, E> Error<M, E> {
	pub fn duplicate_key_ref(
		json_syntax::object::Duplicate(a, b): json_syntax::object::Duplicate<
			&json_syntax::object::Entry<M>,
		>,
	) -> Meta<Self, M> {
		Meta(Self::DuplicateKey(a.key.clone()), b.key.metadata().clone())
	}
}

impl<M, E> From<json_ld_context_processing::Error<E>> for Error<M, E> {
	fn from(e: json_ld_context_processing::Error<E>) -> Self {
		Self::ContextProcessing(e)
	}
}

impl<M, E> From<crate::LiteralExpansionError> for Error<M, E> {
	fn from(e: crate::LiteralExpansionError) -> Self {
		Self::Literal(e)
	}
}

impl<M, E> From<crate::InvalidValue> for Error<M, E> {
	fn from(e: crate::InvalidValue) -> Self {
		Self::Value(e)
	}
}

impl<M, E: fmt::Display> fmt::Display for Error<M, E> {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::ContextSyntax(e) => e.fmt(f),
			Self::ContextProcessing(e) => write!(f, "context processing error: {}", e),
			Self::InvalidIndexValue => write!(f, "invalid index value"),
			Self::InvalidSetOrListObject => write!(f, "invalid set or list object"),
			Self::InvalidReversePropertyMap => write!(f, "invalid reverse property map"),
			Self::InvalidTypeValue => write!(f, "invalid type value"),
			Self::KeyExpansionFailed => write!(f, "key expansion failed"),
			Self::InvalidReversePropertyValue => write!(f, "invalid reverse property value"),
			Self::InvalidLanguageMapValue => write!(f, "invalid language map value"),
			Self::CollidingKeywords => write!(f, "colliding keywords"),
			Self::InvalidIdValue => write!(f, "invalid id value"),
			Self::InvalidIncludedValue => write!(f, "invalid included value"),
			Self::InvalidReverseValue => write!(f, "invalid reverse value"),
			Self::InvalidNestValue => write!(f, "invalid nest value"),
			Self::DuplicateKey(Meta(key, _)) => write!(f, "duplicate key `{}`", key),
			Self::Literal(e) => e.fmt(f),
			Self::Value(e) => e.fmt(f),
		}
	}
}