use std::{future::Future, pin::Pin};
use zbus::message::Flags;
use zvariant::DynamicType;
use crate::{Connection, Result, fdo, message::Message};
use tracing::trace;
#[deprecated(since = "5.15.0", note = "Use `DispatchResult2` instead.")]
pub enum DispatchResult<'a> {
NotFound,
RequiresMut,
Async(Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>),
}
#[allow(deprecated)]
impl<'a> DispatchResult<'a> {
pub fn new_async<F, T, E>(conn: &'a Connection, msg: &'a Message, f: F) -> Self
where
F: Future<Output = ::std::result::Result<T, E>> + Send + 'a,
T: serde::Serialize + DynamicType + Send + Sync,
E: zbus::DBusError + Send,
{
DispatchResult::Async(Box::pin(async move {
let hdr = msg.header();
let ret = f.await;
if !hdr.primary().flags().contains(Flags::NoReplyExpected) {
match ret {
Ok(r) => conn.reply(&hdr, &r).await,
Err(e) => conn.reply_dbus_error(&hdr, e).await,
}
.map(|_seq| ())
} else {
trace!("No reply expected for {:?} by the caller.", msg);
Ok(())
}
}))
}
}
pub enum DispatchResult2<'a> {
NotFound,
RequiresMut,
Async(Pin<Box<dyn Future<Output = fdo::Result<()>> + Send + 'a>>),
}
impl<'a> DispatchResult2<'a> {
pub fn new_async<F, T, E>(conn: &'a Connection, msg: &'a Message, f: F) -> Self
where
F: Future<Output = ::std::result::Result<T, E>> + Send + 'a,
T: serde::Serialize + DynamicType + Send + Sync,
E: zbus::DBusError + Send,
{
DispatchResult2::Async(Box::pin(async move {
let hdr = msg.header();
let ret = f.await;
if !hdr.primary().flags().contains(Flags::NoReplyExpected) {
match ret {
Ok(r) => conn.reply(&hdr, &r).await,
Err(e) => conn.reply_dbus_error(&hdr, e).await,
}
.map(|_seq| ())
.map_err(|e| fdo::Error::Failed(e.to_string()))
} else {
trace!("No reply expected for {:?} by the caller.", msg);
Ok(())
}
}))
}
}