use super::*;
#[derive(Educe)]
#[educe(Clone, Copy, Default)]
pub struct MultiplicitySelector<Field>(PhantomData<fn(Field)>);
#[derive(Educe)]
#[educe(Clone, Copy, Default)]
pub struct DeterminedMultiplicitySelector<Field>(PhantomData<fn(Field)>);
#[derive(Educe)]
#[educe(Clone, Copy, Default)]
pub struct SingletonMultiplicitySelector<Field>(PhantomData<fn(Field)>);
pub trait MultiplicityMethods<'f>: Copy + Sized {
type Each: Sized + 'f;
type Field: Sized;
fn selector(self) -> Self {
self
}
fn iter_ordered(self, f: &'f Self::Field) -> impl Iterator<Item = &'f Self::Each> + 'f;
fn check_netdoc_encodable(self)
where
Self::Each: NetdocEncodable,
{
}
fn check_item_value_encodable(self)
where
Self::Each: ItemValueEncodable,
{
}
fn check_item_argument_encodable(self)
where
Self::Each: ItemArgument,
{
}
fn check_item_object_encodable(self)
where
Self::Each: ItemObjectEncodable,
{
}
}
impl<T> MultiplicitySelector<Vec<T>> {
pub fn selector(self) -> DeterminedMultiplicitySelector<Vec<T>> {
DeterminedMultiplicitySelector::default()
}
}
impl<'f, T: EncodeOrd + 'f> MultiplicityMethods<'f> for DeterminedMultiplicitySelector<Vec<T>> {
type Each = T;
type Field = Vec<T>;
fn iter_ordered(self, f: &'f Self::Field) -> impl Iterator<Item = &'f Self::Each> {
let mut v = f.iter().collect_vec();
v.sort_by(|a, b| a.encode_cmp(*b));
v.into_iter()
}
}
impl<'f, T: 'f> MultiplicityMethods<'f> for MultiplicitySelector<BTreeSet<T>> {
type Each = T;
type Field = BTreeSet<T>;
fn iter_ordered(self, f: &'f Self::Field) -> impl Iterator<Item = &'f Self::Each> {
f.iter()
}
}
impl<'f, T: 'f> MultiplicityMethods<'f> for MultiplicitySelector<Option<T>> {
type Each = T;
type Field = Option<T>;
fn iter_ordered(self, f: &'f Self::Field) -> impl Iterator<Item = &'f Self::Each> + 'f {
f.iter()
}
}
impl<'f, T: 'f> MultiplicityMethods<'f> for &'_ MultiplicitySelector<T> {
type Each = T;
type Field = T;
fn iter_ordered(self, f: &'f Self::Field) -> impl Iterator<Item = &'f Self::Each> + 'f {
iter::once(f)
}
}
impl<'f, T: 'f> MultiplicityMethods<'f> for SingletonMultiplicitySelector<T> {
type Each = T;
type Field = T;
fn iter_ordered(self, f: &'f Self::Field) -> impl Iterator<Item = &'f Self::Each> + 'f {
iter::once(f)
}
}
pub trait OptionalityMethods: Copy + Sized {
type Each: Sized + 'static;
type Field: Sized;
fn as_option<'f>(self, f: &'f Self::Field) -> Option<&'f Self::Each>;
}
impl<T: 'static> OptionalityMethods for MultiplicitySelector<Option<T>> {
type Each = T;
type Field = Option<T>;
fn as_option<'f>(self, f: &'f Self::Field) -> Option<&'f Self::Each> {
f.as_ref()
}
}
impl<T: 'static> OptionalityMethods for &'_ MultiplicitySelector<T> {
type Each = T;
type Field = T;
fn as_option<'f>(self, f: &'f Self::Field) -> Option<&'f Self::Each> {
Some(f)
}
}