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
use std::{borrow::Cow, ops::Add};

use smallvec::{smallvec, SmallVec};

use super::{serde::SerializedData, Error, Global};

#[derive(Debug)]
pub(crate) struct Aggregate(pub(crate) SmallVec<[Error; 2]>);

impl<LS, RS> Add<Error<RS>> for Error<LS> {
	type Output = Self;
	fn add(self, rhs: Error<RS>) -> Self::Output {
		let lhs = match self.back_cast::<Aggregate>() {
			Ok(mut agg) => {
				agg.0.push(rhs.into());
				return Error::<Global>::from(agg).into();
			}
			Err(lhs) => lhs,
		};

		let lhs = if lhs.name() == "Aggregate" {
			match lhs.back_cast::<SerializedData>() {
				Ok(mut agg) => {
					match &mut agg.inner {
						Some(inner) => inner.push(Cow::Owned(rhs.into())),
						None => agg.inner = Some(smallvec![Cow::Owned(rhs.into())]),
					}
					return Error::<Global>::from(agg).into();
				}
				Err(lhs) => lhs,
			}
		} else {
			lhs
		};

		Error::<Global>::from(Aggregate(smallvec![lhs.into(), rhs.into()])).into()
	}
}