Add GVariant with the same (very low) hope to trully make it work as DBus

This commit is contained in:
Zykino 2022-10-27 00:22:37 +02:00
parent ceb10fabd9
commit f5b2a25eaf

View File

@ -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<String>,
#[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<String>,
/// 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::<LE>::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::<LE>::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::<LE>::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;