From a202dd84e807c33332fc2f331418c7cc9f27fc92 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Sat, 16 May 2020 22:11:38 +0100 Subject: [PATCH] Clean up dbus::arg::Variant type alias --- src/padfoot.rs | 3 +++ src/padfoot/connection.rs | 3 ++- src/padfoot/connection/contact_list.rs | 5 +---- src/padfoot/connection/contacts.rs | 21 +++++++++---------- src/padfoot/connection/requests.rs | 7 ++----- src/padfoot/connection_manager.rs | 28 +++++++++++++------------- src/padfoot/protocol.rs | 27 +++++++++---------------- src/padfoot/var_arg.rs | 19 +++++++++++++++++ 8 files changed, 60 insertions(+), 53 deletions(-) create mode 100644 src/padfoot/var_arg.rs diff --git a/src/padfoot.rs b/src/padfoot.rs index de26bd3..5991558 100644 --- a/src/padfoot.rs +++ b/src/padfoot.rs @@ -6,3 +6,6 @@ pub use self::connection_manager::*; mod protocol; pub use self::protocol::*; + +mod var_arg; +pub use self::var_arg::*; diff --git a/src/padfoot/connection.rs b/src/padfoot/connection.rs index c1a7434..0f371c4 100644 --- a/src/padfoot/connection.rs +++ b/src/padfoot/connection.rs @@ -20,6 +20,7 @@ pub use self::requests::*; mod simple_presence; pub use self::simple_presence::*; +use crate::padfoot::VarArg; use crate::telepathy; use dbus::blocking::{stdintf::org_freedesktop_dbus::RequestNameReply, LocalConnection}; @@ -60,7 +61,7 @@ pub struct ConnSettings { } impl ConnSettings { - pub fn from_params(params: HashMap<&str, super::Variant>) -> Result { + pub fn from_params(params: HashMap<&str, VarArg>) -> Result { let err = Err(MethodErr::no_arg()); let account = match params.get("account") { diff --git a/src/padfoot/connection/contact_list.rs b/src/padfoot/connection/contact_list.rs index 6c7742d..326c251 100644 --- a/src/padfoot/connection/contact_list.rs +++ b/src/padfoot/connection/contact_list.rs @@ -1,6 +1,6 @@ +use crate::padfoot::VarArg; use crate::telepathy; -use dbus::arg::{RefArg, Variant}; use dbus::tree::MethodErr; use deltachat::constants::DC_GCL_ADD_SELF; use deltachat::contact::Contact; @@ -16,9 +16,6 @@ impl AsRef for std::rc: } } -// TODO: extract a utility module for this? -type VarArg = Variant>; - // FIXME: come back and do this properly later impl telepathy::ConnectionInterfaceContactList for Connection { fn get_contact_list_attributes( diff --git a/src/padfoot/connection/contacts.rs b/src/padfoot/connection/contacts.rs index 881614c..824b2e4 100644 --- a/src/padfoot/connection/contacts.rs +++ b/src/padfoot/connection/contacts.rs @@ -1,6 +1,6 @@ +use crate::padfoot::{var_str, VarArg}; use crate::telepathy; -use dbus::arg::{RefArg, Variant}; use dbus::tree::MethodErr; use deltachat::contact::{Contact, Origin}; use std::collections::HashMap; @@ -13,16 +13,13 @@ impl AsRef for std::rc::Rc } } -// TODO: extract a utility module for this? -type VarArgs = Variant>; - impl telepathy::ConnectionInterfaceContacts for Connection { fn get_contact_attributes( &self, handles: Vec, interfaces: Vec<&str>, hold: bool, - ) -> Result>, MethodErr> { + ) -> Result>, MethodErr> { println!( "Connection<{}>::get_contact_attributes({:?}, {:?}, {})", self.id(), @@ -31,7 +28,7 @@ impl telepathy::ConnectionInterfaceContacts for Connection { hold ); - let mut out = HashMap::>::new(); + let mut out = HashMap::>::new(); for id in handles.iter() { // FIXME: work out how to use get_all let contact = match Contact::get_by_id(&self.ctx.read().unwrap(), *id) { @@ -39,28 +36,28 @@ impl telepathy::ConnectionInterfaceContacts for Connection { Err(_e) => continue, // Invalid IDs are silently ignored }; - let mut props = HashMap::::new(); + let mut props = HashMap::::new(); // This is mandatory props.insert( "org.freedesktop.Telepathy.Connection/contact-id".to_string(), - Variant(Box::new(contact.get_addr().to_string())), + var_str(contact.get_addr().to_string()), ); // The empty string means "no avatar" props.insert( "org.freedesktop.Telepathy.Connection.Interface.Avatars/token".to_string(), - Variant(Box::new("".to_string())), + var_str("".to_string()), ); /* // TODO: we need to publish DBUS services on these endpoints props.insert( "org.freedesktop.Telepathy.Connection.Interface.ContactList/publish".to_string(), - Variant(Box::new(4)), + var_arg(Box::new(4)), ); props.insert( "org.freedesktop.Telepathy.Connection.Interface.ContactList/subscribe".to_string(), - Variant(Box::new(4)), + var_arg(Box::new(4)), ); */ out.insert(*id, props); @@ -73,7 +70,7 @@ impl telepathy::ConnectionInterfaceContacts for Connection { &self, identifier: &str, interfaces: Vec<&str>, - ) -> Result<(u32, HashMap), MethodErr> { + ) -> Result<(u32, HashMap), MethodErr> { println!( "Connection<{}>::get_contact_by_id({}, {:?})", self.id(), diff --git a/src/padfoot/connection/requests.rs b/src/padfoot/connection/requests.rs index 7b2b6a6..bfb73ff 100644 --- a/src/padfoot/connection/requests.rs +++ b/src/padfoot/connection/requests.rs @@ -1,14 +1,11 @@ +use crate::padfoot::{requestables, VarArg}; use crate::telepathy; -use dbus::arg::{RefArg, Variant}; use dbus::tree::MethodErr; use std::collections::HashMap; use super::Connection; -// TODO: extract a utility module for this? -type VarArg = Variant>; - impl AsRef for std::rc::Rc { fn as_ref(&self) -> &(dyn telepathy::ConnectionInterfaceRequests + 'static) { &**self @@ -44,6 +41,6 @@ impl telepathy::ConnectionInterfaceRequests for Connection { fn requestable_channel_classes(&self) -> Result, MethodErr> { println!("Connection<{}>::requestable_channel_classes()", self.id()); - Ok(crate::padfoot::requestables()) + Ok(requestables()) } } diff --git a/src/padfoot/connection_manager.rs b/src/padfoot/connection_manager.rs index 74a5c51..a7ce23f 100644 --- a/src/padfoot/connection_manager.rs +++ b/src/padfoot/connection_manager.rs @@ -1,6 +1,6 @@ +use crate::padfoot::{var_arg, var_str, var_str_vec, VarArg}; use crate::telepathy; -use dbus::arg; use dbus::message::SignalArgs; use dbus::tree::MethodErr; use std::collections::{HashMap, HashSet}; @@ -114,7 +114,7 @@ impl telepathy::ConnectionManager for ConnectionManager { fn request_connection( &self, protocol: &str, - params: HashMap<&str, super::Variant>, + params: HashMap<&str, VarArg>, ) -> Result<(String, dbus::Path<'static>), MethodErr> { println!("CM::request_connection({}, ...)", protocol); @@ -124,48 +124,48 @@ impl telepathy::ConnectionManager for ConnectionManager { } } - fn protocols(&self) -> Result>, MethodErr> { + fn protocols(&self) -> Result>, MethodErr> { println!("CM::protocols()"); // FIXME: so much duplication. It would be good if we could get the // properties from the Protocol instead - let mut out = HashMap::>::new(); - let mut props = HashMap::::new(); + let mut out = HashMap::>::new(); + let mut props = HashMap::::new(); props.insert( "org.freedesktop.Telepathy.Protocol.AuthenticationTypes".to_string(), - arg::Variant(Box::new(vec![ + var_str_vec(vec![ "org.freedesktop.Telepathy.Channel.Type.ServerTLSConnection".to_string(), - ])), + ]), ); props.insert( "org.freedesktop.Telepathy.Protocol.ConnectionInterfaces".to_string(), - arg::Variant(Box::new(super::connection_interfaces())), + var_str_vec(super::connection_interfaces()), ); props.insert( "org.freedesktop.Telepathy.Protocol.EnglishName".to_string(), - arg::Variant(Box::new("Delta Chat".to_string())), + var_str("Delta Chat".to_string()), ); props.insert( "org.freedesktop.Telepathy.Protocol.Icon".to_string(), - arg::Variant(Box::new("im-delta".to_string())), + var_str("im-delta".to_string()), ); props.insert( "org.freedesktop.Telepathy.Protocol.Interfaces".to_string(), - arg::Variant(Box::new(super::protocol_interfaces())), + var_str_vec(super::protocol_interfaces()), ); props.insert( "org.freedesktop.Telepathy.Protocol.Parameters".to_string(), - arg::Variant(Box::new(super::parameters())), + var_arg(Box::new(super::parameters())), ); props.insert( "org.freedesktop.Telepathy.Protocol.RequestableChannelClasses".to_string(), - arg::Variant(Box::new(super::requestables())), + var_arg(Box::new(super::requestables())), ); props.insert( "org.freedesktop.Telepathy.Protocol.VCardField".to_string(), - arg::Variant(Box::new("email".to_string())), + var_str("email".to_string()), ); out.insert(super::PROTO_NAME.to_string(), props); diff --git a/src/padfoot/protocol.rs b/src/padfoot/protocol.rs index d828923..d5fa07a 100644 --- a/src/padfoot/protocol.rs +++ b/src/padfoot/protocol.rs @@ -1,6 +1,6 @@ +use crate::padfoot::{var_str, var_u32, VarArg}; use crate::telepathy; -use dbus::arg; use dbus::tree::MethodErr; use deltachat as dc; use std::collections::HashMap; @@ -14,19 +14,17 @@ pub const PROTO_NAME: &str = "delta"; #[derive(Debug)] pub struct Protocol {} -pub type Variant = arg::Variant>; - pub type ParamSpec = ( String, // Name u32, // Flags (Conn_Mgr_Param_Flags) String, // Signature - Variant, // Default + VarArg, // Default value ); // Requestable_Channel_Class pub type RequestableChannelSpec = ( - HashMap, // Fixed properties - Vec, // Allowed properties + HashMap, // Fixed properties + Vec, // Allowed properties ); // FIXME: these should come from codegen @@ -43,13 +41,13 @@ pub fn parameters() -> Vec { "account".to_string(), FLAG_REQUIRED, "s".to_string(), - arg::Variant(Box::new("".to_string())), + var_str("".to_string()), ), ( "password".to_string(), FLAG_REQUIRED | FLAG_SECRET, "s".to_string(), - arg::Variant(Box::new("".to_string())), + var_str("".to_string()), ), ] } @@ -62,19 +60,14 @@ pub fn protocol_interfaces() -> Vec { } pub fn requestables() -> Vec { - let mut rf = HashMap::::new(); + let mut rf = HashMap::::new(); rf.insert( "org.freedesktop.Telepathy.Channel.ChannelType".to_string(), - arg::Variant(Box::new( - "org.freedesktop.Telepathy.Channel.Type.Text".to_string(), - )), + var_str("org.freedesktop.Telepathy.Channel.Type.Text".to_string()), ); - rf.insert( - "org.freedesktop.Telepathy.Channel.TargetHandleType".to_string(), - arg::Variant(Box::new(1)), - ); + rf.insert("org.freedesktop.Telepathy.Channel.TargetHandleType".to_string(), var_u32(1)); let ra = vec![ "org.freedesktop.Telepathy.Channel.TargetHandle".to_string(), @@ -91,7 +84,7 @@ impl AsRef for std::rc::Rc { } impl telepathy::Protocol for Protocol { - fn identify_account(&self, params: HashMap<&str, Variant>) -> Result { + fn identify_account(&self, params: HashMap<&str, VarArg>) -> Result { println!("Protocol::identify_account(...)"); let settings = ConnSettings::from_params(params)?; diff --git a/src/padfoot/var_arg.rs b/src/padfoot/var_arg.rs new file mode 100644 index 0000000..05228f4 --- /dev/null +++ b/src/padfoot/var_arg.rs @@ -0,0 +1,19 @@ +use dbus::arg::{RefArg, Variant}; + +pub type VarArg = Variant>; + +pub fn var_arg(item: Box) -> VarArg { + Variant(item) +} + +pub fn var_str(item: String) -> VarArg { + var_arg(Box::new(item)) +} + +pub fn var_str_vec(item: Vec) -> VarArg { + var_arg(Box::new(item)) +} + +pub fn var_u32(item: u32) -> VarArg { + var_arg(Box::new(item)) +}