From c4becb0b123769e51719b2a726d270944b1cc266 Mon Sep 17 00:00:00 2001 From: Zykino Date: Sat, 17 Sep 2022 20:29:35 +0200 Subject: [PATCH] POC: Inital commit (transcode json, ron, yaml) --- .gitignore | 1 + Cargo.lock | 331 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 29 ++++ README.md | 24 +++ examples/gameConfig.ron | 26 ++++ examples/simple.json | 4 + examples/standard.ron | 20 +++ src/lib.rs | 113 ++++++++++++++ src/main.rs | 12 ++ 9 files changed, 560 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 examples/gameConfig.ron create mode 100644 examples/simple.json create mode 100644 examples/standard.ron create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..14c7909 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,331 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "clap" +version = "3.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b71c3ce99b7611011217b366d923f1d0a7e07a92bb2dbf1e84508c673ca3bd" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" + +[[package]] +name = "libc" +version = "0.2.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" + +[[package]] +name = "once_cell" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0" + +[[package]] +name = "os_str_bytes" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ron" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300a51053b1cb55c80b7a9fde4120726ddf25ca241a1cbb926626f62fb136bff" +dependencies = [ + "base64", + "bitflags", + "serde", +] + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde-transcode" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "590c0e25c2a5bb6e85bf5c1bce768ceb86b316e7a01bdf07d2cb4ec2271990e2" +dependencies = [ + "serde", +] + +[[package]] +name = "serde_derive" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_yaml" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89f31df3f50926cdf2855da5fd8812295c34752cb20438dae42a67f79e021ac3" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" + +[[package]] +name = "tradaf" +version = "0.1.0" +dependencies = [ + "clap", + "ron", + "serde", + "serde-transcode", + "serde_json", + "serde_yaml", +] + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" + +[[package]] +name = "unsafe-libyaml" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "931179334a56395bcf64ba5e0ff56781381c1a5832178280c7d7f91d1679aeb0" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6c189f0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "tradaf" +version = "0.1.0" +authors = ["Zykino "] +description = "Tranduit Data Format: Translate serialized data from one format to another." +license = "GPL-3.0-or-later" +keywords = ["conversion", "serialization", "serde"] +categories = ["command-line-utilities"] +readme = "README.md" +repository = "https://zykino.net/gitea/zykino/Tradaf" +documentation = "https://docs.rs/tradaf" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +# TODO: handle errors (or pass them to main) instead of unwrapping +#anyhow = "1.0.64" +clap = { version = "3.2.20", features = ["derive"] } +# TODO: May be used to "deserialize any" if I understood correctly. (Or maybe `typetag`?) But I did not achieved to make it work with serde-transcode +#erased-serde = "0.3.23" +serde = "1.0" +serde-transcode = "1.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 +ron = "0.8" +serde_json = "1.0" +serde_yaml = "0.9" diff --git a/README.md b/README.md new file mode 100644 index 0000000..59b82b3 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Description +This tool "traduit" (french for translate) data formats. It was made in the hope to bring serde to the cli (serdi ^^). But that not exaclty what serde is about and I have no affiliation with them so I did not want to namesquat. + +# Installation +First install [the rust toolchain](https://www.rust-lang.org/tools/install) and then execute the following command: +```sh +cargo install tradaf +``` + +# Usage example +Prettify (or minify) some data format for ease of reading (resp: compact and have less data to save/transfert) +```sh +tradaf json json --pretty < simple.json +``` + +Litterally transcode between data formats +```sh +tradaf ron yaml < examples/standard.ron +``` + +Transcode into JSON so we can use `jq`, do some transformations and then transcode again into another format for viewing +```sh +tradaf RON json < examples/gameConfig.ron | jq .key_bindings | tradaf Json yaml +``` diff --git a/examples/gameConfig.ron b/examples/gameConfig.ron new file mode 100644 index 0000000..2f6b6a1 --- /dev/null +++ b/examples/gameConfig.ron @@ -0,0 +1,26 @@ +GameConfig( // optional struct name + window_size: (800, 600), + window_title: "PAC-MAN", + fullscreen: false, + + mouse_sensitivity: 1.4, + key_bindings: { + "up": Up, + "down": Down, + "left": Left, + "right": Right, + + // Uncomment to enable WASD controls + /* + "W": Up, + "A": Down, + "S": Left, + "D": Right, + */ + }, + + difficulty_options: ( + start_difficulty: Easy, + adaptive: false, + ), +) diff --git a/examples/simple.json b/examples/simple.json new file mode 100644 index 0000000..f435905 --- /dev/null +++ b/examples/simple.json @@ -0,0 +1,4 @@ +{ + "a boolean": true, + "an array": [3, 2, 1] +} diff --git a/examples/standard.ron b/examples/standard.ron new file mode 100644 index 0000000..f45c763 --- /dev/null +++ b/examples/standard.ron @@ -0,0 +1,20 @@ +Scene( // class name is optional + materials: { // this is a map + "metal": ( + reflectivity: 1.0, + ), + "plastic": ( + reflectivity: 0.5, + ), + }, + entities: [ // this is an array + ( + name: "hero", + material: "metal", + ), + ( + name: "monster", + material: "plastic", + ), + ], +) diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c9c34be --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,113 @@ +use clap::{Parser, ValueEnum}; +use serde::de::Deserializer; +use std::io::{Read, Write}; + +#[derive(Parser)] +#[clap(author, version, about, long_about = None)] +pub struct Opt { + /// Input type + #[clap(value_enum, ignore_case = true)] + pub input: Input, + /// Output type + #[clap(value_enum, ignore_case = true)] + pub output: Output, + + /// For data format compatible, a default pretty format is output instead of a minified one + /// Output a pretty formated data instead of minified, only for format compatible + #[clap(long, short)] + pub pretty: bool, + /// Do not output a newline at the end of the stream + #[clap(long, short)] + pub no_newline: bool, +} + +#[derive(Copy, Clone, Debug, ValueEnum)] +pub enum Input { + Json, + Ron, + Yaml, +} + +#[derive(Copy, Clone, Debug, ValueEnum)] +pub enum Output { + Json, + Ron, + Yaml, +} + +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 + // (de)serializer and then call `serde_transcode::transcode(deserializer, serializer)` + // here once for all. This will be a clearer separation of function, + // but it may need `erased_serde` that I did not made it work with `serde_transcode`. + + if !opt.no_newline { + output.write_all(b"\n").expect( + "We already wrote the whole serialized struct, a new line at the end should be OK", + ); + } +} + +fn de(opt: &Opt, input: &mut dyn Read, output: &mut dyn Write) { + match opt.input { + Input::Json => { + // NOTE: Apparently serde_json do not implement `Deserializer` on const? + let mut deserializer = serde_json::Deserializer::from_reader(input); + ser(opt, &mut deserializer, output); + } + Input::Ron => { + // NOTE: Apparently ron do not implement `Deserializer` on const? + // NOTE: Apparently ron do not implement `Deserializer::from_reader` but it can serialise into a writter… + let mut buf = vec![]; + let _buf_size = input.read_to_end(&mut buf).unwrap(); + let mut deserializer = ron::Deserializer::from_bytes(&buf).unwrap(); + ser(opt, &mut deserializer, output); + } + Input::Yaml => { + let deserializer = serde_yaml::Deserializer::from_reader(input); + ser(opt, deserializer, output); + } + }; +} + +fn ser<'de, D>(opt: &Opt, deserializer: D, output: &mut dyn Write) +where + D: Deserializer<'de>, +{ + match opt.output { + Output::Json => { + if opt.pretty { + let formatter = serde_json::ser::PrettyFormatter::with_indent(b"\t"); + let serializer = &mut serde_json::Serializer::with_formatter(output, formatter); + + serde_transcode::transcode(deserializer, serializer).unwrap(); + } else { + let serializer = &mut serde_json::Serializer::new(output); + + serde_transcode::transcode(deserializer, serializer).unwrap(); + }; + + // NOTE: serde_json’s PrettyFormatter and CompactFormatter are incompatibles… + // serde_transcode::transcode(deserializer, serializer).unwrap(); + } + Output::Ron => { + let mut pretty_config = None; + if opt.pretty { + let pretty = ron::ser::PrettyConfig::new().indentor("\t".to_owned()); + pretty_config = Some(pretty); + } + let serializer = &mut ron::Serializer::new(output, pretty_config).unwrap(); + + serde_transcode::transcode(deserializer, serializer).unwrap(); + } + Output::Yaml => { + let serializer = &mut serde_yaml::Serializer::new(output); + + serde_transcode::transcode(deserializer, serializer).unwrap(); + } + }; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a92febc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,12 @@ +mod lib; + +use crate::lib::*; + +use clap::Parser; +use std::io; + +fn main() { + let opt = Opt::parse(); + + transcode(opt, &mut io::stdin(), &mut io::stdout()); +}