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
use derivative::Derivative;
use iref::IriRef;
use locspan::{Meta, StrippedPartialEq};

use super::{
	AnyDefinition, Context, ContextSubFragments, Definition, FragmentRef, Traverse, Value,
};

#[derive(Derivative)]
#[derivative(Clone(bound = ""))]
pub enum ArrayIter<'a, M, D> {
	Owned(core::slice::Iter<'a, Meta<Context<D>, M>>),
	Borrowed(core::slice::Iter<'a, Meta<ContextRef<'a, D>, M>>),
}

impl<'a, D, M: Clone> Iterator for ArrayIter<'a, M, D> {
	type Item = Meta<ContextRef<'a, D>, M>;

	fn size_hint(&self) -> (usize, Option<usize>) {
		match self {
			Self::Owned(m) => m.size_hint(),
			Self::Borrowed(m) => m.size_hint(),
		}
	}

	fn next(&mut self) -> Option<Self::Item> {
		match self {
			Self::Owned(m) => m
				.next()
				.map(|Meta(d, m)| Meta(d.as_context_ref(), m.clone())),
			Self::Borrowed(m) => m.next().cloned(),
		}
	}
}

impl<'a, D, M: Clone> ExactSizeIterator for ArrayIter<'a, M, D> {}

pub trait AnyValue<M>: Sized + StrippedPartialEq + Clone + Send + Sync {
	type Definition: AnyDefinition<M, ContextValue = Self> + Send + Sync;

	fn as_value_ref(&self) -> ValueRef<M, Self::Definition>;

	fn traverse(&self) -> Traverse<M, Self> {
		match self.as_value_ref() {
			ValueRef::One(Meta(c, _)) => Traverse::new(FragmentRef::Context(c)),
			ValueRef::Many(m) => Traverse::new(FragmentRef::ContextArray(m)),
		}
	}
}

pub trait AnyValueMut {
	fn append(&mut self, context: Self);
}

impl<M: Clone + Send + Sync> AnyValue<M> for Value<M> {
	type Definition = Definition<M, Self>;

	fn as_value_ref(&self) -> ValueRef<M, Self::Definition> {
		self.into()
	}
}

impl<M> AnyValueMut for Value<M> {
	fn append(&mut self, context: Self) {
		match self {
			Self::One(a) => {
				let a = unsafe { core::ptr::read(a) };

				let contexts = match context {
					Self::One(b) => vec![a, b],
					Self::Many(b) => {
						let mut contexts = vec![a];
						contexts.extend(b);
						contexts
					}
				};

				unsafe { core::ptr::write(self, Self::Many(contexts)) }
			}
			Self::Many(a) => match context {
				Self::One(b) => a.push(b),
				Self::Many(b) => a.extend(b),
			},
		}
	}
}

/// Reference to a context entry.
#[derive(Derivative)]
#[derivative(Clone(bound = "M: Clone"))]
pub enum ValueRef<'a, M, D = Definition<M>> {
	One(Meta<ContextRef<'a, D>, M>),
	Many(ArrayIter<'a, M, D>),
}

impl<'a, M, D> ValueRef<'a, M, D> {
	pub fn is_array(&self) -> bool {
		matches!(self, Self::Many(_))
	}

	pub fn is_object(&self) -> bool {
		match self {
			Self::One(c) => c.is_object(),
			_ => false,
		}
	}
}

impl<'a, M: Clone, D> IntoIterator for ValueRef<'a, M, D> {
	type Item = Meta<ContextRef<'a, D>, M>;
	type IntoIter = ContextEntryIter<'a, M, D>;

	fn into_iter(self) -> Self::IntoIter {
		match self {
			Self::One(i) => ContextEntryIter::One(Some(i)),
			Self::Many(m) => ContextEntryIter::Many(m),
		}
	}
}

impl<'a, M: Clone> From<&'a Value<M>> for ValueRef<'a, M, Definition<M>> {
	fn from(e: &'a Value<M>) -> Self {
		match e {
			Value::One(c) => Self::One(c.borrow_value().cast()),
			Value::Many(m) => Self::Many(ArrayIter::Owned(m.iter())),
		}
	}
}

// #[derive(Clone)]
// pub struct ManyContexts<'a, M>(std::slice::Iter<'a, Meta<Context<Definition<M>>, M>>);

// impl<'a, M: Clone> Iterator for ManyContexts<'a, M> {
// 	type Item = Meta<ContextRef<'a, Definition<M>>, M>;

// 	fn size_hint(&self) -> (usize, Option<usize>) {
// 		self.0.size_hint()
// 	}

// 	fn next(&mut self) -> Option<Self::Item> {
// 		self.0.next().map(|c| c.borrow_value().cast())
// 	}
// }

// impl<'a, M: Clone> ExactSizeIterator for ManyContexts<'a, M> {}

// impl<'a, M, D> From<std::slice::Iter<'a, Meta<Context<D>, M>>> for ManyContexts<'a, M> {
// 	fn from(i: std::slice::Iter<'a, Meta<Context<D>, M>>) -> Self {
// 		Self(i)
// 	}
// }

pub enum ContextEntryIter<'a, M, D = Definition<M>> {
	One(Option<Meta<ContextRef<'a, D>, M>>),
	Many(ArrayIter<'a, M, D>),
}

impl<'a, M: Clone, D> Iterator for ContextEntryIter<'a, M, D> {
	type Item = Meta<ContextRef<'a, D>, M>;

	fn next(&mut self) -> Option<Self::Item> {
		match self {
			Self::One(i) => i.take(),
			Self::Many(m) => m.next(),
		}
	}
}

/// Reference to context.
#[derive(Derivative)]
#[derivative(Clone(bound = ""), Copy(bound = ""))]
pub enum ContextRef<'a, D> {
	Null,
	IriRef(IriRef<'a>),
	Definition(&'a D),
}

impl<'a, D> ContextRef<'a, D> {
	pub fn is_object(&self) -> bool {
		matches!(self, Self::Definition(_))
	}

	pub fn sub_items<M>(&self) -> ContextSubFragments<'a, M, D>
	where
		D: AnyDefinition<M>,
	{
		match self {
			Self::Definition(d) => ContextSubFragments::Definition(Box::new(d.entries())),
			_ => ContextSubFragments::None,
		}
	}
}

impl<'a, D> From<&'a Context<D>> for ContextRef<'a, D> {
	fn from(c: &'a Context<D>) -> Self {
		match c {
			Context::Null => ContextRef::Null,
			Context::IriRef(i) => ContextRef::IriRef(i.as_iri_ref()),
			Context::Definition(d) => ContextRef::Definition(d),
		}
	}
}