use zbus_names::{BusName, InterfaceName, MemberName};
use crate::{zvariant::ObjectPath, Connection, Error, Result};
#[derive(Clone, Debug)]
pub struct SignalEmitter<'s> {
conn: Connection,
path: ObjectPath<'s>,
destination: Option<BusName<'s>>,
}
impl<'s> SignalEmitter<'s> {
pub fn new<P>(conn: &Connection, path: P) -> Result<Self>
where
P: TryInto<ObjectPath<'s>>,
P::Error: Into<Error>,
{
path.try_into()
.map(|p| Self {
conn: conn.clone(),
path: p,
destination: None,
})
.map_err(Into::into)
}
pub fn from_parts(conn: Connection, path: ObjectPath<'s>) -> Self {
Self {
conn,
path,
destination: None,
}
}
pub async fn emit<'i, 'm, I, M, B>(&self, interface: I, signal_name: M, body: &B) -> Result<()>
where
I: TryInto<InterfaceName<'i>>,
I::Error: Into<Error>,
M: TryInto<MemberName<'m>>,
M::Error: Into<Error>,
B: serde::ser::Serialize + zvariant::DynamicType,
{
self.conn
.emit_signal(
self.destination.as_ref(),
&self.path,
interface,
signal_name,
body,
)
.await
}
pub fn set_destination(mut self, destination: BusName<'s>) -> Self {
self.destination = Some(destination);
self
}
pub fn connection(&self) -> &Connection {
&self.conn
}
pub fn path(&self) -> &ObjectPath<'s> {
&self.path
}
pub fn destination(&self) -> Option<&BusName<'s>> {
self.destination.as_ref()
}
pub fn to_owned(&self) -> SignalEmitter<'static> {
SignalEmitter {
conn: self.conn.clone(),
path: self.path.to_owned(),
destination: self.destination.as_ref().map(|d| d.to_owned()),
}
}
pub fn into_owned(self) -> SignalEmitter<'static> {
SignalEmitter {
conn: self.conn,
path: self.path.into_owned(),
destination: self.destination.map(|d| d.into_owned()),
}
}
}