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
use super::{expand_element, Expanded, Options};
use crate::{
	context::{Loader, TermDefinition},
	object::*,
	syntax::ContainerType,
	ContextMut, Error, Id,
};
use iref::Iri;
use json::JsonValue;

pub async fn expand_array<
	T: Send + Sync + Id,
	C: Send + Sync + ContextMut<T>,
	L: Send + Sync + Loader,
>(
	active_context: &C,
	active_property: Option<&str>,
	active_property_definition: Option<&TermDefinition<T, C>>,
	element: &[JsonValue],
	base_url: Option<Iri<'_>>,
	loader: &mut L,
	options: Options,
	from_map: bool,
) -> Result<Expanded<T>, Error>
where
	C::LocalContext: Send + Sync + From<L::Output> + From<JsonValue>,
	L::Output: Into<JsonValue>,
{
	// Initialize an empty array, result.
	let mut is_list = false;
	let mut result = Vec::new();

	// If the container mapping of `active_property` includes `@list`, and
	// `expanded_item` is an array, set `expanded_item` to a new map containing
	// the entry `@list` where the value is the original `expanded_item`.
	if let Some(definition) = active_property_definition {
		is_list = definition.container.contains(ContainerType::List);
	}

	// For each item in element:
	for item in element {
		// Initialize `expanded_item` to the result of using this algorithm
		// recursively, passing `active_context`, `active_property`, `item` as element,
		// `base_url`, the `frame_expansion`, `ordered`, and `from_map` flags.
		result.extend(
			expand_element(
				active_context,
				active_property,
				item,
				base_url,
				loader,
				options,
				from_map,
			)
			.await?,
		);
	}

	if is_list {
		return Ok(Expanded::Object(Object::List(result).into()));
	}

	// Return result.
	Ok(Expanded::Array(result))
}