From 971a19a6e029ecdef90542fda90c6ca21de458f8 Mon Sep 17 00:00:00 2001 From: Zykino Date: Sun, 4 Dec 2022 14:24:05 +0100 Subject: [PATCH] Add Bencode Deserialization --- Cargo.lock | 133 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 + README.md | 5 ++ src/lib.rs | 43 ++++++++++++- test-data/list.bencode | 1 + 5 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 test-data/list.bencode diff --git a/Cargo.lock b/Cargo.lock index b5c2dd7..0f1c4c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. 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]] name = "atty" version = "0.2.14" @@ -19,12 +34,38 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "base64" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "bitflags" version = "1.3.2" @@ -46,6 +87,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "cc" +version = "1.0.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581f5dba903aac52ea3feb5ec4810848460ee833876f1f9b0fdeab1f19091574" + [[package]] name = "cfg-if" version = "1.0.0" @@ -120,6 +167,28 @@ dependencies = [ "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]] name = "generic-array" version = "0.14.6" @@ -130,6 +199,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "gimli" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d" + [[package]] name = "hashbrown" version = "0.12.3" @@ -190,6 +265,21 @@ version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "num-bigint" version = "0.4.3" @@ -220,6 +310,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "object" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21158b2c33aa6d4561f1c0a6ea283ca92bc54802a93b263e910746d679a7eb53" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.14.0" @@ -374,6 +473,12 @@ dependencies = [ "serde", ] +[[package]] +name = "rustc-demangle" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" + [[package]] name = "ryu" version = "1.0.11" @@ -411,6 +516,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_bytes" +version = "0.11.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfc50e8183eeeb6178dcb167ae34a8051d63535023ae38b5d8d12beae193d37b" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.144" @@ -491,6 +605,18 @@ dependencies = [ "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]] name = "termcolor" version = "1.1.3" @@ -539,6 +665,7 @@ dependencies = [ name = "tradaf" version = "0.1.0" dependencies = [ + "bendy", "byteorder", "clap", "json5", @@ -571,6 +698,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" + [[package]] name = "unsafe-libyaml" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index 606e1fc..93508ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,11 +24,14 @@ serde-transcode = "1.1" # Data types we support # 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" #ciborium = "0.2" +#serde_dynamo = "4.0" # `Serializer` and `Deserializer` exposed does not provieded required API. #envy = "0.4" # No `Deserializer` exposed. #envy-store = "0.1" # No `Deserializer` exposed. #flexbuffers = "2.0" # `Serializer` and `Deserializer` exposed does not provieded required API. +#deser-hjson = "1.0" # No `Deserializer` exposed. serde_json = "1.0" json5 = "0.4" #serde-lexpr = "0.1" diff --git a/README.md b/README.md index 551abf9..ed72738 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,19 @@ tradaf RON json < examples/gameConfig.ron | jq .key_bindings | tradaf Json yaml # TODOS * [ ] 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 * [ ] CBOR * [-] DBus (not sure it is working, but it compileā€¦) + * [ ] DynamoDB * [ ] Envy (deserialization only) * [ ] Envy store (deserialization only) * [ ] FlexBuffer * The `Serializer` and `Deserializer` exposed does not work with serde-transcode. * [-] GVariant => See DBus: the same crate propose both + * [ ] HJSON * [X] JSON * [-] JSON5 * [X] Deserialization diff --git a/src/lib.rs b/src/lib.rs index 069dc32..5f8347a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,9 +31,11 @@ pub struct Opt { #[derive(Copy, Clone, Debug, ValueEnum)] pub enum Input { + Bencode, //Bson, //Cbor, DBus, + //DynamoDB, //FlexBuffer, GVariant, Json, @@ -49,9 +51,11 @@ pub enum Input { #[derive(Copy, Clone, Debug, ValueEnum)] pub enum Output { + //Bencode, //Bson, //Cbor, DBus, + //DynamoDB, //FlexBuffer, GVariant, Json, @@ -66,8 +70,6 @@ pub enum Output { } 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); // 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) { 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 => { use bson::Deserializer; @@ -111,6 +121,14 @@ fn de(opt: &Opt, input: &mut dyn Read, output: &mut dyn Write) { 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 => { use serde_yaml::Deserializer; @@ -192,6 +210,16 @@ where D: Deserializer<'de>, { 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 => { use bson::Serializer; @@ -230,6 +258,15 @@ where 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 => { use flexbuffers::FlexbufferSerializer as Serializer; @@ -261,7 +298,7 @@ where // let _buf_size = output.read_to_string(&mut buf).unwrap(); // let mut deserializer = Deserializer::new(); serde_transcode::transcode(deserializer, serializer).unwrap(); - } + } Output::Json => { use serde_json::Serializer; diff --git a/test-data/list.bencode b/test-data/list.bencode new file mode 100644 index 0000000..f9c4a90 --- /dev/null +++ b/test-data/list.bencode @@ -0,0 +1 @@ +l4:spami42ee