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
//! The indices.

use {Result, Tape, Value};
use compact1::{Offset, OffsetSize};

table! {
    @define
    #[doc = "An index."]
    pub Index {
        count       (u16         ), // count
        offset_size (OffsetSize  ), // offSize
        offsets     (Vec<Offset> ), // offset
        data        (Vec<Vec<u8>>), // data
    }
}

deref! { Index::data => [Vec<u8>] }

impl Value for Index {
    fn read<T: Tape>(tape: &mut T) -> Result<Self> {
        let count = tape.take::<u16>()?;
        if count == 0 {
            return Ok(Index { count: 0, offset_size: 0, offsets: vec![], data: vec![] });
        }
        let offset_size = tape.take::<OffsetSize>()?;
        let mut offsets = Vec::with_capacity(count as usize + 1);
        for i in 0..(count as usize + 1) {
            let offset = tape.take_given::<Offset>(offset_size)?;
            if i == 0 && offset != Offset(1) || i > 0 && offset <= offsets[i - 1] {
                raise!("found a malformed index");
            }
            offsets.push(offset);
        }
        let mut data = Vec::with_capacity(count as usize);
        for i in 0..(count as usize) {
            let size = (u32::from(offsets[i + 1]) - u32::from(offsets[i])) as usize;
            data.push(tape.take_given(size)?);
        }
        Ok(Index { count: count, offset_size: offset_size, offsets: offsets, data: data })
    }
}

macro_rules! index {
    ($(#[$attribute:meta])* pub $structure:ident) => (
        index! { @define $(#[$attribute])* pub $structure }
        index! { @implement $structure }
    );
    (@define $(#[$attribute:meta])* pub $structure:ident) => (
        $(#[$attribute])*
        #[derive(Clone, Debug)]
        pub struct $structure(pub ::compact1::index::Index);
        deref! { $structure::0 => ::compact1::index::Index }
    );
    (@implement $structure:ident) => (
        impl ::tape::Value for $structure {
            #[inline]
            fn read<T: ::tape::Tape>(tape: &mut T) -> ::Result<Self> {
                Ok($structure(tape.take()?))
            }
        }
    );
}

mod char_strings;
mod dictionaries;
mod names;
mod strings;
mod subroutines;

pub use self::char_strings::CharStrings;
pub use self::dictionaries::Dictionaries;
pub use self::names::Names;
pub use self::strings::Strings;
pub use self::subroutines::Subroutines;