diff --git a/Cargo.lock b/Cargo.lock index fa829b0..b5c2dd7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -288,6 +288,17 @@ dependencies = [ "sha1", ] +[[package]] +name = "proc-macro-crate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eda0fc3b0fb7c975631757e14d9049da17374063edb6ebbcbc54d880d4fe94e9" +dependencies = [ + "once_cell", + "thiserror", + "toml", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -457,6 +468,12 @@ dependencies = [ "digest", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "strsim" version = "0.10.0" @@ -509,10 +526,20 @@ dependencies = [ "syn", ] +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + [[package]] name = "tradaf" version = "0.1.0" dependencies = [ + "byteorder", "clap", "json5", "rmp-serde", @@ -523,6 +550,7 @@ dependencies = [ "serde_json", "serde_qs", "serde_yaml", + "zvariant", ] [[package]] @@ -585,3 +613,28 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "zvariant" +version = "3.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bd68e4e6432ef19df47d7e90e2e72b5e7e3d778e0ae3baddf12b951265cc758" +dependencies = [ + "byteorder", + "libc", + "serde", + "static_assertions", + "zvariant_derive", +] + +[[package]] +name = "zvariant_derive" +version = "3.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08e977eaa3af652f63d479ce50d924254ad76722a6289ec1a1eac3231ca30430" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index f759869..ad438b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,11 +16,11 @@ edition = "2021" [dependencies] # TODO: handle errors (or pass them to main) instead of unwrapping #anyhow = "1.0.64" -clap = { version = "3.2.20", features = ["derive"] } +clap = { version = "3.2", 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" +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 @@ -37,4 +37,5 @@ rmp-serde = "1.1" ron = "0.8" #toml = "0.5" serde_yaml = "0.9" - \ No newline at end of file +zvariant = "3.6" # D-Bus & GVariant # TODO: try to implement it with the from signature? + byteorder = "1.3" # required by zvariant diff --git a/src/lib.rs b/src/lib.rs index faff0e7..d1af8b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,9 @@ use clap::{Parser, ValueEnum}; use serde::de::Deserializer; -use std::io::{Read, Write}; +use std::{ + convert::TryFrom, + io::{Read, Write}, +}; #[derive(Debug, Parser)] #[clap(author, version, about, long_about = None)] @@ -12,6 +15,11 @@ pub struct Opt { #[clap(value_enum, ignore_case = true)] pub output: Output, + #[clap(long, short = 'F')] + pub format_in: Option, + #[clap(long, short = 'f', required_if_eq_any(&[("output", "d-bus")]))] + pub format_out: Option, + /// 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)] @@ -25,6 +33,7 @@ pub struct Opt { pub enum Input { //Bson, //Cbor, + DBus, Json, Json5, //SExpression, @@ -40,6 +49,8 @@ pub enum Input { pub enum Output { //Bson, //Cbor, + //#[clap(requires("format_out"))] + DBus, Json, //Json5, // NOTE: The crate is not noted "(deserialization only)" on the [serde listing](https://serde.rs/#data-formats) but it does not expose a `Serializer` //SExpression, @@ -78,6 +89,24 @@ fn de(opt: &Opt, input: &mut dyn Read, output: &mut dyn Write) { ser(opt, deserializer, output); } */ + Input::DBus => { + use zvariant::dbus::Deserializer; + use zvariant::EncodingContext; + use zvariant::Signature; + + use byteorder::LE; + + let sig = opt.format_in.clone().unwrap(); + let mut buf = String::new(); + let _buf_size = input.read_to_string(&mut buf).unwrap(); + let mut deserializer = Deserializer::new( + buf.as_bytes(), + None, + &Signature::try_from(sig).unwrap(), + EncodingContext::::new_dbus(0), + ); + ser(opt, &mut deserializer, output); + } Input::Json => { use serde_json::Deserializer; @@ -149,6 +178,28 @@ where serde_transcode::transcode(deserializer, serializer).unwrap(); } */ + Output::DBus => { + use zvariant::dbus::Serializer; + use zvariant::EncodingContext; + use zvariant::Signature; + + use byteorder::LE; + + let sig = opt.format_out.clone().unwrap(); + let mut out = std::io::Cursor::new(vec![]); + let mut fs = vec![]; + let serializer = &mut Serializer::new( + &Signature::try_from(dbg!(sig)).unwrap(), + &mut out, + &mut fs, //None, + EncodingContext::::new_dbus(0), + ); + + //let mut buf = String::new(); + // 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;