fuel_data_parser

Struct DataParser

Source
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 - An Option<Arc<dyn CompressionStrategy>> that defines the method of data compression. If None, 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

Source

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 an Arc of a CompressionStrategy 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);
Source

pub fn with_serialization_type( self, serialization_type: SerializationType, ) -> Self

Sets the serialization type for the DataParser.

§Arguments
  • serialization_type - A SerializationType 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);
Source

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 the DataParseable 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(())
}
Source

pub async fn serialize<T: DataParseable>( &self, raw_data: &T, ) -> Result<Vec<u8>, Error>

Serializes the provided data according to the selected SerializationType.

§Arguments
  • raw_data - A reference to a data structure implementing the DataParseable trait.
§Returns

A Result containing either a Vec<u8> of the serialized data, or an Error if serialization fails.

Source

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(())
}
Source

pub async fn deserialize<'a, T: Deserialize<'a>>( &self, raw_data: &'a [u8], ) -> Result<T, Error>

Deserializes the provided data according to the selected SerializationType.

§Arguments
  • raw_data - A byte slice (&[u8]) representing the serialized data.
§Returns

A Result containing either the deserialized data structure, or an Error if deserialization fails.

Trait Implementations§

Source§

impl Clone for DataParser

Source§

fn clone(&self) -> DataParser

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for DataParser

Source§

fn default() -> Self

Provides a default instance of DataParser with no compression strategy and SerializationType::Json.

§Examples
use fuel_data_parser::{DataParser, SerializationType};

let parser = DataParser::default();
assert!(matches!(parser.serialization_type, SerializationType::Json));

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.