postscript/compact1/index/
mod.rs

1//! The indices.
2
3use crate::compact1::{Offset, OffsetSize};
4use crate::Result;
5
6table! {
7    @define
8    /// An index.
9    pub Index {
10        count       (u16         ), // count
11        offset_size (OffsetSize  ), // offSize
12        offsets     (Vec<Offset> ), // offset
13        data        (Vec<Vec<u8>>), // data
14    }
15}
16
17dereference! { Index::data => [Vec<u8>] }
18
19impl crate::value::Read for Index {
20    fn read<T: crate::tape::Read>(tape: &mut T) -> Result<Self> {
21        let count = tape.take::<u16>()?;
22        if count == 0 {
23            return Ok(Index {
24                count: 0,
25                offset_size: 0,
26                offsets: vec![],
27                data: vec![],
28            });
29        }
30        let offset_size = tape.take::<OffsetSize>()?;
31        let mut offsets = Vec::with_capacity(count as usize + 1);
32        for _ in 0..(count as usize + 1) {
33            let offset = tape.take_given::<Offset>(offset_size)?;
34            offsets.push(offset);
35        }
36        if offsets[0] != Offset(1) {
37            raise!("found a malformed index");
38        }
39        let mut data = Vec::with_capacity(count as usize);
40        for i in 0..(count as usize) {
41            if offsets[i] > offsets[i + 1] {
42                raise!("found a malformed index");
43            }
44            let size = (offsets[i + 1].0 - offsets[i].0) as usize;
45            data.push(tape.take_given(size)?);
46        }
47        Ok(Index {
48            count,
49            offset_size,
50            offsets,
51            data,
52        })
53    }
54}
55
56macro_rules! index {
57    ($(#[$attribute:meta])* pub $structure:ident) => (
58        index! { @define $(#[$attribute])* pub $structure }
59        index! { @implement $structure }
60    );
61    (@define $(#[$attribute:meta])* pub $structure:ident) => (
62        $(#[$attribute])*
63        #[derive(Clone, Debug)]
64        pub struct $structure(pub $crate::compact1::index::Index);
65        dereference! { $structure::0 => $crate::compact1::index::Index }
66    );
67    (@implement $structure:ident) => (
68        impl $crate::value::Read for $structure {
69            #[inline]
70            fn read<T: $crate::tape::Read>(tape: &mut T) -> $crate::Result<Self> {
71                Ok($structure(tape.take()?))
72            }
73        }
74    );
75}
76
77mod character_strings;
78mod dictionaries;
79mod names;
80mod strings;
81mod subroutines;
82
83pub use character_strings::CharacterStrings;
84pub use dictionaries::Dictionaries;
85pub use names::Names;
86pub use strings::Strings;
87pub use subroutines::Subroutines;