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

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

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

dereference! { 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 _ in 0..(count as usize + 1) {
            let offset = tape.take_given::<Offset>(offset_size)?;
            offsets.push(offset);
        }
        if offsets[0] != Offset(1) {
            raise!("found a malformed index");
        }
        let mut data = Vec::with_capacity(count as usize);
        for i in 0..(count as usize) {
            if offsets[i] > offsets[i + 1] {
                raise!("found a malformed index");
            }
            let size = (offsets[i + 1].0 - offsets[i].0) as usize;
            data.push(tape.take_given(size)?);
        }
        Ok(Index {
            count,
            offset_size,
            offsets,
            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 crate::compact1::index::Index);
        dereference! { $structure::0 => crate::compact1::index::Index }
    );
    (@implement $structure:ident) => (
        impl typeface::Value for $structure {
            #[inline]
            fn read<T: typeface::Tape>(tape: &mut T) -> typeface::Result<Self> {
                Ok($structure(tape.take()?))
            }
        }
    );
}

mod character_strings;
mod dictionaries;
mod names;
mod strings;
mod subroutines;

pub use character_strings::CharacterStrings;
pub use dictionaries::Dictionaries;
pub use names::Names;
pub use strings::Strings;
pub use subroutines::Subroutines;