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
use super::{Any, InvalidExpandedJson, MappedEq};
use crate::{Id, IndexedObject, Relabel, TryFromJson};
use contextual::WithContext;
use educe::Educe;
use json_ld_syntax::{IntoJson, IntoJsonWithContext};
use rdf_types::{Generator, Subject, Vocabulary, VocabularyMut};
use std::hash::Hash;

#[allow(clippy::derived_hash_with_manual_eq)]
#[derive(Educe, Debug, Clone, Hash)]
#[educe(
	PartialEq(bound = "T: Eq + Hash, B: Eq + Hash"),
	Eq(bound = "T: Eq + Hash, B: Eq + Hash")
)]
/// List object.
pub struct List<T, B> {
	entry: Vec<IndexedObject<T, B>>,
}

impl<T, B> List<T, B> {
	/// Creates a new list object.
	pub fn new(objects: Vec<IndexedObject<T, B>>) -> Self {
		Self { entry: objects }
	}

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

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

	/// Returns a reference to the "@list" entry of the list object.
	///
	/// Alias for `as_slice`.
	pub fn entry(&self) -> &[IndexedObject<T, B>] {
		&self.entry
	}

	pub fn entry_mut(&mut self) -> &mut Vec<IndexedObject<T, B>> {
		&mut self.entry
	}

	pub fn as_slice(&self) -> &[IndexedObject<T, B>] {
		self.entry.as_slice()
	}

	pub fn as_mut_slice(&mut self) -> &mut [IndexedObject<T, B>] {
		self.entry.as_mut_slice()
	}

	pub fn into_entry(self) -> Vec<IndexedObject<T, B>> {
		self.entry
	}

	pub fn push(&mut self, object: IndexedObject<T, B>) {
		self.entry.push(object)
	}

	pub fn pop(&mut self) -> Option<IndexedObject<T, B>> {
		self.entry.pop()
	}

	pub fn iter(&self) -> core::slice::Iter<IndexedObject<T, B>> {
		self.entry.iter()
	}

	pub fn iter_mut(&mut self) -> core::slice::IterMut<IndexedObject<T, B>> {
		self.entry.iter_mut()
	}

	/// Puts this list object literals into canonical form using the given
	/// `buffer`.
	///
	/// The buffer is used to compute the canonical form of numbers.
	pub fn canonicalize_with(&mut self, buffer: &mut ryu_js::Buffer) {
		for object in self {
			object.canonicalize_with(buffer)
		}
	}

	/// Puts this list object literals into canonical form.
	pub fn canonicalize(&mut self) {
		let mut buffer = ryu_js::Buffer::new();
		self.canonicalize_with(&mut buffer)
	}

	/// Map the identifiers present in this list (recursively).
	pub fn map_ids<U, C>(
		self,
		mut map_iri: impl FnMut(T) -> U,
		mut map_id: impl FnMut(Id<T, B>) -> Id<U, C>,
	) -> List<U, C>
	where
		U: Eq + Hash,
		C: Eq + Hash,
	{
		self.map_ids_with(&mut map_iri, &mut map_id)
	}

	pub(crate) fn map_ids_with<U, C>(
		self,
		map_iri: &mut impl FnMut(T) -> U,
		map_id: &mut impl FnMut(Id<T, B>) -> Id<U, C>,
	) -> List<U, C>
	where
		U: Eq + Hash,
		C: Eq + Hash,
	{
		List::new(
			self.entry
				.into_iter()
				.map(|indexed_object| {
					indexed_object.map_inner(|object| object.map_ids_with(map_iri, map_id))
				})
				.collect(),
		)
	}
}

impl<T, B> Relabel<T, B> for List<T, B> {
	fn relabel_with<N: Vocabulary<Iri = T, BlankId = B>, G: Generator<N>>(
		&mut self,
		vocabulary: &mut N,
		generator: &mut G,
		relabeling: &mut hashbrown::HashMap<B, Subject<T, B>>,
	) where
		T: Clone + Eq + Hash,
		B: Clone + Eq + Hash,
	{
		for object in self {
			object.relabel_with(vocabulary, generator, relabeling)
		}
	}
}

impl<T: Eq + Hash, B: Eq + Hash> List<T, B> {
	pub(crate) fn try_from_json_object_in(
		vocabulary: &mut impl VocabularyMut<Iri = T, BlankId = B>,
		object: json_syntax::Object,
		list_entry: json_syntax::object::Entry,
	) -> Result<Self, InvalidExpandedJson> {
		let list = Vec::try_from_json_in(vocabulary, list_entry.value)?;

		match object.into_iter().next() {
			Some(_) => Err(InvalidExpandedJson::UnexpectedEntry),
			None => Ok(Self::new(list)),
		}
	}
}

impl<T, B> Any<T, B> for List<T, B> {
	fn as_ref(&self) -> super::Ref<T, B> {
		super::Ref::List(self)
	}
}

impl<T: Eq + Hash, B: Eq + Hash> MappedEq for List<T, B> {
	type BlankId = B;

	fn mapped_eq<'a, 'b, F: Clone + Fn(&'a B) -> &'b B>(&'a self, other: &Self, f: F) -> bool
	where
		B: 'a + 'b,
	{
		self.entry.mapped_eq(&other.entry, f)
	}
}

impl<'a, T, B> IntoIterator for &'a List<T, B> {
	type Item = &'a IndexedObject<T, B>;
	type IntoIter = core::slice::Iter<'a, IndexedObject<T, B>>;

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

impl<'a, T, B> IntoIterator for &'a mut List<T, B> {
	type Item = &'a mut IndexedObject<T, B>;
	type IntoIter = core::slice::IterMut<'a, IndexedObject<T, B>>;

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

impl<T, B> IntoIterator for List<T, B> {
	type Item = IndexedObject<T, B>;
	type IntoIter = std::vec::IntoIter<IndexedObject<T, B>>;

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

/// List object fragment.
pub enum FragmentRef<'a, T, B> {
	/// "@list" entry.
	Entry(&'a [IndexedObject<T, B>]),

	/// "@list" entry key.
	Key,

	/// "@list" value.
	Value(&'a [IndexedObject<T, B>]),
}

impl<T, B, N: Vocabulary<Iri = T, BlankId = B>> IntoJsonWithContext<N> for List<T, B> {
	fn into_json_with(self, vocabulary: &N) -> json_syntax::Value {
		let mut obj = json_syntax::Object::new();

		obj.insert("@list".into(), self.entry.into_with(vocabulary).into_json());

		obj.into()
	}
}