Skip to main content

gaia_types/assembler/
mod.rs

1#![doc = include_str!("readme.md")]
2
3use byteorder::{ByteOrder, WriteBytesExt};
4use std::{
5    io::{Seek, SeekFrom, Write},
6    marker::PhantomData,
7};
8
9/// Binary writer for writing data to types that implement WriteBytesExt.
10///
11/// This is a generic struct that can wrap any type implementing WriteBytesExt,
12/// providing binary data writing functionality.
13#[derive(Copy, Clone, Debug)]
14pub struct BinaryWriter<W, E> {
15    writer: W,
16    endian: PhantomData<E>,
17}
18
19impl<W: Write, E> Write for BinaryWriter<W, E> {
20    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
21        self.writer.write(buf)
22    }
23
24    fn flush(&mut self) -> std::io::Result<()> {
25        self.writer.flush()
26    }
27}
28
29impl<W: Seek, E> Seek for BinaryWriter<W, E> {
30    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
31        self.writer.seek(pos)
32    }
33}
34
35impl<W, E> BinaryWriter<W, E> {
36    /// Create a new binary writer.
37    pub fn new(writer: W) -> Self {
38        Self { writer, endian: PhantomData }
39    }
40    /// Get the inner writer.
41    pub fn finish(self) -> W {
42        self.writer
43    }
44}
45
46impl<W: Write, E> BinaryWriter<W, E> {
47    /// Write a u8 value to the byte stream.
48    ///
49    /// # Arguments
50    ///
51    /// * `value` - The u8 value to write.
52    pub fn write_u8(&mut self, value: u8) -> std::io::Result<()>
53    where
54        W: Write,
55    {
56        self.writer.write_u8(value)
57    }
58
59    /// Write a u16 value to the byte stream.
60    ///
61    /// # Arguments
62    ///
63    /// * `value` - The u16 value to write.
64    pub fn write_u16(&mut self, value: u16) -> std::io::Result<()>
65    where
66        W: Write,
67        E: ByteOrder,
68    {
69        self.writer.write_u16::<E>(value)
70    }
71
72    /// Write a u32 value to the byte stream.
73    ///
74    /// # Arguments
75    ///
76    /// * `value` - The u32 value to write.
77    pub fn write_u32(&mut self, value: u32) -> std::io::Result<()>
78    where
79        W: Write,
80        E: ByteOrder,
81    {
82        self.writer.write_u32::<E>(value)
83    }
84
85    /// Write a u64 value to the byte stream.
86    ///
87    /// # Arguments
88    ///
89    /// * `value` - The u64 value to write.
90    pub fn write_u64(&mut self, value: u64) -> std::io::Result<()>
91    where
92        W: Write,
93        E: ByteOrder,
94    {
95        self.writer.write_u64::<E>(value)
96    }
97
98    /// Write an i16 value to the byte stream.
99    pub fn write_i16(&mut self, value: i16) -> std::io::Result<()>
100    where
101        W: Write,
102        E: ByteOrder,
103    {
104        self.writer.write_i16::<E>(value)
105    }
106
107    /// Write an i32 value to the byte stream.
108    pub fn write_i32(&mut self, value: i32) -> std::io::Result<()>
109    where
110        W: Write,
111        E: ByteOrder,
112    {
113        self.writer.write_i32::<E>(value)
114    }
115
116    /// Write an i64 value to the byte stream.
117    pub fn write_i64(&mut self, value: i64) -> std::io::Result<()>
118    where
119        W: Write,
120        E: ByteOrder,
121    {
122        self.writer.write_i64::<E>(value)
123    }
124
125    /// Write a byte array to the byte stream.
126    ///
127    /// # Arguments
128    ///
129    /// * `bytes` - The byte array to write.
130    pub fn write_bytes(&mut self, bytes: &[u8]) -> std::io::Result<()>
131    where
132        W: Write,
133    {
134        self.writer.write_all(bytes)
135    }
136}