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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Implements the basic reader/writer functionality for HFF.
#![warn(missing_docs)]
use hff_core::{Ecc, Error, Result};

mod data_source;
pub use data_source::{DataSource, StdWriter};

mod chunk_desc;
use chunk_desc::ChunkDesc;

mod table_desc;
pub use table_desc::TableDesc;

mod table_builder;
pub use table_builder::TableBuilder;

mod data_builder;
pub use data_builder::DataBuilder;

mod writer;
pub use writer::write_stream;

mod reader;
pub use reader::{
    read_stream, read_stream_full, ChunkCache, ChunkIter, ChunkView, DepthFirstIter, Hff, ReadSeek,
    TableIter, TableView,
};

/// Create a table structure to be contained in the HFF.
/// Panics if the given Ecc data is invalid.
pub fn table(primary: impl Into<Ecc>, secondary: impl Into<Ecc>) -> TableBuilder {
    TableBuilder::new(primary.into(), secondary.into())
}

#[cfg(test)]
mod tests {
    use crate::reader::{ChunkCache, Hff};

    use super::*;

    fn test_table() -> Result<TableDesc> {
        Ok(table("Test", "TestSub")
            .metadata("This is some metadata attached to the table.")?
            .chunk("TRC0", "TRS0", "Chunks can be most types.")?
            .chunk(
                "TRC1",
                "TRS1",
                "Both chunks and tables will maintain their order within the file.",
            )?
            .chunk(
                "TRC2",
                "TRS2",
                "But, there is no relationship maintained between chunks and tables.",
            )?
            .chunk(
                "TRC3",
                "TRS3",
                "In other words, we are creating two lists, one for chunks and one for tables.",
            )?
            .chunk(
                "TRC4",
                "TRS4",
                "So, the order of adding chunks inbetween tables has no impact on the result.",
            )?
            .chunk(
                "TRC5",
                "TRS5",
                "Only which table the chunk is associated with will matter..",
            )?
            .table(
                table("C0Prime", "C0Sub")
                    .metadata("Each table has its own metadata.")?
                    .chunk("C0C0", "C0S0", "Each table also has its own set of chunks.")?
                    .table(
                        table("C1Prime", "C1Sub")
                            .chunk(
                                "C1C0",
                                "C1S0",
                                "They will only be listed while iterating that specific table.",
                            )?
                            .end(),
                    )
                    .table(
                        table("C2Prime", "C2Sub")
                            .table(table("C3Prime", "C3Sub")
                            .chunk("C2C0", "C2S0", "Tables don't *have* to have chunks, tables can be used to simply contain other tables.")?
                            .end())
                            .end(),
                    )
                    .end(),
            )
            .table(table("C4Prime", "C4Sub").chunk("C4C0", "C4S0","The last chunk in the overall file.")?
                .metadata("And we're done.")?.end())
            .end())
    }

    fn checks(hff: &Hff, cache: &mut ChunkCache) {
        {
            // Check the content of root is as expected.
            let root = hff.tables().next().unwrap();
            assert_eq!(root.primary(), "Test".into());
            assert_eq!(root.secondary(), "TestSub".into());
            assert_eq!(root.child_count(), 2);
            assert_eq!(root.chunk_count(), 6);

            // Check that we get a proper child iterator from the root.
            let mut root_children = root.iter();
            let c0 = root_children.next().unwrap();
            assert_eq!(c0.primary(), "C0Prime".into());
            let c4 = root_children.next().unwrap();
            assert_eq!(c4.primary(), "C4Prime".into());
            assert_eq!(root_children.next(), None);
        }

        {
            // Check the metadata for the root.
            let root = hff.tables().next().unwrap();
            let metadata = root.metadata(cache).unwrap();
            assert!(std::str::from_utf8(&metadata)
                .unwrap()
                .starts_with("This is some metadata"));

            // Check the last table (second root child) metadata.
            let mut children = hff.tables().next().unwrap().iter();
            children.next();
            let c4 = children.next().unwrap();
            let metadata = c4.metadata(cache).unwrap();
            assert!(std::str::from_utf8(&metadata)
                .unwrap()
                .starts_with("And we're done."));
        }

        {
            // Check the root chunks are as expected.
            let root = hff.tables().next().unwrap();

            let test_data = [
                ("TRC0", "TRS0", "Chunks can be most types."),
                (
                    "TRC1",
                    "TRS1",
                    "Both chunks and tables will maintain their order within the file.",
                ),
                (
                    "TRC2",
                    "TRS2",
                    "But, there is no relationship maintained between chunks and tables.",
                ),
                (
                    "TRC3",
                    "TRS3",
                    "In other words, we are creating two lists, one for chunks and one for tables.",
                ),
                (
                    "TRC4",
                    "TRS4",
                    "So, the order of adding chunks inbetween tables has no impact on the result.",
                ),
                (
                    "TRC5",
                    "TRS5",
                    "Only which table the chunk is associated with will matter..",
                ),
            ];
            for (index, chunk) in root.chunks().enumerate() {
                let test_entry = test_data[index];
                assert_eq!(Ecc::new(test_entry.0), chunk.primary());
                assert_eq!(Ecc::new(test_entry.1), chunk.secondary());
                assert_eq!(chunk.size(), test_entry.2.len());
                assert_eq!(
                    chunk.data(cache).unwrap(),
                    Vec::from(test_entry.2.as_bytes())
                );
            }

            {
                let test_data = [
                    (0, "Test", "TestSub"),
                    (1, "C0Prime", "C0Sub"),
                    (2, "C1Prime", "C1Sub"),
                    (2, "C2Prime", "C2Sub"),
                    (3, "C3Prime", "C3Sub"),
                    (1, "C4Prime", "C4Sub"),
                ];
                // Test depth first iteration.
                for ((depth, table), data) in hff.depth_first().zip(test_data.iter()) {
                    assert_eq!(depth, data.0);
                    assert_eq!(table.primary(), data.1.into());
                    assert_eq!(table.secondary(), data.2.into());
                }
            }
        }
    }

    #[test]
    fn test() {
        // Simple dev test for structure.
        {
            let table = test_table().unwrap();
            let mut buffer = vec![];

            assert!(write_stream::<hff_core::NE>("Test", table, &mut buffer).is_ok());

            // Read it back in and iterate.
            let (hff, mut cache) = read_stream_full(&mut buffer.as_slice()).unwrap();
            checks(&hff, &mut cache);
        }

        // Simple dev test for structure.
        {
            let table = test_table().unwrap();
            let mut buffer = vec![];

            assert!(write_stream::<hff_core::OP>("Test", table, &mut buffer).is_ok());

            // Read it back in and iterate.
            let (hff, mut cache) = read_stream_full(&mut buffer.as_slice()).unwrap();
            checks(&hff, &mut cache);
        }
    }
}