1use crate::{traits::BinaryFormat, TrackingReader};
2use gaia_types::{GaiaError, Result};
3use std::io::{Read, Seek};
4
5pub struct BinaryReader<R: Read, F: BinaryFormat> {
7 inner: TrackingReader<R>,
8 diagnostics: Vec<GaiaError>,
9 _marker: std::marker::PhantomData<F>,
10}
11
12impl<R: Read, F: BinaryFormat> BinaryReader<R, F> {
13 pub fn new(inner: R) -> Self {
15 Self { inner: TrackingReader { inner, position: 0 }, diagnostics: Vec::new(), _marker: std::marker::PhantomData }
16 }
17
18 pub fn position(&self) -> u64 {
20 self.inner.position
21 }
22
23 pub fn get_position(&self) -> u64 {
25 self.inner.position
26 }
27
28 pub fn set_position(&mut self, pos: u64) -> Result<u64>
30 where
31 R: std::io::Seek,
32 {
33 use std::io::Seek;
34 self.seek(std::io::SeekFrom::Start(pos)).map_err(|_| GaiaError::invalid_data("Seek failed"))
35 }
36
37 pub fn stream_position(&mut self) -> Result<u64>
39 where
40 R: std::io::Seek,
41 {
42 use std::io::Seek;
43 self.inner.stream_position().map_err(|_| GaiaError::invalid_data("Stream position failed"))
44 }
45
46 pub fn add_error(&mut self, error: GaiaError) {
48 self.diagnostics.push(error);
49 }
50
51 pub fn take_errors(&mut self) -> Vec<GaiaError> {
53 std::mem::take(&mut self.diagnostics)
54 }
55
56 pub fn read_u8(&mut self) -> Result<u8> {
58 let mut buf = [0u8; 1];
59 self.inner.read_exact(&mut buf).map_err(|_| GaiaError::truncated())?;
60 Ok(buf[0])
61 }
62
63 pub fn read_u16(&mut self) -> Result<u16> {
65 F::read_u16(&mut self.inner)
66 }
67
68 pub fn read_u32(&mut self) -> Result<u32> {
70 F::read_u32(&mut self.inner)
71 }
72
73 pub fn read_u64(&mut self) -> Result<u64> {
75 F::read_u64(&mut self.inner)
76 }
77
78 pub fn read_i16(&mut self) -> Result<i16> {
80 F::read_i16(&mut self.inner)
81 }
82
83 pub fn read_i32(&mut self) -> Result<i32> {
85 F::read_i32(&mut self.inner)
86 }
87
88 pub fn read_i64(&mut self) -> Result<i64> {
90 F::read_i64(&mut self.inner)
91 }
92
93 pub fn read_f32(&mut self) -> Result<f32> {
95 F::read_f32(&mut self.inner)
96 }
97
98 pub fn read_f64(&mut self) -> Result<f64> {
100 F::read_f64(&mut self.inner)
101 }
102
103 pub fn read_bytes(&mut self, len: usize) -> Result<Vec<u8>> {
105 let mut buf = vec![0u8; len];
106 self.inner.read_exact(&mut buf).map_err(|_| GaiaError::truncated())?;
107 Ok(buf)
108 }
109
110 pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
112 self.inner.read_exact(buf).map_err(|_| GaiaError::truncated())
113 }
114
115 pub fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
117 self.inner.read_to_end(buf).map_err(|_| GaiaError::truncated())
118 }
119}
120
121impl<R: Read + std::io::Seek, F: BinaryFormat> std::io::Seek for BinaryReader<R, F> {
122 fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
123 self.inner.seek(pos)
124 }
125}
126
127impl<R: Read + std::io::Seek, F: BinaryFormat> BinaryReader<R, F> {
128 pub fn seek_gaia(&mut self, pos: std::io::SeekFrom) -> Result<u64> {
130 self.seek(pos).map_err(|_| GaiaError::invalid_data("Seek failed"))
131 }
132}
133
134impl<R: Read + std::fmt::Debug, F: BinaryFormat> std::fmt::Debug for BinaryReader<R, F> {
135 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136 f.debug_struct("BinaryReader").field("inner", &self.inner).field("position", &self.position()).finish()
137 }
138}