gistools/parsers/read/
mod.rs

1/// Buffer Reader for reading data from a buffer
2mod buffer;
3/// Fetch Reader
4#[cfg(any(feature = "std", target_arch = "wasm32", feature = "wasm"))]
5mod fetch;
6/// File Reader for reading data from a file
7#[cfg(feature = "std")]
8mod file;
9/// Memory Mapped Reader for reading data from a file
10#[cfg(feature = "std")]
11mod mmap;
12
13use alloc::{string::String, vec::Vec};
14pub use buffer::*;
15#[cfg(any(feature = "std", target_arch = "wasm32", feature = "wasm"))]
16pub use fetch::*;
17#[cfg(feature = "std")]
18pub use file::*;
19#[cfg(feature = "std")]
20pub use mmap::*;
21use s2json::VectorFeature;
22#[cfg(feature = "std")]
23use std::{io::Result, path::Path};
24
25/// Expands on the Reader trait to include creating a file
26#[cfg(feature = "std")]
27pub trait StdReader: Reader {
28    /// Creates a new file reader from a file path
29    fn new<P: AsRef<Path>>(path: P) -> Result<Self>
30    where
31        Self: core::marker::Sized;
32}
33
34/// Reader interface. Implemented to read data from either a buffer or a filesystem
35pub trait Reader: Clone {
36    // Properties
37    /// Get the number of bytes in the reader
38    fn len(&self) -> u64;
39    /// See if empty
40    fn is_empty(&self) -> bool {
41        self.len() == 0
42    }
43    // Getters
44    /// Get the unsigned 64 bit integer at the given byte offset and endian. Default to big-endian
45    fn uint64(&self, byte_offset: Option<u64>, little_endian: Option<bool>) -> u64;
46    /// Get the big-endian unsigned 64 bit integer at the given byte offset
47    fn uint64_be(&self, byte_offset: Option<u64>) -> u64;
48    /// Get the little-endian unsigned 64 bit integer at the given byte offset
49    fn uint64_le(&self, byte_offset: Option<u64>) -> u64;
50    /// Get the signed 64 bit integer at the given byte offset and endian. Default to big-endian
51    fn int64(&self, byte_offset: Option<u64>, little_endian: Option<bool>) -> i64;
52    /// Get the big-endian signed 64 bit integer at the given byte offset
53    fn int64_be(&self, byte_offset: Option<u64>) -> i64;
54    /// Get the little-endian signed 64 bit integer at the given byte offset
55    fn int64_le(&self, byte_offset: Option<u64>) -> i64;
56    /// Get the 64 bit floating point at the given byte offset and endian. Default to big-endian
57    fn f64(&self, byte_offset: Option<u64>, little_endian: Option<bool>) -> f64;
58    /// Get the big-endian floating point 64 bit integer at the given byte offset
59    fn f64_be(&self, byte_offset: Option<u64>) -> f64;
60    /// Get the little-endian floating point 64 bit integer at the given byte offset
61    fn f64_le(&self, byte_offset: Option<u64>) -> f64;
62    /// Get the unsigned 32 bit integer at the given byte offset and endian. Default to big-endian
63    fn uint32(&self, byte_offset: Option<u64>, little_endian: Option<bool>) -> u32;
64    /// Get the big-endian unsigned 32 bit integer at the given byte offset
65    fn uint32_be(&self, byte_offset: Option<u64>) -> u32;
66    /// Get the little-endian unsigned 32 bit integer at the given byte offset
67    fn uint32_le(&self, byte_offset: Option<u64>) -> u32;
68    /// Get the signed 32 bit integer at the given byte offset and endian. Default to big-endian
69    fn int32(&self, byte_offset: Option<u64>, little_endian: Option<bool>) -> i32;
70    /// Get the big-endian signed 32 bit integer at the given byte offset
71    fn int32_be(&self, byte_offset: Option<u64>) -> i32;
72    /// Get the little-endian signed 32 bit integer at the given byte offset
73    fn int32_le(&self, byte_offset: Option<u64>) -> i32;
74    /// Get the 32 bit floating point at the given byte offset and endian. Default to big-endian
75    fn f32(&self, byte_offset: Option<u64>, little_endian: Option<bool>) -> f32;
76    /// Get the big-endian floating point 32 bit integer at the given byte offset
77    fn f32_be(&self, byte_offset: Option<u64>) -> f32;
78    /// Get the little-endian floating point 32 bit integer at the given byte offset
79    fn f32_le(&self, byte_offset: Option<u64>) -> f32;
80    /// Get the unsigned 16 bit integer at the given byte offset and endian. Default to big-endian
81    fn uint16(&self, byte_offset: Option<u64>, little_endian: Option<bool>) -> u16;
82    /// Get the big-endian unsigned 16 bit integer at the given byte offset
83    fn uint16_be(&self, byte_offset: Option<u64>) -> u16;
84    /// Get the little-endian unsigned 16 bit integer at the given byte offset
85    fn uint16_le(&self, byte_offset: Option<u64>) -> u16;
86    /// Get the signed 16 bit integer at the given byte offset and endian. Default to big-endian
87    fn int16(&self, byte_offset: Option<u64>, little_endian: Option<bool>) -> i16;
88    /// Get the big-endian signed 16 bit integer at the given byte offset
89    fn int16_be(&self, byte_offset: Option<u64>) -> i16;
90    /// Get the little-endian signed 16 bit integer at the given byte offset
91    fn int16_le(&self, byte_offset: Option<u64>) -> i16;
92    /// Get the 16 bit floating point at the given byte offset and endian. Default to big-endian
93    fn f16(&self, byte_offset: Option<u64>, little_endian: Option<bool>) -> f32;
94    /// Get the big-endian floating point 16 bit integer at the given byte offset
95    fn f16_be(&self, byte_offset: Option<u64>) -> f32;
96    /// Get the little-endian floating point 16 bit integer at the given byte offset
97    fn f16_le(&self, byte_offset: Option<u64>) -> f32;
98    /// Get the unsigned 8 bit integer at the given byte offset
99    fn uint8(&self, byte_offset: Option<u64>) -> u8;
100    /// Get the signed 8 bit integer at the given byte offset
101    fn int8(&self, byte_offset: Option<u64>) -> i8;
102    // Methods
103    /// Seek to the given byte offset
104    fn tell(&self) -> u64;
105    /// Seek to the given byte offset
106    fn seek(&self, pos: u64);
107    /// Get a slice of the reader
108    fn slice(&self, begin: Option<u64>, end: Option<u64>) -> Vec<u8>;
109    /// Get a slice of the reader at the current position
110    fn seek_slice(&self, size: usize) -> Vec<u8>;
111    /// Parse a string from the reader
112    fn parse_string(&self, byte_offset: Option<u64>, byte_length: Option<u64>) -> String;
113    /// Fetch based mechanic. Defaults to slice mechanic
114    fn get_slice<'a>(
115        &'a self,
116        byte_offset: u64,
117        byte_length: Option<u64>,
118    ) -> impl Future<Output = Vec<u8>> + 'a {
119        async move { self.slice(Some(byte_offset), byte_length.map(|l| l + byte_offset)) }
120    }
121}
122
123/// A feature reader that all readers should implement
124pub trait FeatureReader<M: Clone, P: Clone + Default, D: Clone + Default> {
125    /// The Feature Reader should implement an iterator of some kind
126    type FeatureIterator<'a>: Iterator<Item = VectorFeature<M, P, D>>
127    where
128        Self: 'a;
129    /// All readers have an iter function that returns a Iterator struct
130    fn iter(&self) -> Self::FeatureIterator<'_>;
131    /// All readers have a par_iter function that returns a ParallelIterator struct
132    fn par_iter(&self, pool_size: usize, thread_id: usize) -> Self::FeatureIterator<'_>;
133}