use async_trait::async_trait;
use ntex_bytes::{ByteString, Bytes, BytesMut};
use crate::request::{RequestContext, Response};
use crate::{error::DecodeError, error::HttpError, types::Message};
pub trait ServiceDef {
const NAME: &'static str;
type Methods: MethodsDef;
#[inline]
fn method_by_name(name: &str) -> Option<Self::Methods> {
<Self::Methods as MethodsDef>::by_name(name)
}
}
pub trait MethodsDef: Sized {
fn by_name(name: &str) -> Option<Self>;
}
pub trait MethodDef {
const NAME: &'static str;
const PATH: ByteString;
type Input: Message;
type Output: Message;
#[inline]
fn decode(&self, buf: &mut Bytes) -> Result<Self::Input, DecodeError> {
Message::read(buf)
}
#[inline]
fn encode(&self, val: Self::Output, buf: &mut BytesMut) {
val.write(buf);
}
}
#[async_trait(?Send)]
pub trait Transport<T: MethodDef> {
type Error: From<HttpError>;
async fn request(
&self,
args: &T::Input,
ctx: RequestContext,
) -> Result<Response<T>, Self::Error>;
}
pub trait ClientInformation<T> {
fn create(transport: T) -> Self;
fn transport(&self) -> &T;
fn transport_mut(&mut self) -> &mut T;
fn into_inner(self) -> T;
}