Clean up dbus::arg::Variant type alias

This commit is contained in:
2020-05-16 22:11:38 +01:00
parent 0f9dcf476b
commit a202dd84e8
8 changed files with 60 additions and 53 deletions

View File

@@ -6,3 +6,6 @@ pub use self::connection_manager::*;
mod protocol; mod protocol;
pub use self::protocol::*; pub use self::protocol::*;
mod var_arg;
pub use self::var_arg::*;

View File

@@ -20,6 +20,7 @@ pub use self::requests::*;
mod simple_presence; mod simple_presence;
pub use self::simple_presence::*; pub use self::simple_presence::*;
use crate::padfoot::VarArg;
use crate::telepathy; use crate::telepathy;
use dbus::blocking::{stdintf::org_freedesktop_dbus::RequestNameReply, LocalConnection}; use dbus::blocking::{stdintf::org_freedesktop_dbus::RequestNameReply, LocalConnection};
@@ -60,7 +61,7 @@ pub struct ConnSettings {
} }
impl ConnSettings { impl ConnSettings {
pub fn from_params(params: HashMap<&str, super::Variant>) -> Result<Self, MethodErr> { pub fn from_params(params: HashMap<&str, VarArg>) -> Result<Self, MethodErr> {
let err = Err(MethodErr::no_arg()); let err = Err(MethodErr::no_arg());
let account = match params.get("account") { let account = match params.get("account") {

View File

@@ -1,6 +1,6 @@
use crate::padfoot::VarArg;
use crate::telepathy; use crate::telepathy;
use dbus::arg::{RefArg, Variant};
use dbus::tree::MethodErr; use dbus::tree::MethodErr;
use deltachat::constants::DC_GCL_ADD_SELF; use deltachat::constants::DC_GCL_ADD_SELF;
use deltachat::contact::Contact; use deltachat::contact::Contact;
@@ -16,9 +16,6 @@ impl AsRef<dyn telepathy::ConnectionInterfaceContactList + 'static> for std::rc:
} }
} }
// TODO: extract a utility module for this?
type VarArg = Variant<Box<dyn RefArg + 'static>>;
// FIXME: come back and do this properly later // FIXME: come back and do this properly later
impl telepathy::ConnectionInterfaceContactList for Connection { impl telepathy::ConnectionInterfaceContactList for Connection {
fn get_contact_list_attributes( fn get_contact_list_attributes(

View File

@@ -1,6 +1,6 @@
use crate::padfoot::{var_str, VarArg};
use crate::telepathy; use crate::telepathy;
use dbus::arg::{RefArg, Variant};
use dbus::tree::MethodErr; use dbus::tree::MethodErr;
use deltachat::contact::{Contact, Origin}; use deltachat::contact::{Contact, Origin};
use std::collections::HashMap; use std::collections::HashMap;
@@ -13,16 +13,13 @@ impl AsRef<dyn telepathy::ConnectionInterfaceContacts + 'static> for std::rc::Rc
} }
} }
// TODO: extract a utility module for this?
type VarArgs = Variant<Box<dyn RefArg + 'static>>;
impl telepathy::ConnectionInterfaceContacts for Connection { impl telepathy::ConnectionInterfaceContacts for Connection {
fn get_contact_attributes( fn get_contact_attributes(
&self, &self,
handles: Vec<u32>, handles: Vec<u32>,
interfaces: Vec<&str>, interfaces: Vec<&str>,
hold: bool, hold: bool,
) -> Result<HashMap<u32, HashMap<String, VarArgs>>, MethodErr> { ) -> Result<HashMap<u32, HashMap<String, VarArg>>, MethodErr> {
println!( println!(
"Connection<{}>::get_contact_attributes({:?}, {:?}, {})", "Connection<{}>::get_contact_attributes({:?}, {:?}, {})",
self.id(), self.id(),
@@ -31,7 +28,7 @@ impl telepathy::ConnectionInterfaceContacts for Connection {
hold hold
); );
let mut out = HashMap::<u32, HashMap<String, VarArgs>>::new(); let mut out = HashMap::<u32, HashMap<String, VarArg>>::new();
for id in handles.iter() { for id in handles.iter() {
// FIXME: work out how to use get_all // FIXME: work out how to use get_all
let contact = match Contact::get_by_id(&self.ctx.read().unwrap(), *id) { 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 Err(_e) => continue, // Invalid IDs are silently ignored
}; };
let mut props = HashMap::<String, VarArgs>::new(); let mut props = HashMap::<String, VarArg>::new();
// This is mandatory // This is mandatory
props.insert( props.insert(
"org.freedesktop.Telepathy.Connection/contact-id".to_string(), "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" // The empty string means "no avatar"
props.insert( props.insert(
"org.freedesktop.Telepathy.Connection.Interface.Avatars/token".to_string(), "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 // TODO: we need to publish DBUS services on these endpoints
props.insert( props.insert(
"org.freedesktop.Telepathy.Connection.Interface.ContactList/publish".to_string(), "org.freedesktop.Telepathy.Connection.Interface.ContactList/publish".to_string(),
Variant(Box::new(4)), var_arg(Box::new(4)),
); );
props.insert( props.insert(
"org.freedesktop.Telepathy.Connection.Interface.ContactList/subscribe".to_string(), "org.freedesktop.Telepathy.Connection.Interface.ContactList/subscribe".to_string(),
Variant(Box::new(4)), var_arg(Box::new(4)),
); );
*/ */
out.insert(*id, props); out.insert(*id, props);
@@ -73,7 +70,7 @@ impl telepathy::ConnectionInterfaceContacts for Connection {
&self, &self,
identifier: &str, identifier: &str,
interfaces: Vec<&str>, interfaces: Vec<&str>,
) -> Result<(u32, HashMap<String, VarArgs>), MethodErr> { ) -> Result<(u32, HashMap<String, VarArg>), MethodErr> {
println!( println!(
"Connection<{}>::get_contact_by_id({}, {:?})", "Connection<{}>::get_contact_by_id({}, {:?})",
self.id(), self.id(),

View File

@@ -1,14 +1,11 @@
use crate::padfoot::{requestables, VarArg};
use crate::telepathy; use crate::telepathy;
use dbus::arg::{RefArg, Variant};
use dbus::tree::MethodErr; use dbus::tree::MethodErr;
use std::collections::HashMap; use std::collections::HashMap;
use super::Connection; use super::Connection;
// TODO: extract a utility module for this?
type VarArg = Variant<Box<dyn RefArg + 'static>>;
impl AsRef<dyn telepathy::ConnectionInterfaceRequests + 'static> for std::rc::Rc<Connection> { impl AsRef<dyn telepathy::ConnectionInterfaceRequests + 'static> for std::rc::Rc<Connection> {
fn as_ref(&self) -> &(dyn telepathy::ConnectionInterfaceRequests + 'static) { fn as_ref(&self) -> &(dyn telepathy::ConnectionInterfaceRequests + 'static) {
&**self &**self
@@ -44,6 +41,6 @@ impl telepathy::ConnectionInterfaceRequests for Connection {
fn requestable_channel_classes(&self) -> Result<Vec<RequestableChannelSpec>, MethodErr> { fn requestable_channel_classes(&self) -> Result<Vec<RequestableChannelSpec>, MethodErr> {
println!("Connection<{}>::requestable_channel_classes()", self.id()); println!("Connection<{}>::requestable_channel_classes()", self.id());
Ok(crate::padfoot::requestables()) Ok(requestables())
} }
} }

View File

@@ -1,6 +1,6 @@
use crate::padfoot::{var_arg, var_str, var_str_vec, VarArg};
use crate::telepathy; use crate::telepathy;
use dbus::arg;
use dbus::message::SignalArgs; use dbus::message::SignalArgs;
use dbus::tree::MethodErr; use dbus::tree::MethodErr;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@@ -114,7 +114,7 @@ impl telepathy::ConnectionManager for ConnectionManager {
fn request_connection( fn request_connection(
&self, &self,
protocol: &str, protocol: &str,
params: HashMap<&str, super::Variant>, params: HashMap<&str, VarArg>,
) -> Result<(String, dbus::Path<'static>), MethodErr> { ) -> Result<(String, dbus::Path<'static>), MethodErr> {
println!("CM::request_connection({}, ...)", protocol); println!("CM::request_connection({}, ...)", protocol);
@@ -124,48 +124,48 @@ impl telepathy::ConnectionManager for ConnectionManager {
} }
} }
fn protocols(&self) -> Result<HashMap<String, HashMap<String, super::Variant>>, MethodErr> { fn protocols(&self) -> Result<HashMap<String, HashMap<String, VarArg>>, MethodErr> {
println!("CM::protocols()"); println!("CM::protocols()");
// FIXME: so much duplication. It would be good if we could get the // FIXME: so much duplication. It would be good if we could get the
// properties from the Protocol instead // properties from the Protocol instead
let mut out = HashMap::<String, HashMap<String, super::Variant>>::new(); let mut out = HashMap::<String, HashMap<String, VarArg>>::new();
let mut props = HashMap::<String, super::Variant>::new(); let mut props = HashMap::<String, VarArg>::new();
props.insert( props.insert(
"org.freedesktop.Telepathy.Protocol.AuthenticationTypes".to_string(), "org.freedesktop.Telepathy.Protocol.AuthenticationTypes".to_string(),
arg::Variant(Box::new(vec![ var_str_vec(vec![
"org.freedesktop.Telepathy.Channel.Type.ServerTLSConnection".to_string(), "org.freedesktop.Telepathy.Channel.Type.ServerTLSConnection".to_string(),
])), ]),
); );
props.insert( props.insert(
"org.freedesktop.Telepathy.Protocol.ConnectionInterfaces".to_string(), "org.freedesktop.Telepathy.Protocol.ConnectionInterfaces".to_string(),
arg::Variant(Box::new(super::connection_interfaces())), var_str_vec(super::connection_interfaces()),
); );
props.insert( props.insert(
"org.freedesktop.Telepathy.Protocol.EnglishName".to_string(), "org.freedesktop.Telepathy.Protocol.EnglishName".to_string(),
arg::Variant(Box::new("Delta Chat".to_string())), var_str("Delta Chat".to_string()),
); );
props.insert( props.insert(
"org.freedesktop.Telepathy.Protocol.Icon".to_string(), "org.freedesktop.Telepathy.Protocol.Icon".to_string(),
arg::Variant(Box::new("im-delta".to_string())), var_str("im-delta".to_string()),
); );
props.insert( props.insert(
"org.freedesktop.Telepathy.Protocol.Interfaces".to_string(), "org.freedesktop.Telepathy.Protocol.Interfaces".to_string(),
arg::Variant(Box::new(super::protocol_interfaces())), var_str_vec(super::protocol_interfaces()),
); );
props.insert( props.insert(
"org.freedesktop.Telepathy.Protocol.Parameters".to_string(), "org.freedesktop.Telepathy.Protocol.Parameters".to_string(),
arg::Variant(Box::new(super::parameters())), var_arg(Box::new(super::parameters())),
); );
props.insert( props.insert(
"org.freedesktop.Telepathy.Protocol.RequestableChannelClasses".to_string(), "org.freedesktop.Telepathy.Protocol.RequestableChannelClasses".to_string(),
arg::Variant(Box::new(super::requestables())), var_arg(Box::new(super::requestables())),
); );
props.insert( props.insert(
"org.freedesktop.Telepathy.Protocol.VCardField".to_string(), "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); out.insert(super::PROTO_NAME.to_string(), props);

View File

@@ -1,6 +1,6 @@
use crate::padfoot::{var_str, var_u32, VarArg};
use crate::telepathy; use crate::telepathy;
use dbus::arg;
use dbus::tree::MethodErr; use dbus::tree::MethodErr;
use deltachat as dc; use deltachat as dc;
use std::collections::HashMap; use std::collections::HashMap;
@@ -14,19 +14,17 @@ pub const PROTO_NAME: &str = "delta";
#[derive(Debug)] #[derive(Debug)]
pub struct Protocol {} pub struct Protocol {}
pub type Variant = arg::Variant<Box<dyn arg::RefArg + 'static>>;
pub type ParamSpec = ( pub type ParamSpec = (
String, // Name String, // Name
u32, // Flags (Conn_Mgr_Param_Flags) u32, // Flags (Conn_Mgr_Param_Flags)
String, // Signature String, // Signature
Variant, // Default VarArg, // Default value
); );
// Requestable_Channel_Class // Requestable_Channel_Class
pub type RequestableChannelSpec = ( pub type RequestableChannelSpec = (
HashMap<String, Variant>, // Fixed properties HashMap<String, VarArg>, // Fixed properties
Vec<String>, // Allowed properties Vec<String>, // Allowed properties
); );
// FIXME: these should come from codegen // FIXME: these should come from codegen
@@ -43,13 +41,13 @@ pub fn parameters() -> Vec<ParamSpec> {
"account".to_string(), "account".to_string(),
FLAG_REQUIRED, FLAG_REQUIRED,
"s".to_string(), "s".to_string(),
arg::Variant(Box::new("".to_string())), var_str("".to_string()),
), ),
( (
"password".to_string(), "password".to_string(),
FLAG_REQUIRED | FLAG_SECRET, FLAG_REQUIRED | FLAG_SECRET,
"s".to_string(), "s".to_string(),
arg::Variant(Box::new("".to_string())), var_str("".to_string()),
), ),
] ]
} }
@@ -62,19 +60,14 @@ pub fn protocol_interfaces() -> Vec<String> {
} }
pub fn requestables() -> Vec<RequestableChannelSpec> { pub fn requestables() -> Vec<RequestableChannelSpec> {
let mut rf = HashMap::<String, Variant>::new(); let mut rf = HashMap::<String, VarArg>::new();
rf.insert( rf.insert(
"org.freedesktop.Telepathy.Channel.ChannelType".to_string(), "org.freedesktop.Telepathy.Channel.ChannelType".to_string(),
arg::Variant(Box::new( var_str("org.freedesktop.Telepathy.Channel.Type.Text".to_string()),
"org.freedesktop.Telepathy.Channel.Type.Text".to_string(),
)),
); );
rf.insert( rf.insert("org.freedesktop.Telepathy.Channel.TargetHandleType".to_string(), var_u32(1));
"org.freedesktop.Telepathy.Channel.TargetHandleType".to_string(),
arg::Variant(Box::new(1)),
);
let ra = vec![ let ra = vec![
"org.freedesktop.Telepathy.Channel.TargetHandle".to_string(), "org.freedesktop.Telepathy.Channel.TargetHandle".to_string(),
@@ -91,7 +84,7 @@ impl AsRef<dyn telepathy::Protocol + 'static> for std::rc::Rc<Protocol> {
} }
impl telepathy::Protocol for Protocol { impl telepathy::Protocol for Protocol {
fn identify_account(&self, params: HashMap<&str, Variant>) -> Result<String, MethodErr> { fn identify_account(&self, params: HashMap<&str, VarArg>) -> Result<String, MethodErr> {
println!("Protocol::identify_account(...)"); println!("Protocol::identify_account(...)");
let settings = ConnSettings::from_params(params)?; let settings = ConnSettings::from_params(params)?;

19
src/padfoot/var_arg.rs Normal file
View File

@@ -0,0 +1,19 @@
use dbus::arg::{RefArg, Variant};
pub type VarArg = Variant<Box<dyn RefArg + 'static>>;
pub fn var_arg(item: Box<dyn RefArg + 'static>) -> VarArg {
Variant(item)
}
pub fn var_str(item: String) -> VarArg {
var_arg(Box::new(item))
}
pub fn var_str_vec(item: Vec<String>) -> VarArg {
var_arg(Box::new(item))
}
pub fn var_u32(item: u32) -> VarArg {
var_arg(Box::new(item))
}