1+ //! Header module for the binseq library
2+ //!
3+ //! This module provides the header structure and functionality for binary sequence files.
4+ //! The header contains metadata about the binary sequence data, including format version,
5+ //! sequence length, and other information necessary for proper interpretation of the data.
6+
17use byteorder:: { ByteOrder , LittleEndian } ;
28use std:: io:: { Read , Write } ;
39
410use crate :: { error:: Result , HeaderError } ;
511
6- /// Current magic number: "BSEQ" in ASCII
12+ /// Current magic number: "BSEQ" in ASCII (in little-endian byte order)
13+ ///
14+ /// This is used to identify binary sequence files and verify file integrity.
715const MAGIC : u32 = 0x51455342 ;
816
9- /// Current format version
17+ /// Current format version of the binary sequence file format
18+ ///
19+ /// This version number allows for future format changes while maintaining backward compatibility.
1020const FORMAT : u8 = 1 ;
1121
1222/// Size of the header in bytes
23+ ///
24+ /// The header has a fixed size to ensure consistent reading and writing of binary sequence files.
1325pub const SIZE_HEADER : usize = 32 ;
1426
27+ /// Header structure for binary sequence files
28+ ///
29+ /// The `BinseqHeader` contains metadata about the binary sequence data stored in a file,
30+ /// including format information, sequence lengths, and space for future extensions.
31+ ///
32+ /// The total size of this structure is 32 bytes, with a fixed layout to ensure
33+ /// consistent reading and writing across different platforms.
1534#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
1635pub struct BinseqHeader {
1736 /// Magic number to identify the file format
@@ -40,6 +59,19 @@ pub struct BinseqHeader {
4059 pub reserved : [ u8 ; 19 ] ,
4160}
4261impl BinseqHeader {
62+ /// Creates a new header with the specified sequence length
63+ ///
64+ /// This constructor initializes a standard header with the given sequence length,
65+ /// setting the magic number and format version to their default values.
66+ /// The extended sequence length (xlen) is set to 0.
67+ ///
68+ /// # Arguments
69+ ///
70+ /// * `slen` - The length of sequences in the file
71+ ///
72+ /// # Returns
73+ ///
74+ /// A new `BinseqHeader` instance
4375 pub fn new ( slen : u32 ) -> Self {
4476 Self {
4577 magic : MAGIC ,
@@ -50,6 +82,19 @@ impl BinseqHeader {
5082 }
5183 }
5284
85+ /// Creates a new header with both primary and extended sequence lengths
86+ ///
87+ /// This constructor initializes a header for files that contain both primary
88+ /// and secondary sequence data, such as quality scores or annotations.
89+ ///
90+ /// # Arguments
91+ ///
92+ /// * `slen` - The length of primary sequences in the file
93+ /// * `xlen` - The length of secondary/extended sequences in the file
94+ ///
95+ /// # Returns
96+ ///
97+ /// A new `BinseqHeader` instance with extended sequence information
5398 pub fn new_extended ( slen : u32 , xlen : u32 ) -> Self {
5499 Self {
55100 magic : MAGIC ,
@@ -60,6 +105,26 @@ impl BinseqHeader {
60105 }
61106 }
62107
108+ /// Parses a header from a fixed-size byte array
109+ ///
110+ /// This method validates the magic number and format version before constructing
111+ /// a header instance. If validation fails, appropriate errors are returned.
112+ ///
113+ /// # Arguments
114+ ///
115+ /// * `buffer` - A byte array of exactly `SIZE_HEADER` bytes containing the header data
116+ ///
117+ /// # Returns
118+ ///
119+ /// * `Ok(BinseqHeader)` - A valid header parsed from the buffer
120+ /// * `Err(Error)` - If the buffer contains invalid header data
121+ ///
122+ /// # Errors
123+ ///
124+ /// Returns an error if:
125+ /// * The magic number is incorrect
126+ /// * The format version is unsupported
127+ /// * The reserved bytes are invalid
63128 pub fn from_bytes ( buffer : & [ u8 ; SIZE_HEADER ] ) -> Result < Self > {
64129 let magic = LittleEndian :: read_u32 ( & buffer[ 0 ..4 ] ) ;
65130 if magic != MAGIC {
@@ -84,7 +149,26 @@ impl BinseqHeader {
84149 } )
85150 }
86151
87- /// Parses an arbitrarily sized buffer
152+ /// Parses a header from an arbitrarily sized buffer
153+ ///
154+ /// This method extracts the header from the beginning of a buffer that may be larger
155+ /// than the header size. It checks that the buffer is at least as large as the header
156+ /// before attempting to parse it.
157+ ///
158+ /// # Arguments
159+ ///
160+ /// * `buffer` - A byte slice containing at least `SIZE_HEADER` bytes
161+ ///
162+ /// # Returns
163+ ///
164+ /// * `Ok(BinseqHeader)` - A valid header parsed from the buffer
165+ /// * `Err(Error)` - If the buffer is too small or contains invalid header data
166+ ///
167+ /// # Errors
168+ ///
169+ /// Returns an error if:
170+ /// * The buffer is smaller than `SIZE_HEADER`
171+ /// * The header data is invalid (see `from_bytes` for validation details)
88172 pub fn from_buffer ( buffer : & [ u8 ] ) -> Result < Self > {
89173 let mut bytes = [ 0u8 ; SIZE_HEADER ] ;
90174 if buffer. len ( ) < SIZE_HEADER {
@@ -94,6 +178,23 @@ impl BinseqHeader {
94178 Self :: from_bytes ( & bytes)
95179 }
96180
181+ /// Writes the header to a writer
182+ ///
183+ /// This method serializes the header to its binary representation and writes it
184+ /// to the provided writer.
185+ ///
186+ /// # Arguments
187+ ///
188+ /// * `writer` - Any type that implements the `Write` trait
189+ ///
190+ /// # Returns
191+ ///
192+ /// * `Ok(())` - If the header was successfully written
193+ /// * `Err(Error)` - If writing to the writer failed
194+ ///
195+ /// # Errors
196+ ///
197+ /// Returns an error if writing to the writer fails (typically an I/O error).
97198 pub fn write_bytes < W : Write > ( & self , writer : & mut W ) -> Result < ( ) > {
98199 let mut buffer = [ 0u8 ; SIZE_HEADER ] ;
99200 LittleEndian :: write_u32 ( & mut buffer[ 0 ..4 ] , self . magic ) ;
@@ -105,6 +206,25 @@ impl BinseqHeader {
105206 Ok ( ( ) )
106207 }
107208
209+ /// Reads a header from a reader
210+ ///
211+ /// This method reads exactly `SIZE_HEADER` bytes from the provided reader and
212+ /// parses them into a header structure.
213+ ///
214+ /// # Arguments
215+ ///
216+ /// * `reader` - Any type that implements the `Read` trait
217+ ///
218+ /// # Returns
219+ ///
220+ /// * `Ok(BinseqHeader)` - A valid header read from the reader
221+ /// * `Err(Error)` - If reading from the reader failed or the header data is invalid
222+ ///
223+ /// # Errors
224+ ///
225+ /// Returns an error if:
226+ /// * Reading from the reader fails (typically an I/O error)
227+ /// * The header data is invalid (see `from_bytes` for validation details)
108228 pub fn from_reader < R : Read > ( reader : & mut R ) -> Result < Self > {
109229 let mut buffer = [ 0u8 ; SIZE_HEADER ] ;
110230 reader. read_exact ( & mut buffer) ?;
0 commit comments