iof/read/
tuple.rs

1use crate::{fmt::Format, BufReadExt, ReadFrom, ReadFromError};
2use std::fmt::{self, Display};
3
4macro_rules! impl_read_into_for_tuple {
5    ($e:ident) => {
6        use std::convert::Infallible as $e;
7        impl ReadFrom for () {
8            type ParseError = $e;
9            #[inline]
10            fn try_read_from<F: Format, S: BufReadExt>(_stream: &mut S, _format: F) -> Result<(), ReadFromError<Self>> {
11                Ok(())
12            }
13        }
14    };
15    ($e:ident $($t:ident)+) => {
16        #[derive(Debug)]
17        pub enum $e<$($t, )+ > {
18            $($t($t), )+
19        }
20        impl<$($t: std::error::Error, )+ > Display for $e<$($t, )+ > {
21            #[inline]
22            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23                match self { $( Self::$t(err) => Display::fmt(err, f), )+ }
24            }
25        }
26        impl<$($t: std::error::Error, )+ > std::error::Error for $e<$($t, )+ > {}
27        impl<$($t: ReadFrom, )+> ReadFrom for ( $($t, )+ ) {
28            type ParseError = $e<$(<$t as ReadFrom>::ParseError, )+>;
29            #[inline]
30            fn try_read_from<F: Format, S: BufReadExt>(stream: &mut S, format: F) -> Result<($($t, )+), ReadFromError<Self>> {
31                Ok(( $(<$t as ReadFrom>::try_read_from(stream, format).map_err(|err| match err {
32                    ReadFromError::<$t>::IOError(e) => ReadFromError::<Self>::IOError(e),
33                    ReadFromError::<$t>::EOF => ReadFromError::<Self>::EOF,
34                    ReadFromError::<$t>::EOL => ReadFromError::<Self>::EOL,
35                    ReadFromError::<$t>::UnexpectedChar(c, s) => ReadFromError::<Self>::UnexpectedChar(c, s),
36                    // ReadFromError::<$t>::Unfulfilled(s) => ReadFromError::<Self>::Unfulfilled(s),
37                    ReadFromError::<$t>::FromStrError(e, s, n) => ReadFromError::<Self>::FromStrError($e::$t(e), s, n),
38                })?, )+ ))
39            }
40        }
41    };
42}
43
44impl_read_into_for_tuple!(Tuple0Error);
45impl_read_into_for_tuple!(Tuple1Error T1);
46impl_read_into_for_tuple!(Tuple2Error T1 T2);
47impl_read_into_for_tuple!(Tuple3Error T1 T2 T3);
48impl_read_into_for_tuple!(Tuple4Error T1 T2 T3 T4);
49impl_read_into_for_tuple!(Tuple5Error T1 T2 T3 T4 T5);
50impl_read_into_for_tuple!(Tuple6Error T1 T2 T3 T4 T5 T6);
51impl_read_into_for_tuple!(Tuple7Error T1 T2 T3 T4 T5 T6 T7);
52impl_read_into_for_tuple!(Tuple8Error T1 T2 T3 T4 T5 T6 T7 T8);
53impl_read_into_for_tuple!(Tuple9Error T1 T2 T3 T4 T5 T6 T7 T8 T9);
54impl_read_into_for_tuple!(Tuple10Error T1 T2 T3 T4 T5 T6 T7 T8 T9 T10);
55impl_read_into_for_tuple!(Tuple11Error T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11);
56impl_read_into_for_tuple!(Tuple12Error T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12);