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
macro_rules! impl_file {
    (
        $(#[$($meta:meta)+])*
        pub enum $enum_name:ident {
            $(
                $(#[$($var_meta:meta)+])*
                $var_name:ident($var_type:ty),
            )*
        }
    ) => {
        $(#[$($meta)+])*
        pub enum $enum_name {
            $(
                $(#[$($var_meta)+])*
                $var_name($var_type),
            )*
        }

        $(
            $(#[$($var_meta)+])*
            impl From<$var_type> for $enum_name {
                fn from(file: $var_type) -> Self {
                    $enum_name::$var_name(file)
                }
            }
        )*

        impl std::io::Read for $enum_name {
            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
                match self {
                    $(
                        $(#[$($var_meta)+])*
                        $enum_name::$var_name(ref mut file) => file.read(buf),
                    )*
                }
            }
        }

        impl std::io::Seek for File {
            fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
                match self {
                    $(
                        $(#[$($var_meta)+])*
                        $enum_name::$var_name(ref mut file) => file.seek(pos),
                    )*
                }
            }
        }
    }
}

macro_rules! store_entries {
    ($self:ident, $path:expr, $head:ident,) => { $self.0.entries_path($path)? };
    ($self:ident, $path:expr, $head:ident, $($tail:ident,)+) => {
        $self.0.entries_path($path)?.chain(store_entries!($self, $path, $($tail,)+) )
    }
}

macro_rules! store_tuples {
    ($head:ident,) => {};
    ($head:ident, $($tail:ident,)+) => {
        impl<$head, $($tail,)+> Store for ($head, $($tail,)+)
        where
            $head: Store,
            $($tail: Store,)+
            $head::File: Into<$crate::File>,
            $($tail::File: Into<$crate::File>,)+
        {
            type File = $crate::File;
            #[allow(non_snake_case)]
            fn open_path(&self, path: &Path) -> io::Result<Self::File> {
                let ($head, $($tail,)+) = self;
                match $head.open_path(path) {
                    Ok(file) => return Ok(file.into()),
                    Err(ref err) if err.kind() == io::ErrorKind::NotFound => {},
                    Err(err) => return Err(err),
                }
                $(
                match $tail.open_path(path) {
                    Ok(file) => return Ok(file.into()),
                    Err(ref err) if err.kind() == io::ErrorKind::NotFound => {},
                    Err(err) => return Err(err),
                }
                )+

                Err(io::Error::from(io::ErrorKind::NotFound))
            }

            fn entries_path(&self, path: &Path) -> io::Result<Entries> {
                // chain all elements from the tuple
                let raw = store_entries!(self, path, $head, $($tail,)+);
                Ok(Entries::new(TupleEntries::new(raw)))
            }
        }
        store_tuples!($($tail,)+);
    };
}