ntex_service/
map_init_err.rs1use std::{fmt, marker::PhantomData};
2
3use super::ServiceFactory;
4
5pub struct MapInitErr<F, Sf, Err> {
9 f: F,
10 sf: Sf,
11 e: PhantomData<fn() -> Err>,
12}
13
14impl<F, Sf, Err> MapInitErr<F, Sf, Err> {
15 pub(crate) fn new<St, Req>(f: F, sf: Sf) -> Self
17 where
18 Sf: ServiceFactory<St, Req>,
19 F: Fn(Sf::InitError) -> Err,
20 {
21 Self {
22 f,
23 sf,
24 e: PhantomData,
25 }
26 }
27}
28
29impl<F, Sf, Err> Clone for MapInitErr<F, Sf, Err>
30where
31 F: Clone,
32 Sf: Clone,
33{
34 fn clone(&self) -> Self {
35 Self {
36 f: self.f.clone(),
37 sf: self.sf.clone(),
38 e: PhantomData,
39 }
40 }
41}
42
43impl<F, Sf, Err> fmt::Debug for MapInitErr<F, Sf, Err>
44where
45 Sf: fmt::Debug,
46{
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 f.debug_struct("MapInitErr")
49 .field("sf", &self.sf)
50 .field("map", &std::any::type_name::<F>())
51 .finish()
52 }
53}
54
55impl<F, Sf, St, Req, Err> ServiceFactory<St, Req> for MapInitErr<F, Sf, Err>
56where
57 F: Fn(Sf::InitError) -> Err + Clone,
58 Sf: ServiceFactory<St, Req>,
59{
60 type Res = Sf::Res;
61 type Error = Sf::Error;
62
63 type Service = Sf::Service;
64 type InitError = Err;
65
66 #[inline]
67 async fn create(&self, st: &St) -> Result<Self::Service, Self::InitError> {
68 self.sf.create(st).await.map_err(|e| (self.f)(e))
69 }
70}
71
72#[cfg(test)]
73#[path = "../tests/unit/map_init_err.rs"]
74mod tests;