@@ -9,6 +9,7 @@ use libtw2_warn::Warn;
99use std:: io;
1010use thiserror:: Error ;
1111
12+ use crate :: format:: ChunkHeader ;
1213use crate :: format:: TickMarker ;
1314use crate :: format:: Warning ;
1415use crate :: format:: MAX_SNAPSHOT_SIZE ;
@@ -53,6 +54,13 @@ pub struct Reader<'a> {
5354 huffman : ArrayVec < [ u8 ; MAX_SNAPSHOT_SIZE ] > ,
5455}
5556
57+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
58+ pub enum ChunkType {
59+ Tick ,
60+ Snapshot ,
61+ Message ,
62+ }
63+
5664impl < ' a > Reader < ' a > {
5765 pub fn new < W , R > ( mut data : R , warn : & mut W ) -> Result < Reader < ' a > , ReadError >
5866 where
@@ -163,6 +171,31 @@ impl<'a> Reader<'a> {
163171 }
164172 }
165173 }
174+ /// Peeks into the next chunk header to determine the next chunk type.
175+ /// It returns `None` in one of three cases:
176+ /// - the demo ends
177+ /// - the stream position couldn't be determined
178+ /// - a read or parsing error happened
179+ /// If `Ok` is returned, the reader's position is unchanged.
180+ /// If `Err` is returned, the reader's position is unspecified.
181+ pub ( crate ) fn next_chunk_type ( & mut self ) -> Result < Option < ChunkType > , io:: Error > {
182+ let position = self . stream_position ( ) ?;
183+ let chunk_type =
184+ match ChunkHeader :: read ( & mut self . data , self . start . version , & mut libtw2_warn:: Ignore ) {
185+ Ok ( Some ( ChunkHeader :: Tick { .. } ) ) => Some ( ChunkType :: Tick ) ,
186+ Ok ( Some ( ChunkHeader :: Data {
187+ kind : format:: DataKind :: Snapshot | format:: DataKind :: SnapshotDelta ,
188+ ..
189+ } ) ) => Some ( ChunkType :: Snapshot ) ,
190+ Ok ( Some ( ChunkHeader :: Data {
191+ kind : format:: DataKind :: Message ,
192+ ..
193+ } ) ) => Some ( ChunkType :: Message ) ,
194+ _ => None ,
195+ } ;
196+ self . data . seek ( io:: SeekFrom :: Start ( position) ) ?;
197+ Ok ( chunk_type)
198+ }
166199 /// Gets the position of the underlying reader.
167200 pub fn stream_position ( & mut self ) -> Result < u64 , io:: Error > {
168201 self . data . stream_position ( )
0 commit comments