Struct fuel_data_parser::DataParser
source · pub struct DataParser {
pub serialization_type: SerializationType,
/* private fields */
}Expand description
DataParser is a utility struct for encoding (compressing & serializing)
and decoding (decompressing & deserializing) data. It is useful for
optimizing memory usage and I/O bandwidth by applying different
compression strategies and serialization formats.
§Fields
compression_strategy- AnArcto aCompressionStrategytrait object that defines the method of data compression.serialization_type- An enum that specifies the serialization format (e.g., Bincode, Postcard, JSON).
§Examples
use fuel_data_parser::{DataParser, SerializationType};
use std::sync::Arc;
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
struct TestData {
field: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let parser = DataParser::default();
let original_data = TestData { field: "test".to_string() };
let encoded = parser.encode(&original_data).await?;
let decoded: TestData = parser.decode(&encoded).await?;
assert_eq!(original_data, decoded);
Ok(())
}Fields§
§serialization_type: SerializationTypeImplementations§
source§impl DataParser
impl DataParser
sourcepub fn with_compression_strategy(
self,
compression_strategy: &Arc<dyn CompressionStrategy>,
) -> Self
pub fn with_compression_strategy( self, compression_strategy: &Arc<dyn CompressionStrategy>, ) -> Self
Sets the compression strategy for the DataParser.
§Arguments
compression_strategy- A reference to anArcof aCompressionStrategytrait object.
§Returns
A new instance of DataParser with the updated compression strategy.
§Examples
use fuel_data_parser::{DataParser, DEFAULT_COMPRESSION_STRATEGY};
use std::sync::Arc;
let parser = DataParser::default()
.with_compression_strategy(&DEFAULT_COMPRESSION_STRATEGY);sourcepub fn with_serialization_type(
self,
serialization_type: SerializationType,
) -> Self
pub fn with_serialization_type( self, serialization_type: SerializationType, ) -> Self
Sets the serialization type for the DataParser.
§Arguments
serialization_type- ASerializationTypeenum specifying the desired serialization format.
§Returns
A new instance of DataParser with the updated serialization type.
§Examples
use fuel_data_parser::{DataParser, SerializationType};
let parser = DataParser::default()
.with_serialization_type(SerializationType::Json);sourcepub async fn encode<T: DataParseable>(&self, data: &T) -> Result<Vec<u8>, Error>
pub async fn encode<T: DataParseable>(&self, data: &T) -> Result<Vec<u8>, Error>
Encodes the provided data by serializing and then compressing it.
§Arguments
data- A reference to a data structure implementing theDataParseabletrait.
§Returns
A Result containing either a Vec<u8> of the compressed, serialized data,
or an Error if encoding fails.
§Examples
use fuel_data_parser::DataParser;
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
struct TestData {
field: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let parser = DataParser::default();
let data = TestData { field: "test".to_string() };
let encoded = parser.encode(&data).await?;
assert!(!encoded.is_empty());
Ok(())
}sourcepub async fn decode<T: DataParseable>(&self, data: &[u8]) -> Result<T, Error>
pub async fn decode<T: DataParseable>(&self, data: &[u8]) -> Result<T, Error>
Decodes the provided data by decompressing and then deserializing it.
§Arguments
data- A byte slice (&[u8]) representing the compressed, serialized data.
§Returns
A Result containing either the deserialized data structure,
or an Error if decoding fails.
§Examples
use fuel_data_parser::DataParser;
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
struct TestData {
field: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let parser = DataParser::default();
let original_data = TestData { field: "test".to_string() };
let encoded = parser.encode(&original_data).await?;
let decoded: TestData = parser.decode(&encoded).await?;
assert_eq!(original_data, decoded);
Ok(())
}sourcepub async fn deserialize<'a, T: Deserialize<'a>>(
&self,
raw_data: &'a [u8],
) -> Result<T, Error>
pub async fn deserialize<'a, T: Deserialize<'a>>( &self, raw_data: &'a [u8], ) -> Result<T, Error>
Trait Implementations§
source§impl Clone for DataParser
impl Clone for DataParser
source§fn clone(&self) -> DataParser
fn clone(&self) -> DataParser
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Default for DataParser
impl Default for DataParser
source§fn default() -> Self
fn default() -> Self
Provides a default instance of DataParser with default compression strategy
and SerializationType::Postcard.
§Examples
use fuel_data_parser::{DataParser, SerializationType};
let parser = DataParser::default();
assert!(matches!(parser.serialization_type, SerializationType::Postcard));Auto Trait Implementations§
impl Freeze for DataParser
impl !RefUnwindSafe for DataParser
impl Send for DataParser
impl Sync for DataParser
impl Unpin for DataParser
impl !UnwindSafe for DataParser
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)