use std::borrow::Cow;
use crate::dependency::Dependencies;
use crate::key::KeyName;
use crate::transport::Transport;
use crate::transport::enhancer::EnhancerSpec;
use crate::transport::metadata::{MetaTier, Metadata};
pub struct HandlerSpec<T: Transport, H> {
pub(crate) name: &'static str,
pub(crate) handler: H,
pub(crate) controller: EnhancerSpec<T>,
pub(crate) method: EnhancerSpec<T>,
pub(crate) meta: Metadata,
pub(crate) route: Option<Cow<'static, str>>,
pub(crate) shape: Shape,
pub(crate) dependencies: Dependencies,
}
impl<T: Transport, H: Send + Sync + 'static> HandlerSpec<T, H> {
pub fn new(name: &'static str, handler: H) -> Self {
HandlerSpec {
name,
handler,
controller: EnhancerSpec::new(),
method: EnhancerSpec::new(),
meta: Metadata::new(),
route: None,
shape: Shape::default(),
dependencies: Dependencies::default(),
}
}
pub fn controller(mut self, spec: EnhancerSpec<T>) -> Self {
self.controller = spec;
self
}
pub fn method(mut self, spec: EnhancerSpec<T>) -> Self {
self.method = spec;
self
}
pub fn meta(mut self, meta: Metadata) -> Self {
self.meta = meta;
self
}
pub fn route(mut self, route: impl Into<Cow<'static, str>>) -> Self {
self.route = Some(route.into());
self
}
pub fn shape(mut self, shape: Shape) -> Self {
self.shape = shape;
self
}
pub fn dependencies(mut self, dependencies: Dependencies) -> Self {
self.dependencies = dependencies;
self
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Shape {
#[default]
Unary,
ServerStreaming,
ClientStreaming,
Bidi,
}
pub struct HandlerInfo {
pub(crate) transport: &'static str,
pub(crate) controller: KeyName,
pub(crate) name: &'static str,
pub(crate) route: Option<Cow<'static, str>>,
pub(crate) prefix: Option<Cow<'static, str>>,
pub(crate) shape: Shape,
pub(crate) meta: Metadata,
}
impl HandlerInfo {
pub fn transport(&self) -> &'static str {
self.transport
}
pub fn controller(&self) -> KeyName {
self.controller
}
pub fn name(&self) -> &'static str {
self.name
}
pub fn route(&self) -> Option<&str> {
self.route.as_deref()
}
pub fn prefix(&self) -> Option<&str> {
self.prefix.as_deref()
}
pub fn shape(&self) -> Shape {
self.shape
}
pub fn metadata(&self) -> &Metadata {
&self.meta
}
pub fn meta<T: 'static>(&self) -> Option<&T> {
self.meta.get::<T>()
}
pub fn meta_all<T: 'static>(&self) -> impl Iterator<Item = &T> + '_ {
self.meta.get_all::<T>()
}
pub fn entries(&self) -> impl Iterator<Item = (&'static str, MetaTier)> + '_ {
self.meta.entries()
}
}