pub struct DataParser {
pub serialization_type: SerializationType,
/* private fields */
}
Expand description
DataParser
is a utility struct for encoding (serializing and optionally compressing)
and decoding (deserializing and optionally decompressing) data. It is useful for
optimizing memory usage and I/O bandwidth by applying different
serialization formats and optional compression strategies.
§Fields
compression_strategy
- AnOption<Arc<dyn CompressionStrategy>>
that defines the method of data compression. IfNone
, no compression is applied.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: SerializationType
Implementations§
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 anArc
of aCompressionStrategy
trait 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
- ASerializationType
enum 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 optionally compressing it.
§Arguments
data
- A reference to a data structure implementing theDataParseable
trait.
§Returns
A Result
containing either a Vec<u8>
of the serialized (and optionally compressed) 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 deserializing and optionally decompressing it.
§Arguments
data
- A byte slice (&[u8]
) representing the serialized (and optionally compressed) 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 more