microrm 0.6.3

Lightweight ORM using sqlite as a backend
Documentation
use super::{
    BorrowedDatum, BorrowedDatumList, DatumVisitor, OwnedDatum, OwnedDatumList, StringQuery,
};
use crate::DBResult;

impl OwnedDatumList for () {
    type RefList<'a> = ();

    const IS_EMPTY: bool = true;
    // arbitrary Datum, just as a placeholder
    type ListHead = bool;
    type ListTail = ();

    fn list_head(&self) -> &Self::ListHead {
        unreachable!()
    }
    fn list_tail(&self) -> Self::ListTail {}

    fn accept(&self, _: &mut impl DatumVisitor) -> DBResult<()> {
        Ok(())
    }

    fn build_equivalent<'l>(
        _strs: impl Iterator<Item = &'l str>,
    ) -> impl BorrowedDatumList<'l, Self> {
    }
}

impl BorrowedDatumList<'_, ()> for () {
    const IS_EMPTY: bool = true;
    // arbitrary Datum, just as a placeholder
    type ListHead = bool;
    type ListTail = ();

    fn list_head(&self) -> &Self::ListHead {
        unreachable!()
    }
    fn list_tail(&self) -> Self::ListTail {}

    fn accept(&self, _: &mut impl DatumVisitor) -> DBResult<()> {
        Ok(())
    }
}

impl<T: OwnedDatum> OwnedDatumList for T {
    type ListHead = T;
    type ListTail = ();

    type RefList<'l>
        = T::RefData<'l>
    where
        Self: 'l;

    fn list_head(&self) -> &Self::ListHead {
        self
    }
    fn list_tail(&self) -> Self::ListTail {}

    fn accept(&self, visitor: &mut impl DatumVisitor) -> DBResult<()> {
        visitor.visit(self)
    }

    fn build_equivalent<'l>(
        mut strs: impl Iterator<Item = &'l str>,
    ) -> impl BorrowedDatumList<'l, Self> {
        StringQuery(strs.next().unwrap())
    }
}

impl<'a, O: OwnedDatum, T: BorrowedDatum<'a, O>> BorrowedDatumList<'a, O> for T {
    type ListHead = T;
    type ListTail = ();

    fn list_head(&self) -> &Self::ListHead {
        self
    }
    fn list_tail(&self) -> Self::ListTail {}

    fn accept(&self, visitor: &mut impl DatumVisitor) -> DBResult<()> {
        visitor.visit(self)
    }
}

impl<T0: OwnedDatum> OwnedDatumList for (T0,) {
    type ListHead = T0;
    type ListTail = ();

    type RefList<'l>
        = (T0::RefData<'l>,)
    where
        Self: 'l;

    fn list_head(&self) -> &Self::ListHead {
        &self.0
    }
    fn list_tail(&self) -> Self::ListTail {}

    fn accept(&self, visitor: &mut impl DatumVisitor) -> DBResult<()> {
        visitor.visit(&self.0)
    }

    fn build_equivalent<'l>(
        mut strs: impl Iterator<Item = &'l str>,
    ) -> impl BorrowedDatumList<'l, Self> {
        (StringQuery(strs.next().unwrap()),)
    }
}

impl<'a, O0: OwnedDatum, T0: BorrowedDatum<'a, O0>> BorrowedDatumList<'a, (O0,)> for (T0,) {
    type ListHead = T0;
    type ListTail = ();

    fn list_head(&self) -> &Self::ListHead {
        &self.0
    }
    fn list_tail(&self) -> Self::ListTail {}

    fn accept(&self, visitor: &mut impl DatumVisitor) -> DBResult<()> {
        visitor.visit(&self.0)
    }
}

macro_rules! datum_list {
    ($len:literal, $ty0:ident : $e0 : ident : $n0: tt, $($ty:ident : $e:ident : $n:tt),+) => {
        impl<$ty0: OwnedDatum, $($ty: OwnedDatum),*> OwnedDatumList for ($ty0, $($ty),*) {
            type ListHead = $ty0;
            type ListTail = ( $( $ty ),* , );

            type RefList<'l> = ($ty0 :: RefData<'l> , $( $ty :: RefData<'l> ),* );

            fn list_head(&self) -> &Self::ListHead { &self.0 }
            fn list_tail(&self) -> Self::ListTail {
                todo!()
            }

            fn accept(&self, visitor: &mut impl DatumVisitor) -> DBResult<()> {
                visitor.visit(&self. $n0)?;
                $(visitor.visit(&self. $n)?;)*

                Ok(())
            }

            fn build_equivalent<'l>(mut strs: impl Iterator<Item = &'l str>) -> impl BorrowedDatumList<'l, Self> {
                (
                    StringQuery(strs.next().unwrap()),
                    $( if $n == $n { StringQuery(strs.next().unwrap()) } else { panic!() } ),*
                )
            }
        }

        impl<'a, $e0: OwnedDatum, $ty0: BorrowedDatum<'a, $e0>, $($e: OwnedDatum, $ty: BorrowedDatum<'a, $e>),*> BorrowedDatumList<'a, ( $e0, $( $e ),* )> for ($ty0, $($ty),*) {
            type ListHead = $ty0;
            type ListTail = ( $( $ty ),* , );

            fn list_head(&self) -> &Self::ListHead { &self.0 }
            fn list_tail(&self) -> Self::ListTail {
                todo!()
            }

            fn accept(&self, visitor: &mut impl DatumVisitor) -> DBResult<()> {
                visitor.visit(&self. $n0)?;
                $(visitor.visit(&self. $n)?;)*
                Ok(())
            }
        }
    }
}

datum_list!(2, T0:E0:0, T1:E1:1);
datum_list!(3, T0:E0:0, T1:E1:1, T2:E2:2);
datum_list!(4, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3);
datum_list!(5, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4);
datum_list!(6, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5);
datum_list!(7, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6);
datum_list!(8, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6, T7:E7:7);
datum_list!(9, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6, T7:E7:7, T8:E8:8);
datum_list!(10, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6, T7:E7:7, T8:E8:8, T9:E9:9);
datum_list!(11, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6, T7:E7:7, T8:E8:8, T9:E9:9, T10:E10:10);
datum_list!(12, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6, T7:E7:7, T8:E8:8, T9:E9:9, T10:E10:10, T11:E11:11);
datum_list!(13, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6, T7:E7:7, T8:E8:8, T9:E9:9, T10:E10:10, T11:E11:11, T12:E12:12);
datum_list!(14, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6, T7:E7:7, T8:E8:8, T9:E9:9, T10:E10:10, T11:E11:11, T12:E12:12, T13:E13:13);
datum_list!(15, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6, T7:E7:7, T8:E8:8, T9:E9:9, T10:E10:10, T11:E11:11, T12:E12:12, T13:E13:13, T14:E14:14);
datum_list!(16, T0:E0:0, T1:E1:1, T2:E2:2, T3:E3:3, T4:E4:4, T5:E5:5, T6:E6:6, T7:E7:7, T8:E8:8, T9:E9:9, T10:E10:10, T11:E11:11, T12:E12:12, T13:E13:13, T14:E14:14, T15:E15:15);