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
pub use json_ld_syntax::ContainerKind;
use json_ld_syntax::{context::definition::TypeContainer, Nullable};

pub struct InvalidContainer;

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Container {
	// Empty container
	None,

	Graph,
	Id,
	Index,
	Language,
	List,
	Set,
	Type,

	GraphSet,
	GraphId,
	GraphIndex,
	IdSet,
	IndexSet,
	LanguageSet,
	SetType,

	GraphIdSet,
	GraphIndexSet,
}

impl Default for Container {
	fn default() -> Self {
		Self::new()
	}
}

impl Container {
	pub fn new() -> Container {
		Container::None
	}

	pub fn from_syntax(r: Nullable<&json_ld_syntax::Container>) -> Result<Self, InvalidContainer> {
		match r {
			Nullable::Null => Ok(Self::None),
			Nullable::Some(json_ld_syntax::Container::One(c)) => Ok((*c).into()),
			Nullable::Some(json_ld_syntax::Container::Many(m)) => {
				let mut container = Container::new();

				for t in m {
					if !container.add(*t) {
						return Err(InvalidContainer);
					}
				}

				Ok(container)
			}
		}
	}

	pub fn from<'a, I: IntoIterator<Item = &'a ContainerKind>>(
		iter: I,
	) -> Result<Container, ContainerKind> {
		let mut container = Container::new();
		for item in iter {
			if !container.add(*item) {
				return Err(*item);
			}
		}

		Ok(container)
	}

	pub fn as_slice(&self) -> &[ContainerKind] {
		use Container::*;
		match self {
			None => &[],
			Graph => &[ContainerKind::Graph],
			Id => &[ContainerKind::Id],
			Index => &[ContainerKind::Index],
			Language => &[ContainerKind::Language],
			List => &[ContainerKind::List],
			Set => &[ContainerKind::Set],
			Type => &[ContainerKind::Type],
			GraphSet => &[ContainerKind::Graph, ContainerKind::Set],
			GraphId => &[ContainerKind::Graph, ContainerKind::Id],
			GraphIndex => &[ContainerKind::Graph, ContainerKind::Index],
			IdSet => &[ContainerKind::Id, ContainerKind::Set],
			IndexSet => &[ContainerKind::Index, ContainerKind::Set],
			LanguageSet => &[ContainerKind::Language, ContainerKind::Set],
			SetType => &[ContainerKind::Type, ContainerKind::Set],
			GraphIdSet => &[ContainerKind::Graph, ContainerKind::Id, ContainerKind::Set],
			GraphIndexSet => &[
				ContainerKind::Graph,
				ContainerKind::Index,
				ContainerKind::Set,
			],
		}
	}

	pub fn iter(&self) -> impl Iterator<Item = &ContainerKind> {
		self.as_slice().iter()
	}

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

	pub fn is_empty(&self) -> bool {
		matches!(self, Container::None)
	}

	pub fn contains(&self, c: ContainerKind) -> bool {
		self.as_slice().contains(&c)
	}

	pub fn with(&self, c: ContainerKind) -> Option<Container> {
		let new_container = match (self, c) {
			(Container::None, c) => c.into(),
			(Container::Graph, ContainerKind::Graph) => *self,
			(Container::Graph, ContainerKind::Set) => Container::GraphSet,
			(Container::Graph, ContainerKind::Id) => Container::GraphId,
			(Container::Graph, ContainerKind::Index) => Container::GraphIndex,
			(Container::Id, ContainerKind::Id) => *self,
			(Container::Id, ContainerKind::Graph) => Container::GraphId,
			(Container::Id, ContainerKind::Set) => Container::IdSet,
			(Container::Index, ContainerKind::Index) => *self,
			(Container::Index, ContainerKind::Graph) => Container::GraphIndex,
			(Container::Index, ContainerKind::Set) => Container::IndexSet,
			(Container::Language, ContainerKind::Language) => *self,
			(Container::Language, ContainerKind::Set) => Container::LanguageSet,
			(Container::List, ContainerKind::List) => *self,
			(Container::Set, ContainerKind::Set) => *self,
			(Container::Set, ContainerKind::Graph) => Container::GraphSet,
			(Container::Set, ContainerKind::Id) => Container::IdSet,
			(Container::Set, ContainerKind::Index) => Container::IndexSet,
			(Container::Set, ContainerKind::Language) => Container::LanguageSet,
			(Container::Set, ContainerKind::Type) => Container::SetType,
			(Container::Type, ContainerKind::Type) => *self,
			(Container::Type, ContainerKind::Set) => Container::SetType,
			(Container::GraphSet, ContainerKind::Graph) => *self,
			(Container::GraphSet, ContainerKind::Set) => *self,
			(Container::GraphSet, ContainerKind::Id) => Container::GraphIdSet,
			(Container::GraphSet, ContainerKind::Index) => Container::GraphIdSet,
			(Container::GraphId, ContainerKind::Graph) => *self,
			(Container::GraphId, ContainerKind::Id) => *self,
			(Container::GraphId, ContainerKind::Set) => Container::GraphIdSet,
			(Container::GraphIndex, ContainerKind::Graph) => *self,
			(Container::GraphIndex, ContainerKind::Index) => *self,
			(Container::GraphIndex, ContainerKind::Set) => Container::GraphIndexSet,
			(Container::IdSet, ContainerKind::Id) => *self,
			(Container::IdSet, ContainerKind::Set) => *self,
			(Container::IdSet, ContainerKind::Graph) => Container::GraphIdSet,
			(Container::IndexSet, ContainerKind::Index) => *self,
			(Container::IndexSet, ContainerKind::Set) => *self,
			(Container::IndexSet, ContainerKind::Graph) => Container::GraphIndexSet,
			(Container::LanguageSet, ContainerKind::Language) => *self,
			(Container::LanguageSet, ContainerKind::Set) => *self,
			(Container::SetType, ContainerKind::Set) => *self,
			(Container::SetType, ContainerKind::Type) => *self,
			(Container::GraphIdSet, ContainerKind::Graph) => *self,
			(Container::GraphIdSet, ContainerKind::Id) => *self,
			(Container::GraphIdSet, ContainerKind::Set) => *self,
			(Container::GraphIndexSet, ContainerKind::Graph) => *self,
			(Container::GraphIndexSet, ContainerKind::Index) => *self,
			(Container::GraphIndexSet, ContainerKind::Set) => *self,
			_ => return None,
		};

		Some(new_container)
	}

	pub fn add(&mut self, c: ContainerKind) -> bool {
		match self.with(c) {
			Some(container) => {
				*self = container;
				true
			}
			None => false,
		}
	}

	pub fn into_syntax(self) -> Option<json_ld_syntax::Container> {
		let slice = self.as_slice();

		match slice.len() {
			0 => None,
			1 => Some(json_ld_syntax::Container::One(slice[0])),
			_ => Some(json_ld_syntax::Container::Many(slice.to_vec())),
		}
	}
}

impl From<ContainerKind> for Container {
	fn from(c: ContainerKind) -> Self {
		match c {
			ContainerKind::Graph => Self::Graph,
			ContainerKind::Id => Self::Id,
			ContainerKind::Index => Self::Index,
			ContainerKind::Language => Self::Language,
			ContainerKind::List => Self::List,
			ContainerKind::Set => Self::Set,
			ContainerKind::Type => Self::Type,
		}
	}
}

impl From<TypeContainer> for Container {
	fn from(c: TypeContainer) -> Self {
		match c {
			TypeContainer::Set => Container::Set,
		}
	}
}