Skip to content

Commit

Permalink
Merge pull request #1 from savannstm/0.3.0
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
savannstm authored Sep 29, 2024
2 parents 9568e45 + db969d9 commit 2829ee0
Show file tree
Hide file tree
Showing 9 changed files with 763 additions and 731 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "marshal-rs"
version = "0.2.4"
version = "0.3.0"
authors = ["savannstm <[email protected]>"]
edition = "2021"
rust-version = "1.63.0"
description = "Blazingly fast Ruby-lang's Marshal implementation in Rust."
readme = "README.md"
repository = "https://github.com/savannstm/marshal-rs"
documentation = "https://docs.rs/marshal-rs/"
license-file = "LICENSE.md"
keywords = ["marshal", "ruby", "serialize", "deserialize"]

Expand All @@ -15,11 +16,10 @@ sonic = ["dep:sonic-rs"]
default = ["dep:serde_json"]

[dependencies]
cfg-if = "1.0.0"
encoding_rs = "0.8.34"
num-bigint = "0.4.6"
serde_json = { version = "1.0.125", optional = true, features = ["preserve_order"] }
sonic-rs = { version = "0.3.11", optional = true }
serde_json = { version = "1.0.128", optional = true, features = ["preserve_order"] }
sonic-rs = { version = "0.3.13", optional = true }

[dev-dependencies]
rayon = "1.10.0"
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
**`marshal-rs` is a Rust implementation of Ruby-lang's `Marshal`.**

This project is essentially just [@savannstm/marshal](https://github.com/savannstm/marshal), rewritten using Rust.
It is capable of :fire: **_BLAZINGLY FAST_** loading dumped from Ruby Marshal files, as well as :fire: **_BLAZINGLY FAST_** dumping them back to Marshal format.
It is capable of :fire: **_BLAZINGLY FAST_** loading data from dumped Ruby Marshal files, as well as :fire: **_BLAZINGLY FAST_** dumping it back to Marshal format.

## Installation

Expand Down Expand Up @@ -68,16 +68,17 @@ However, in current implementation, this unsafe code will NOT ever cause any dat

```rust
use std::fs::read;
use marshal_rs::load::load;
use marshal_rs::dump::dump;
use marshal_rs::{load, dump};

fn main() {
// Read marshal data
let marshal_data: Vec<u8> = read("./marshal_file.marshal").unwrap();
// Read marshal data from file
// let marshal_data: Vec<u8> = read("./Map001.rvdata2").unwrap();
// For this example, we'll just take pre-defined marshal data
let marshal_data: Vec<u8> = [0x04, 0x08, 0x30].to_vec();

// Serializing to json
// load() takes a &[u8] as argument, so bytes Vec must be borrowed
let serialized_to_json: serde_json::Value = load(&marshal_data, None, None);
let serialized_to_json: serde_json::Value = load(&marshal_data, None, None).unwrap();

// Here you may std::fs::write() serialized JSON to file

Expand Down
32 changes: 14 additions & 18 deletions examples/complex.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
use cfg_if::cfg_if;
use marshal_rs::{dump::Dumper, load::Loader};
cfg_if! {
if #[cfg(feature = "sonic")] {
use sonic_rs::json;
} else {
use serde_json::json;
}
}
use marshal_rs::{Dumper, Loader};
#[cfg(not(feature = "sonic"))]
use serde_json::json;
#[cfg(feature = "sonic")]
use sonic_rs::json;

fn main() {
// Bytes slice of Ruby Marshal data
// Files with Marshal data can be read with std::fs::read()
let null: [u8; 3] = [0x04, 0x08, 0x30]; // null
let true_: [u8; 3] = [0x04, 0x08, 0x54]; // true
let false_: [u8; 3] = [0x04, 0x08, 0x46]; // false
let null_bytes: [u8; 3] = [0x04, 0x08, 0x30]; // null
let true_bytes: [u8; 3] = [0x04, 0x08, 0x54]; // true
let false_bytes: [u8; 3] = [0x04, 0x08, 0x46]; // false

// Initialize the loader, might be more efficient to loading multiple files
let mut loader: Loader = Loader::new();

// Load the values of multiple objects
let null_value = loader.load(&null, None, None);
let true_value = loader.load(&true_, None, None);
let false_value = loader.load(&false_, None, None);
let null_value = loader.load(&null_bytes, None, None).unwrap();
let true_value = loader.load(&true_bytes, None, None).unwrap();
let false_value = loader.load(&false_bytes, None, None).unwrap();

assert_eq!(null_value, json!(null));
assert_eq!(true_value, json!(true));
Expand All @@ -37,9 +33,9 @@ fn main() {
let true_marshal: Vec<u8> = dumper.dump(true_value, None);
let false_marshal: Vec<u8> = dumper.dump(false_value, None);

assert_eq!(&null_marshal, &null);
assert_eq!(&true_marshal, &true_);
assert_eq!(&false_marshal, &false_);
assert_eq!(&null_marshal, &null_bytes);
assert_eq!(&true_marshal, &true_bytes);
assert_eq!(&false_marshal, &false_bytes);

// Here you may write bytes back to the Marshal file
}
22 changes: 9 additions & 13 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
use cfg_if::cfg_if;
use marshal_rs::{dump::dump, load::load};
cfg_if! {
if #[cfg(feature = "sonic")] {
use sonic_rs::json;
} else {
use serde_json::json;
}
}
use marshal_rs::{dump, load};
#[cfg(not(feature = "sonic"))]
use serde_json::json;
#[cfg(feature = "sonic")]
use sonic_rs::json;

fn main() {
// Bytes slice of Ruby Marshal data
// Files with Marshal data can be read with std::fs::read()
let bytes: [u8; 3] = [0x04, 0x08, 0x30]; // null
let null_bytes: [u8; 3] = [0x04, 0x08, 0x30]; // null

// Serialize bytes to a Value
// If "sonic" feature is enabled, returns sonic_rs::Value, otherwise serde_json::Value
let json = load(&bytes, None, None);
let json = load(&null_bytes, None, None).unwrap();
assert_eq!(json, json!(null));

// Here you may write the json object to file using std::fs::write()

// Deserialize object back to bytes
let marshal: Vec<u8> = dump(json, None);
assert_eq!(&marshal, &bytes);
let marshal_bytes: Vec<u8> = dump(json, None);
assert_eq!(&marshal_bytes, &null_bytes);

// Here you may write bytes back to the Marshal file
}
Loading

0 comments on commit 2829ee0

Please sign in to comment.