use core::{fmt::Debug, future::Future};
use futures_util::Stream;
use serde::{Deserialize, Serialize};
pub use super::infallible::Infallible;
use crate::{Call, Connection, Reply, connection::Socket};
#[cfg(feature = "std")]
pub type ReplyStreamItem<Params, Error> = (
core::result::Result<Reply<Params>, Error>,
Vec<std::os::fd::OwnedFd>,
);
#[cfg(not(feature = "std"))]
pub type ReplyStreamItem<Params, Error> = core::result::Result<Reply<Params>, Error>;
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 ReplyStreamError: Serialize + Debug;
type ReplyStream: Stream<Item = ReplyStreamItem<Self::ReplyStreamParams, Self::ReplyStreamError>>
+ 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),
}