Add support for Pickle a Python serialization format

This commit is contained in:
Zykino 2022-09-18 12:39:10 +02:00
parent af86c50311
commit 2cdcea5391
3 changed files with 75 additions and 1 deletions

56
Cargo.lock generated
View File

@ -31,6 +31,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]] [[package]]
name = "clap" name = "clap"
version = "3.2.20" version = "3.2.20"
@ -101,6 +107,12 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "iter-read"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c397ca3ea05ad509c4ec451fea28b4771236a376ca1c69fd5143aae0cf8f93c4"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.3" version = "1.0.3"
@ -113,6 +125,36 @@ version = "0.2.132"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5"
[[package]]
name = "num-bigint"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.14.0" version = "1.14.0"
@ -193,6 +235,19 @@ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]]
name = "serde-pickle"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c762ad136a26407c6a80825813600ceeab5e613660d93d79a41f0ec877171e71"
dependencies = [
"byteorder",
"iter-read",
"num-bigint",
"num-traits",
"serde",
]
[[package]] [[package]]
name = "serde-transcode" name = "serde-transcode"
version = "1.1.1" version = "1.1.1"
@ -276,6 +331,7 @@ dependencies = [
"clap", "clap",
"ron", "ron",
"serde", "serde",
"serde-pickle",
"serde-transcode", "serde-transcode",
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",

View File

@ -24,6 +24,9 @@ serde-transcode = "1.1.1"
# Data types we support # Data types we support
# Should tend to be equal to the official list of data formats supported by serde: https://serde.rs/#data-formats # Should tend to be equal to the official list of data formats supported by serde: https://serde.rs/#data-formats
ron = "0.8" #ciborium = "0.2"
serde_json = "1.0" serde_json = "1.0"
serde-pickle = "1.0"
ron = "0.8"
#toml = "0.5"
serde_yaml = "0.9" serde_yaml = "0.9"

View File

@ -24,6 +24,7 @@ pub struct Opt {
#[derive(Copy, Clone, Debug, ValueEnum)] #[derive(Copy, Clone, Debug, ValueEnum)]
pub enum Input { pub enum Input {
Json, Json,
Pickle,
Ron, Ron,
Yaml, Yaml,
} }
@ -31,6 +32,7 @@ pub enum Input {
#[derive(Copy, Clone, Debug, ValueEnum)] #[derive(Copy, Clone, Debug, ValueEnum)]
pub enum Output { pub enum Output {
Json, Json,
Pickle,
Ron, Ron,
Yaml, Yaml,
} }
@ -61,6 +63,12 @@ fn de(opt: &Opt, input: &mut dyn Read, output: &mut dyn Write) {
let mut deserializer = Deserializer::from_reader(input); let mut deserializer = Deserializer::from_reader(input);
ser(opt, &mut deserializer, output); ser(opt, &mut deserializer, output);
} }
Input::Pickle => {
use serde_pickle::Deserializer;
let mut deserializer = Deserializer::new(input, serde_pickle::DeOptions::new());
ser(opt, &mut deserializer, output);
}
Input::Ron => { Input::Ron => {
use ron::Deserializer; use ron::Deserializer;
@ -102,6 +110,13 @@ where
// NOTE: serde_jsons PrettyFormatter and CompactFormatter are incompatibles… // NOTE: serde_jsons PrettyFormatter and CompactFormatter are incompatibles…
// serde_transcode::transcode(deserializer, serializer).unwrap(); // serde_transcode::transcode(deserializer, serializer).unwrap();
} }
Output::Pickle => {
use serde_pickle::Serializer;
let serializer = &mut Serializer::new(output, serde_pickle::SerOptions::new());
serde_transcode::transcode(deserializer, serializer).unwrap();
}
Output::Ron => { Output::Ron => {
use ron::Serializer; use ron::Serializer;