Add Bencode Deserialization

This commit is contained in:
Zykino 2022-12-04 14:24:05 +01:00
parent 91d5fc666f
commit 971a19a6e0
5 changed files with 182 additions and 3 deletions

133
Cargo.lock generated
View File

@ -2,6 +2,21 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "addr2line"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -19,12 +34,38 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "backtrace"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.13.0" version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "bendy"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8133e404c8bec821e531f347dab1247bf64f60882826e7228f8ffeb33a35a658"
dependencies = [
"failure",
"serde",
"serde_bytes",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -46,6 +87,12 @@ version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "cc"
version = "1.0.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "581f5dba903aac52ea3feb5ec4810848460ee833876f1f9b0fdeab1f19091574"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -120,6 +167,28 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "failure"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
dependencies = [
"backtrace",
"failure_derive",
]
[[package]]
name = "failure_derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]] [[package]]
name = "generic-array" name = "generic-array"
version = "0.14.6" version = "0.14.6"
@ -130,6 +199,12 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "gimli"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.3" version = "0.12.3"
@ -190,6 +265,21 @@ 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 = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "miniz_oxide"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34"
dependencies = [
"adler",
]
[[package]] [[package]]
name = "num-bigint" name = "num-bigint"
version = "0.4.3" version = "0.4.3"
@ -220,6 +310,15 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "object"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21158b2c33aa6d4561f1c0a6ea283ca92bc54802a93b263e910746d679a7eb53"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.14.0" version = "1.14.0"
@ -374,6 +473,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.11" version = "1.0.11"
@ -411,6 +516,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_bytes"
version = "0.11.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfc50e8183eeeb6178dcb167ae34a8051d63535023ae38b5d8d12beae193d37b"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.144" version = "1.0.144"
@ -491,6 +605,18 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "synstructure"
version = "0.12.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
dependencies = [
"proc-macro2",
"quote",
"syn",
"unicode-xid",
]
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.1.3" version = "1.1.3"
@ -539,6 +665,7 @@ dependencies = [
name = "tradaf" name = "tradaf"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bendy",
"byteorder", "byteorder",
"clap", "clap",
"json5", "json5",
@ -571,6 +698,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
[[package]]
name = "unicode-xid"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]] [[package]]
name = "unsafe-libyaml" name = "unsafe-libyaml"
version = "0.2.2" version = "0.2.2"

View File

@ -24,11 +24,14 @@ serde-transcode = "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
bendy = { version = "0.3", features = ["std", "serde"] }
#bson = "2.3" #bson = "2.3"
#ciborium = "0.2" #ciborium = "0.2"
#serde_dynamo = "4.0" # `Serializer` and `Deserializer` exposed does not provieded required API.
#envy = "0.4" # No `Deserializer` exposed. #envy = "0.4" # No `Deserializer` exposed.
#envy-store = "0.1" # No `Deserializer` exposed. #envy-store = "0.1" # No `Deserializer` exposed.
#flexbuffers = "2.0" # `Serializer` and `Deserializer` exposed does not provieded required API. #flexbuffers = "2.0" # `Serializer` and `Deserializer` exposed does not provieded required API.
#deser-hjson = "1.0" # No `Deserializer` exposed.
serde_json = "1.0" serde_json = "1.0"
json5 = "0.4" json5 = "0.4"
#serde-lexpr = "0.1" #serde-lexpr = "0.1"

View File

@ -25,14 +25,19 @@ tradaf RON json < examples/gameConfig.ron | jq .key_bindings | tradaf Json yaml
# TODOS # TODOS
* [ ] Implement all [data formats](https://serde.rs/#data-formats), or at least the one that expose a serde serializer or deserializer. * [ ] Implement all [data formats](https://serde.rs/#data-formats), or at least the one that expose a serde serializer or deserializer.
* [-] Bencode
* [X] Deserialization
* [ ] Serialization
* [ ] BSON * [ ] BSON
* [ ] CBOR * [ ] CBOR
* [-] DBus (not sure it is working, but it compile…) * [-] DBus (not sure it is working, but it compile…)
* [ ] DynamoDB
* [ ] Envy (deserialization only) * [ ] Envy (deserialization only)
* [ ] Envy store (deserialization only) * [ ] Envy store (deserialization only)
* [ ] FlexBuffer * [ ] FlexBuffer
* The `Serializer` and `Deserializer` exposed does not work with serde-transcode. * The `Serializer` and `Deserializer` exposed does not work with serde-transcode.
* [-] GVariant => See DBus: the same crate propose both * [-] GVariant => See DBus: the same crate propose both
* [ ] HJSON
* [X] JSON * [X] JSON
* [-] JSON5 * [-] JSON5
* [X] Deserialization * [X] Deserialization

View File

@ -31,9 +31,11 @@ pub struct Opt {
#[derive(Copy, Clone, Debug, ValueEnum)] #[derive(Copy, Clone, Debug, ValueEnum)]
pub enum Input { pub enum Input {
Bencode,
//Bson, //Bson,
//Cbor, //Cbor,
DBus, DBus,
//DynamoDB,
//FlexBuffer, //FlexBuffer,
GVariant, GVariant,
Json, Json,
@ -49,9 +51,11 @@ pub enum Input {
#[derive(Copy, Clone, Debug, ValueEnum)] #[derive(Copy, Clone, Debug, ValueEnum)]
pub enum Output { pub enum Output {
//Bencode,
//Bson, //Bson,
//Cbor, //Cbor,
DBus, DBus,
//DynamoDB,
//FlexBuffer, //FlexBuffer,
GVariant, GVariant,
Json, Json,
@ -66,8 +70,6 @@ pub enum Output {
} }
pub fn transcode(opt: Opt, input: &mut dyn Read, output: &mut dyn Write) { pub fn transcode(opt: Opt, input: &mut dyn Read, output: &mut dyn Write) {
// A JSON deserializer. You can use any Serde Deserializer here.
de(&opt, input, output); de(&opt, input, output);
// TODO: I would love to be able to have `de` and `ser` return the correct // TODO: I would love to be able to have `de` and `ser` return the correct
@ -84,6 +86,14 @@ pub fn transcode(opt: Opt, input: &mut dyn Read, output: &mut dyn Write) {
fn de(opt: &Opt, input: &mut dyn Read, output: &mut dyn Write) { fn de(opt: &Opt, input: &mut dyn Read, output: &mut dyn Write) {
match opt.input { match opt.input {
Input::Bencode => {
use bendy::serde::Deserializer;
let mut buf = vec![];
let _buf_size = input.read_to_end(&mut buf).unwrap();
let mut deserializer = Deserializer::from_bytes(&buf);
ser(opt, &mut deserializer, output);
}
/* /*
Input::Bson => { Input::Bson => {
use bson::Deserializer; use bson::Deserializer;
@ -111,6 +121,14 @@ fn de(opt: &Opt, input: &mut dyn Read, output: &mut dyn Write) {
ser(opt, &mut deserializer, output); ser(opt, &mut deserializer, output);
} }
/* /*
Input::DynamoDB => {
use serde_dynamo::Deserializer;
let deserializer = Deserializer::from(input); // from_reader(input);
ser(opt, deserializer, output);
}
*/
/*
Input::FlexBuffer => { Input::FlexBuffer => {
use serde_yaml::Deserializer; use serde_yaml::Deserializer;
@ -192,6 +210,16 @@ where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
match opt.output { match opt.output {
/*
Output::Bencode => {
use bendy::serde::Serializer;
let serializer = &mut Serializer::new(/*output*/);
serde_transcode::transcode(deserializer, serializer).unwrap();
serializer.into_bytes();
}
*/
/* /*
Output::Bson => { Output::Bson => {
use bson::Serializer; use bson::Serializer;
@ -230,6 +258,15 @@ where
serde_transcode::transcode(deserializer, serializer).unwrap(); serde_transcode::transcode(deserializer, serializer).unwrap();
} }
/* /*
Output::DynamoDB => {
use serde_dynamo::Serializer;
let serializer = &mut Serializer::new(output);
serde_transcode::transcode(deserializer, serializer).unwrap();
}
*/
/*
Output::FlexBuffer => { Output::FlexBuffer => {
use flexbuffers::FlexbufferSerializer as Serializer; use flexbuffers::FlexbufferSerializer as Serializer;
@ -261,7 +298,7 @@ where
// let _buf_size = output.read_to_string(&mut buf).unwrap(); // let _buf_size = output.read_to_string(&mut buf).unwrap();
// let mut deserializer = Deserializer::new(); // let mut deserializer = Deserializer::new();
serde_transcode::transcode(deserializer, serializer).unwrap(); serde_transcode::transcode(deserializer, serializer).unwrap();
} }
Output::Json => { Output::Json => {
use serde_json::Serializer; use serde_json::Serializer;

1
test-data/list.bencode Normal file
View File

@ -0,0 +1 @@
l4:spami42ee