pgs_parse/
pgs_read.rs

1//! # PGS Read Trait
2//!
3//! Defines traits for reading data from PGS files or buffers, with support for seeking.
4use std::io::Read;
5
6use crate::pgs_error::Result;
7
8/// A trait for seeking within a read/write context.
9///
10/// This trait provides methods for seeking to a specific position, querying the current position, getting the length
11/// of the data, and checking if the data is non-empty.
12pub trait PgsSeek {
13    /// Seeks to a specific position in the data.
14    ///
15    /// # Arguments
16    /// * `to` - The position to seek to, specified as an offset from the beginning.
17    ///
18    /// # Returns
19    /// Returns a `Result` containing the new position on success, or an `Error` if the seek operation fails.
20    fn seek(&mut self, to: usize) -> Result<usize>;
21    
22    /// Gets the current position in the data.
23    ///
24    /// # Returns
25    /// Returns a `Result` containing the current position on success, or an `Error` if the position retrieval fails.
26    fn pos(&mut self) -> Result<usize>;
27
28    /// Gets the total length of the data.
29    ///
30    /// # Returns
31    /// Returns a `Result` containing the length of the data on success, or an `Error` if length retrieval fails.
32    fn len(&self) -> Result<usize>;
33
34    /// Checks if the data is non-empty.
35    ///
36    /// This is a convenience method that checks if the length of the data is greater than 0.
37    ///
38    /// # Returns
39    /// Returns `true` if the length of the data is greater than 0, otherwise `false`.
40    fn is_empty(&self) -> bool {
41        self.len().unwrap_or(0) > 0
42    }
43}
44
45/// A trait for reading data with seeking capabilities.
46///
47/// This trait combines `Read` from the standard library and `PgsSeek`, indicating that a type implementing this trait
48/// can both read data and seek within it.
49pub trait PgsRead: Read + PgsSeek {}