larian_formats/
format.rs

1use std::io::{Read, Seek};
2
3use crate::{error::Result, reader::ReadSeek};
4
5/// Contains logic for parsing a specific file format.
6pub trait Parser: Default {
7    /// The "magic number" bytes identifying the type of file.
8    const ID_BYTES: [u8; 4];
9
10    /// The minimum version of the file format this parser supports.
11    const MIN_SUPPORTED_VERSION: u32;
12
13    /// The result of successfully parsing a file.
14    type Output;
15
16    /// Attempts to parse a file with the given format's semantics.
17    fn read(&mut self, reader: &mut ReadSeek<impl Read + Seek>) -> Result<Self::Output>;
18}
19
20/// Contains logic for writing a specific file format.
21pub trait Write {
22    /// The "magic number" bytes identifying the type of file.
23    const ID_BYTES: [u8; 4];
24
25    /// The version of the file format this writer will create.
26    const OUTPUT_VERSION: u32;
27
28    /// The result of successfully parsing a file.
29    type Input;
30
31    /// Attempts to create a file with the given format's semantics.
32    fn write(self, input: Self::Input) -> Result<()>;
33}