From f5b2a25eaf13088c3016518df9c2b035dda6b63d Mon Sep 17 00:00:00 2001 From: Zykino Date: Thu, 27 Oct 2022 00:22:37 +0200 Subject: [PATCH] Add GVariant with the same (very low) hope to trully make it work as DBus --- src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d1af8b6..5d87528 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,9 +15,9 @@ pub struct Opt { #[clap(value_enum, ignore_case = true)] pub output: Output, - #[clap(long, short = 'F')] + #[clap(long, short = 'F', required_if_eq_any(&[("input", "d-bus"), ("input", "g-variant")]))] pub format_in: Option, - #[clap(long, short = 'f', required_if_eq_any(&[("output", "d-bus")]))] + #[clap(long, short = 'f', required_if_eq_any(&[("output", "d-bus"), ("output", "g-variant")]))] pub format_out: Option, /// For data format compatible, a default pretty format is output instead of a minified one @@ -34,6 +34,7 @@ pub enum Input { //Bson, //Cbor, DBus, + GVariant, Json, Json5, //SExpression, @@ -49,8 +50,8 @@ pub enum Input { pub enum Output { //Bson, //Cbor, - //#[clap(requires("format_out"))] DBus, + GVariant, 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, @@ -107,6 +108,24 @@ fn de(opt: &Opt, input: &mut dyn Read, output: &mut dyn Write) { ); ser(opt, &mut deserializer, output); } + Input::GVariant => { + use zvariant::gvariant::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; @@ -189,7 +208,7 @@ where let mut out = std::io::Cursor::new(vec![]); let mut fs = vec![]; let serializer = &mut Serializer::new( - &Signature::try_from(dbg!(sig)).unwrap(), + &Signature::try_from(sig).unwrap(), &mut out, &mut fs, //None, EncodingContext::::new_dbus(0), @@ -200,6 +219,28 @@ where // let mut deserializer = Deserializer::new(); serde_transcode::transcode(deserializer, serializer).unwrap(); } + Output::GVariant => { + use zvariant::gvariant::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(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;