use core::{fmt::Debug, future::Future};
use futures_util::Stream;
use serde::{Deserialize, Serialize};
use crate::{Call, Connection, Reply, connection::Socket};
#[cfg(feature = "std")]
pub type ReplyStreamItem<Params> = (Reply<Params>, Vec<std::os::fd::OwnedFd>);
#[cfg(not(feature = "std"))]
pub type ReplyStreamItem<Params> = Reply<Params>;
pub trait Service<Sock>
where
Sock: Socket,
{
type MethodCall<'de>: Deserialize<'de> + Debug;
type ReplyParams<'ser>: Serialize + Debug
where
Self: 'ser;
type ReplyStreamParams: Serialize + Debug;
type ReplyStream: Stream<Item = ReplyStreamItem<Self::ReplyStreamParams>> + Unpin;
type ReplyError<'ser>: Serialize + Debug
where
Self: 'ser;
fn handle<'ser>(
&'ser mut self,
method: &'ser Call<Self::MethodCall<'_>>,
conn: &mut Connection<Sock>,
#[cfg(feature = "std")] fds: Vec<std::os::fd::OwnedFd>,
) -> impl Future<
Output = HandleResult<Self::ReplyParams<'ser>, Self::ReplyStream, Self::ReplyError<'ser>>,
>;
}
#[cfg(feature = "std")]
pub type HandleResult<Params, ReplyStream, ReplyError> = (
MethodReply<Params, ReplyStream, ReplyError>,
Vec<std::os::fd::OwnedFd>,
);
#[cfg(not(feature = "std"))]
pub type HandleResult<Params, ReplyStream, ReplyError> =
MethodReply<Params, ReplyStream, ReplyError>;
#[derive(Debug)]
pub enum MethodReply<Params, ReplyStream, ReplyError> {
Single(Option<Params>),
Error(ReplyError),
Multi(ReplyStream),
}