criware_utf_core/
table.rs

1use crate::{Result, packet::Packet};
2
3/// A UTF table that can be read, written, and constructed from nothing
4///
5pub trait Table: Sized {
6    /**
7    Creates a new table with default constant values and no rows
8
9    # Example
10    ```
11    # use criware_utf::{Table, utf_table};
12    #[utf_table]
13    struct Tab {
14        #[constant]
15        constant: i32,
16        row_value: i64,
17    }
18
19    fn main() {
20        let table = Tab::new();
21        assert_eq!(table.constants.constant, 0);
22        assert_eq!(table.rows.len(), 0);
23    }
24    ```
25     */
26    fn new() -> Self;
27
28    /**
29    Reads a table from the given stream
30
31    If the table is malformed, or if the table's schema does not match this
32    type, then this function will fail.
33
34    # Example
35    ```
36    # use std::fs::File;
37    # use criware_utf::{Table, utf_table};
38    #[utf_table]
39    struct Tab {
40        #[constant]
41        constant: i32,
42        row_value: i64,
43    }
44
45    fn main() -> Result<(), Box<dyn std::error::Error>> {
46        let mut file = File::open("table.bin")?;
47        let table = Tab::read(&mut file)?;
48        // ... do something ...
49        Ok(())
50    }
51    ```
52     */
53    fn read(reader: &mut dyn std::io::Read) -> Result<Self>;
54
55    /**
56    Writes a table to the given stream
57
58    # Example
59    ```
60    # use std::fs::File;
61    # use criware_utf::{Table, utf_table};
62    #[utf_table]
63    struct Tab {
64        #[constant]
65        constant: i32,
66        row_value: i64,
67    }
68
69    fn main() -> Result<(), Box<dyn std::error::Error>> {
70        let mut file = File::create("table.bin")?;
71        let table = Tab::new();
72        // ... do something ...
73        table.write(&mut file)?;
74        Ok(())
75    }
76    ```
77     */
78    fn write(&self, writer: &mut dyn std::io::Write) -> Result<()>;
79
80    /**
81    Reads a UTF table packet from the given stream, verifying that it has
82    the given 4-byte prefix.
83
84    # Example
85    ```
86    # use std::fs::File;
87    # use criware_utf::{Packet, Table, utf_table};
88    #[utf_table]
89    struct Tab {
90        #[constant]
91        constant: i32,
92        row_value: i64,
93    }
94
95    fn main() -> Result<(), Box<dyn std::error::Error>> {
96        let mut file = File::open("table.bin")?;
97        let table: Packet<Tab> = Tab::read_packet(&mut file, b"TAB ")?;
98        // ... do something ...
99        Ok(())
100    }
101    ```
102     */
103    fn read_packet(
104        reader: &mut dyn std::io::Read,
105        prefix: &'static [u8; 4],
106    ) -> Result<Packet<Self>> {
107        Packet::<Self>::read_packet(reader, prefix)
108    }
109}