use std::marker::PhantomData;
use anyhow::Error;
use crate::bufext::BufExt;
use crate::context_stack::ContextStack;
use crate::context_stack::DummyContextStack;
use crate::framing::Framing;
use crate::framing::FramingEncodedFinal;
pub trait RequestContext {
type ContextStack: ContextStack;
type Name: ?Sized;
fn get_context_stack(
&self,
service_name: &'static Self::Name,
fn_name: &'static Self::Name,
) -> Result<Self::ContextStack, Error>;
fn set_user_exception_header(&self, ex_type: &str, ex_reason: &str) -> Result<(), Error>;
}
pub struct DummyRequestContext<Name: ?Sized, Frame> {
_phantom: PhantomData<(Frame, Name)>,
}
impl<Name: ?Sized, Frame> DummyRequestContext<Name, Frame> {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
}
}
}
impl<Name: ?Sized, Frame: Framing> RequestContext for DummyRequestContext<Name, Frame>
where
FramingEncodedFinal<Frame>: BufExt,
{
type ContextStack = crate::context_stack::DummyContextStack<Name, Frame>;
type Name = Name;
fn get_context_stack(
&self,
_service_name: &Self::Name,
_fn_name: &Self::Name,
) -> Result<Self::ContextStack, Error> {
Ok(DummyContextStack::new())
}
fn set_user_exception_header(&self, _ex_type: &str, _ex_reason: &str) -> Result<(), Error> {
Ok(())
}
}
#[cfg(test)]
mod test {
use bytes::Bytes;
use super::*;
fn assert_context_stack(_: &impl RequestContext) {}
#[test]
fn check_unsized() {
assert_context_stack(&DummyRequestContext::<std::ffi::CStr, Bytes>::new());
}
}