layer_climb_core/querier/middleware/
logger.rs1use std::sync::Arc;
2
3use crate::prelude::*;
4
5#[derive(Clone)]
6pub struct QueryLoggerMiddlewareMapReq {
7 pub logger_fn: Arc<dyn Fn(String) + Send + Sync>,
8}
9impl QueryLoggerMiddlewareMapReq {
10 pub fn new<F>(logger_fn: F) -> Self
11 where
12 F: Fn(String) + Send + Sync + 'static,
13 {
14 Self {
15 logger_fn: Arc::new(logger_fn),
16 }
17 }
18}
19impl Default for QueryLoggerMiddlewareMapReq {
20 fn default() -> Self {
21 Self::new(|msg| eprintln!("{}", msg))
22 }
23}
24
25impl QueryLoggerMiddlewareMapReq {
26 pub async fn map_req<REQ: QueryRequest>(&self, req: REQ) -> Result<REQ> {
27 (self.logger_fn)(format!("{:?}", req));
28 Ok(req)
29 }
30}
31
32pub struct QueryLoggerMiddlewareMapResp {
33 pub logger_fn: Arc<dyn Fn(String) + Send + Sync>,
34}
35impl QueryLoggerMiddlewareMapResp {
36 pub fn new<F>(logger_fn: F) -> Self
37 where
38 F: Fn(String) + Send + Sync + 'static,
39 {
40 Self {
41 logger_fn: Arc::new(logger_fn),
42 }
43 }
44}
45impl Default for QueryLoggerMiddlewareMapResp {
46 fn default() -> Self {
47 Self::new(|msg| eprintln!("{}", msg))
48 }
49}
50
51impl QueryLoggerMiddlewareMapResp {
52 pub async fn map_resp<RESP: std::fmt::Debug + Send>(&self, resp: RESP) -> Result<RESP> {
53 (self.logger_fn)(format!("{:?}", resp));
54 Ok(resp)
55 }
56}