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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use super::{term_definition, AnyValue, Entry, TermDefinition};
use crate::{Direction, LenientLanguageTagBuf, Nullable};
use derivative::Derivative;
use indexmap::IndexMap;
use iref::IriRefBuf;
use locspan::Meta;
use locspan_derive::StrippedPartialEq;

mod import;
mod key;
mod reference;
mod type_;
mod version;
mod vocab;

pub use import::*;
pub use key::*;
pub use reference::*;
pub use type_::*;
pub use version::*;
pub use vocab::*;

/// Context definition.
#[derive(PartialEq, StrippedPartialEq, Eq, Clone, Derivative, Debug)]
#[locspan(ignore(M))]
#[derivative(Default(bound = ""))]
pub struct Definition<M, C = super::Value<M>> {
	#[locspan(unwrap_deref2_stripped)]
	pub base: Option<Entry<Nullable<IriRefBuf>, M>>,
	#[locspan(unwrap_deref2_stripped)]
	pub import: Option<Entry<IriRefBuf, M>>,
	pub language: Option<Entry<Nullable<LenientLanguageTagBuf>, M>>,
	pub direction: Option<Entry<Nullable<Direction>, M>>,
	pub propagate: Option<Entry<bool, M>>,
	pub protected: Option<Entry<bool, M>>,
	pub type_: Option<Entry<Type<M>, M>>,
	pub version: Option<Entry<Version, M>>,
	pub vocab: Option<Entry<Nullable<Vocab>, M>>,
	pub bindings: Bindings<M, C>,
}

impl<M, C> Definition<M, C> {
	pub fn new() -> Self {
		Self::default()
	}
}

/// Context bindings.
#[derive(PartialEq, Eq, Clone, Derivative, Debug)]
#[derivative(Default(bound = ""))]
pub struct Bindings<M, C = super::Value<M>>(IndexMap<Key, TermBinding<M, C>>);

impl<M, C> Bindings<M, C> {
	pub fn new() -> Self {
		Self::default()
	}

	pub fn len(&self) -> usize {
		self.0.len()
	}

	pub fn is_empty(&self) -> bool {
		self.0.is_empty()
	}

	pub fn get(&self, key: &Key) -> Option<&TermBinding<M, C>> {
		self.0.get(key)
	}

	pub fn iter(&self) -> indexmap::map::Iter<Key, TermBinding<M, C>> {
		self.0.iter()
	}

	pub fn insert(
		&mut self,
		Meta(key, key_metadata): Meta<Key, M>,
		def: Meta<Nullable<TermDefinition<M, C>>, M>,
	) -> Option<TermBinding<M, C>> {
		self.0.insert(key, TermBinding::new(key_metadata, def))
	}
}

impl<M, C> IntoIterator for Bindings<M, C> {
	type Item = (Key, TermBinding<M, C>);
	type IntoIter = indexmap::map::IntoIter<Key, TermBinding<M, C>>;

	fn into_iter(self) -> Self::IntoIter {
		self.0.into_iter()
	}
}

impl<M, C> FromIterator<(Key, TermBinding<M, C>)> for Bindings<M, C> {
	fn from_iter<T: IntoIterator<Item = (Key, TermBinding<M, C>)>>(iter: T) -> Self {
		let mut result = Self::new();

		for (key, binding) in iter {
			result.0.insert(key, binding);
		}

		result
	}
}

impl<M, C> FromIterator<(Meta<Key, M>, Meta<Nullable<TermDefinition<M, C>>, M>)>
	for Bindings<M, C>
{
	fn from_iter<
		T: IntoIterator<Item = (Meta<Key, M>, Meta<Nullable<TermDefinition<M, C>>, M>)>,
	>(
		iter: T,
	) -> Self {
		let mut result = Self::new();

		for (key, definition) in iter {
			result.insert(key, definition);
		}

		result
	}
}

impl<M, C: locspan::StrippedPartialEq<D>, N, D> locspan::StrippedPartialEq<Bindings<N, D>>
	for Bindings<M, C>
{
	fn stripped_eq(&self, other: &Bindings<N, D>) -> bool {
		self.len() == other.len()
			&& self
				.iter()
				.all(|(key, a)| other.get(key).map(|b| a.stripped_eq(b)).unwrap_or(false))
	}
}

/// Term binding.
#[derive(PartialEq, StrippedPartialEq, Eq, Clone, Debug)]
#[locspan(ignore(M))]
pub struct TermBinding<M, C = super::Value<M>> {
	#[locspan(ignore)]
	pub key_metadata: M,
	pub definition: Meta<Nullable<TermDefinition<M, C>>, M>,
}

impl<M, C> TermBinding<M, C> {
	pub fn new(key_metadata: M, definition: Meta<Nullable<TermDefinition<M, C>>, M>) -> Self {
		Self {
			key_metadata,
			definition,
		}
	}
}

/// Context definition fragment.
pub enum FragmentRef<'a, M, C> {
	/// Context definition entry.
	Entry(EntryRef<'a, M, C>),

	/// Context definition entry key.
	Key(EntryKeyRef<'a>),

	/// Context definition entry value.
	Value(EntryValueRef<'a, M, C>),

	/// Term definition fragment.
	TermDefinitionFragment(term_definition::FragmentRef<'a, M, C>),
}

impl<'a, M, C> FragmentRef<'a, M, C> {
	pub fn is_key(&self) -> bool {
		match self {
			Self::Key(_) => true,
			Self::TermDefinitionFragment(f) => f.is_key(),
			_ => false,
		}
	}

	pub fn is_entry(&self) -> bool {
		match self {
			Self::Entry(_) => true,
			Self::TermDefinitionFragment(f) => f.is_entry(),
			_ => false,
		}
	}

	pub fn is_array(&self) -> bool {
		match self {
			Self::TermDefinitionFragment(i) => i.is_array(),
			_ => false,
		}
	}

	pub fn is_object(&self) -> bool
	where
		M: Clone,
		C: AnyValue<M>,
	{
		match self {
			Self::Value(v) => v.is_object(),
			Self::TermDefinitionFragment(v) => v.is_object(),
			_ => false,
		}
	}

	pub fn sub_items(&self) -> SubItems<'a, M, C>
	where
		M: Clone,
	{
		match self {
			Self::Entry(e) => SubItems::Entry(Some(e.key()), Some(Box::new(e.value()))),
			Self::Key(_) => SubItems::None,
			Self::Value(v) => SubItems::Value(v.sub_items()),
			Self::TermDefinitionFragment(f) => SubItems::TermDefinitionFragment(f.sub_fragments()),
		}
	}
}

pub enum EntryValueSubItems<'a, M, C> {
	None,
	TermDefinitionFragment(Box<term_definition::Entries<'a, M, C>>),
}

impl<'a, M, C> Iterator for EntryValueSubItems<'a, M, C> {
	type Item = FragmentRef<'a, M, C>;

	fn next(&mut self) -> Option<Self::Item> {
		match self {
			Self::None => None,
			Self::TermDefinitionFragment(d) => d.next().map(|e| {
				FragmentRef::TermDefinitionFragment(term_definition::FragmentRef::Entry(e))
			}),
		}
	}
}

pub enum SubItems<'a, M, C> {
	None,
	Entry(
		Option<EntryKeyRef<'a>>,
		Option<Box<EntryValueRef<'a, M, C>>>,
	),
	Value(EntryValueSubItems<'a, M, C>),
	TermDefinitionFragment(term_definition::SubFragments<'a, M, C>),
}

impl<'a, M, C> Iterator for SubItems<'a, M, C> {
	type Item = FragmentRef<'a, M, C>;

	fn next(&mut self) -> Option<Self::Item> {
		match self {
			Self::None => None,
			Self::Entry(k, v) => k
				.take()
				.map(FragmentRef::Key)
				.or_else(|| v.take().map(|v| FragmentRef::Value(*v))),
			Self::Value(d) => d.next(),
			Self::TermDefinitionFragment(d) => d.next().map(FragmentRef::TermDefinitionFragment),
		}
	}
}