Skip to main content

json_ld/core/object/
list.rs

1use std::hash::Hash;
2
3use crate::{
4	JsonLdIndexedObject, JsonLdVisit,
5	object::{JsonLdObjectMut, JsonLdObjectRef},
6};
7
8use super::AnyJsonLdObject;
9
10#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12/// List object.
13pub struct JsonLdList {
14	#[cfg_attr(feature = "serde", serde(rename = "@list"))]
15	entry: Vec<JsonLdIndexedObject>,
16}
17
18impl JsonLdList {
19	/// Creates a new list object.
20	pub fn new(objects: Vec<JsonLdIndexedObject>) -> Self {
21		Self { entry: objects }
22	}
23
24	pub fn len(&self) -> usize {
25		self.entry.len()
26	}
27
28	pub fn is_empty(&self) -> bool {
29		self.entry.is_empty()
30	}
31
32	/// Returns a reference to the "@list" entry of the list object.
33	///
34	/// Alias for `as_slice`.
35	pub fn entry(&self) -> &[JsonLdIndexedObject] {
36		&self.entry
37	}
38
39	pub fn entry_mut(&mut self) -> &mut Vec<JsonLdIndexedObject> {
40		&mut self.entry
41	}
42
43	pub fn as_slice(&self) -> &[JsonLdIndexedObject] {
44		self.entry.as_slice()
45	}
46
47	pub fn as_mut_slice(&mut self) -> &mut [JsonLdIndexedObject] {
48		self.entry.as_mut_slice()
49	}
50
51	pub fn into_entry(self) -> Vec<JsonLdIndexedObject> {
52		self.entry
53	}
54
55	pub fn push(&mut self, object: JsonLdIndexedObject) {
56		self.entry.push(object)
57	}
58
59	pub fn pop(&mut self) -> Option<JsonLdIndexedObject> {
60		self.entry.pop()
61	}
62
63	pub fn iter(&self) -> core::slice::Iter<'_, JsonLdIndexedObject> {
64		self.entry.iter()
65	}
66
67	pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, JsonLdIndexedObject> {
68		self.entry.iter_mut()
69	}
70}
71
72impl JsonLdVisit for JsonLdList {
73	fn visit_with(&self, f: &mut impl FnMut(JsonLdObjectRef)) {
74		f(JsonLdObjectRef::List(self));
75		for t in self {
76			t.visit_with(f);
77		}
78	}
79
80	fn visit_mut_with(&mut self, f: &mut impl FnMut(JsonLdObjectMut)) {
81		f(JsonLdObjectMut::List(self));
82		for t in &mut self.entry {
83			t.visit_mut_with(f);
84		}
85	}
86}
87
88impl AnyJsonLdObject for JsonLdList {
89	fn as_ref(&self) -> super::JsonLdObjectRef<'_> {
90		super::JsonLdObjectRef::List(self)
91	}
92}
93
94impl<'a> IntoIterator for &'a JsonLdList {
95	type Item = &'a JsonLdIndexedObject;
96	type IntoIter = core::slice::Iter<'a, JsonLdIndexedObject>;
97
98	fn into_iter(self) -> Self::IntoIter {
99		self.iter()
100	}
101}
102
103impl<'a> IntoIterator for &'a mut JsonLdList {
104	type Item = &'a mut JsonLdIndexedObject;
105	type IntoIter = core::slice::IterMut<'a, JsonLdIndexedObject>;
106
107	fn into_iter(self) -> Self::IntoIter {
108		self.iter_mut()
109	}
110}
111
112impl IntoIterator for JsonLdList {
113	type Item = JsonLdIndexedObject;
114	type IntoIter = std::vec::IntoIter<JsonLdIndexedObject>;
115
116	fn into_iter(self) -> Self::IntoIter {
117		self.entry.into_iter()
118	}
119}