fuel_data_parser/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
#![doc = include_str!("../README.md")]
mod compression_strategies;
mod error;
use std::{fmt::Debug, sync::Arc};
pub use compression_strategies::*;
#[cfg(feature = "json")]
use serde::de::DeserializeOwned;
pub use crate::error::{CompressionError, DataParserError, SerdeError};
/// Serialization types supported for data parsing
#[derive(Debug, Clone, strum::EnumIter, strum_macros::Display)]
pub enum SerializationType {
/// Bincode serialization
#[cfg(feature = "bincode")]
#[strum(serialize = "bincode")]
Bincode,
/// json serialization
#[cfg(feature = "json")]
#[strum(serialize = "json")]
Json,
/// Postcard serialization
#[cfg(feature = "postcard")]
#[strum(serialize = "postcard")]
Postcard,
}
#[async_trait::async_trait]
pub trait DataEncoder:
serde::Serialize
+ serde::de::DeserializeOwned
+ Clone
+ Send
+ Sync
+ Debug
+ std::marker::Sized
{
type Err: std::error::Error + From<DataParserError>;
fn data_parser() -> DataParser {
DataParser::default()
}
async fn encode(&self) -> Result<Vec<u8>, Self::Err> {
Self::data_parser().encode(self).await.map_err(Into::into)
}
#[cfg(feature = "json")]
fn encode_json(&self) -> Result<Vec<u8>, Self::Err> {
Self::data_parser().encode_json(self).map_err(Into::into)
}
async fn decode(encoded: &[u8]) -> Result<Self, Self::Err> {
Self::data_parser()
.decode(encoded)
.await
.map_err(Into::into)
}
#[cfg(feature = "json")]
fn decode_json(encoded: &[u8]) -> Result<Self, Self::Err> {
Self::data_parser().decode_json(encoded).map_err(Into::into)
}
#[cfg(feature = "json")]
fn to_json_value(&self) -> Result<serde_json::Value, Self::Err> {
Self::data_parser().to_json_value(self).map_err(Into::into)
}
}
/// `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::*;
/// use std::sync::Arc;
///
/// #[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
/// struct TestData {
/// field: String,
/// }
///
/// impl DataEncoder for TestData {
/// type Err = DataParserError;
/// }
///
/// #[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(())
/// }
/// ```
#[derive(Clone)]
pub struct DataParser {
compression_strategy: Option<Arc<dyn CompressionStrategy>>,
pub serialization_type: SerializationType,
}
impl Default for DataParser {
/// 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));
/// ```
fn default() -> Self {
Self {
compression_strategy: None,
serialization_type: SerializationType::Json,
}
}
}
impl DataParser {
/// 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::*;
/// use std::sync::Arc;
///
/// let parser = DataParser::default()
/// .with_compression_strategy(&DEFAULT_COMPRESSION_STRATEGY);
/// ```
pub fn with_compression_strategy(
mut self,
compression_strategy: &Arc<dyn CompressionStrategy>,
) -> Self {
self.compression_strategy = Some(compression_strategy.clone());
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::*;
///
/// let parser = DataParser::default()
/// .with_serialization_type(SerializationType::Postcard);
/// ```
pub fn with_serialization_type(
mut self,
serialization_type: SerializationType,
) -> Self {
self.serialization_type = serialization_type;
self
}
/// 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::*;
///
/// #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
/// struct TestData {
/// field: String,
/// }
///
/// impl DataEncoder for TestData {
/// type Err = DataParserError;
/// }
///
/// #[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(())
/// }
/// ```
pub async fn encode<T: DataEncoder>(
&self,
data: &T,
) -> Result<Vec<u8>, DataParserError> {
let serialized_data = self.serialize(data).await?;
Ok(match &self.compression_strategy {
Some(strategy) => strategy.compress(&serialized_data[..]).await?,
None => serialized_data,
})
}
#[cfg(feature = "json")]
pub fn encode_json<T: DataEncoder>(
&self,
data: &T,
) -> Result<Vec<u8>, DataParserError> {
self.serialize_json(data)
}
#[cfg(feature = "json")]
pub fn to_json_value<T: serde::Serialize>(
&self,
data: &T,
) -> Result<serde_json::Value, DataParserError> {
self.serialize_json_value(data)
}
/// 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.
pub async fn serialize<T: DataEncoder>(
&self,
raw_data: &T,
) -> Result<Vec<u8>, DataParserError> {
match self.serialization_type {
#[cfg(feature = "bincode")]
SerializationType::Bincode => bincode::serialize(&raw_data)
.map_err(|e| DataParserError::Encode(SerdeError::Bincode(*e))),
#[cfg(feature = "json")]
SerializationType::Json => serde_json::to_vec(&raw_data)
.map_err(|e| DataParserError::EncodeJson(SerdeError::Json(e))),
#[cfg(feature = "postcard")]
SerializationType::Postcard => postcard::to_allocvec(&raw_data)
.map_err(|e| DataParserError::Encode(SerdeError::Postcard(e))),
}
}
#[cfg(feature = "json")]
fn serialize_json<T: DataEncoder>(
&self,
raw_data: &T,
) -> Result<Vec<u8>, DataParserError> {
serde_json::to_vec(&raw_data)
.map_err(|e| DataParserError::EncodeJson(SerdeError::Json(e)))
}
#[cfg(feature = "json")]
fn serialize_json_value<T: serde::Serialize>(
&self,
raw_data: &T,
) -> Result<serde_json::Value, DataParserError> {
serde_json::to_value(raw_data)
.map_err(|e| DataParserError::EncodeJson(SerdeError::Json(e)))
}
/// 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::*;
///
/// #[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
/// struct TestData {
/// field: String,
/// }
///
/// impl DataEncoder for TestData {
/// type Err = DataParserError;
/// }
///
/// #[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(())
/// }
/// ```
pub async fn decode<T: DataEncoder>(
&self,
data: &[u8],
) -> Result<T, DataParserError> {
let data = match &self.compression_strategy {
Some(strategy) => strategy.decompress(data).await?,
None => data.to_vec(),
};
let decoded_data = self.deserialize(&data[..])?;
Ok(decoded_data)
}
#[cfg(feature = "json")]
pub fn decode_json<T: DataEncoder>(
&self,
data: &[u8],
) -> Result<T, DataParserError> {
self.deserialize_json(data)
}
/// 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.
pub fn deserialize<T: DeserializeOwned>(
&self,
raw_data: &[u8],
) -> Result<T, DataParserError> {
match self.serialization_type {
#[cfg(feature = "bincode")]
SerializationType::Bincode => bincode::deserialize(raw_data)
.map_err(|e| DataParserError::Decode(SerdeError::Bincode(*e))),
#[cfg(feature = "json")]
SerializationType::Json => self.deserialize_json(raw_data),
#[cfg(feature = "postcard")]
SerializationType::Postcard => postcard::from_bytes(raw_data)
.map_err(|e| DataParserError::Decode(SerdeError::Postcard(e))),
}
}
#[cfg(feature = "json")]
fn deserialize_json<T: DeserializeOwned>(
&self,
raw_data: &[u8],
) -> Result<T, DataParserError> {
serde_json::from_slice(raw_data)
.map_err(|e| DataParserError::DecodeJson(SerdeError::Json(e)))
}
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::*;
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
struct TestData {
field: String,
}
impl DataEncoder for TestData {
type Err = DataParserError;
}
#[tokio::test]
async fn test_encode_decode() {
let parser = DataParser::default();
let original_data = TestData {
field: "test".to_string(),
};
let encoded = parser.encode(&original_data).await.unwrap();
let decoded: TestData = parser.decode(&encoded).await.unwrap();
assert_eq!(original_data, decoded);
}
#[tokio::test]
async fn test_serialization_types() {
let data = TestData {
field: "test".to_string(),
};
for serialization_type in [
#[cfg(feature = "bincode")]
SerializationType::Bincode,
#[cfg(feature = "postcard")]
SerializationType::Postcard,
#[cfg(feature = "json")]
SerializationType::Json,
] {
let parser = DataParser::default()
.with_serialization_type(serialization_type);
let encoded = parser.encode(&data).await.unwrap();
let decoded: TestData = parser.decode(&encoded).await.unwrap();
assert_eq!(data, decoded);
}
}
#[tokio::test]
async fn test_compression_strategies() {
let data = TestData {
field: "test".to_string(),
};
for strategy in ALL_COMPRESSION_STRATEGIES.iter() {
let parser =
DataParser::default().with_compression_strategy(strategy);
let encoded = parser.encode(&data).await.unwrap();
let decoded: TestData = parser.decode(&encoded).await.unwrap();
assert_eq!(data, decoded);
}
}
}