use arg;
use {Message, MessageType, BusName, Path, Interface, Member};
pub trait SignalArgs: Default {
const NAME: &'static str;
const INTERFACE: &'static str;
fn append(&self, i: &mut arg::IterAppend);
fn get(&mut self, i: &mut arg::Iter) -> Result<(), arg::TypeMismatchError>;
fn to_emit_message(&self, path: &Path) -> Message {
let mut m = Message::signal(path, &Interface::from(Self::INTERFACE), &Member::from(Self::NAME));
self.append(&mut arg::IterAppend::new(&mut m));
m
}
fn from_message(m: &Message) -> Option<Self> {
if m.msg_type() != MessageType::Signal { None }
else if m.interface().as_ref().map(|x| &**x) != Some(Self::INTERFACE) { None }
else if m.member().as_ref().map(|x| &**x) != Some(Self::NAME) { None }
else {
let mut z: Self = Default::default();
z.get(&mut m.iter_init()).ok().map(|_| z)
}
}
fn match_str(sender: Option<&BusName>, path: Option<&Path>) -> String {
let mut r = format!("type='signal',interface='{}',member='{}'", Self::INTERFACE, Self::NAME);
sender.map(|s| r += &format!(",sender='{}'", s));
path.map(|s| r += &format!(",path='{}'", s));
r
}
}
#[test]
fn intf_removed() {
use {Connection, BusType};
use stdintf::org_freedesktop_dbus::ObjectManagerInterfacesRemoved as IR;
let c = Connection::get_private(BusType::Session).unwrap();
let mstr = IR::match_str(Some(&c.unique_name().into()), Some(&"/hello".into()));
println!("Match str: {}", mstr);
c.add_match(&mstr).unwrap();
let ir = IR { object: "/hello".into(), interfaces: vec!("ABC.DEF".into(), "GHI.JKL".into()) };
let cp = c.with_path("dbus.dummy", "/hello", 2000);
cp.emit(&ir).unwrap();
for msg in c.incoming(1000) {
if &*msg.sender().unwrap() != &*c.unique_name() { continue; }
if let Some(ir2) = IR::from_message(&msg) {
assert_eq!(ir2.object, ir.object);
assert_eq!(ir2.interfaces, ir.interfaces);
break;
}
}
}