rsiot/components_config/http_server/
put_endpoint.rs1use std::fmt::Debug;
2
3use serde::{de::DeserializeOwned, Serialize};
4
5use crate::{
6 message::{Message, MsgDataBound},
7 serde_utils::{self, SerdeAlg, SerdeAlgKind},
8};
9
10#[derive(Clone, Debug)]
12pub struct PutEndpointConfig<TMsg, TData>
13where
14 TMsg: MsgDataBound,
15{
16 pub serde_alg: SerdeAlgKind,
18
19 pub path: &'static str,
27
28 pub fn_output: fn(data: TData) -> Option<Message<TMsg>>,
30}
31
32impl<TMsg, TData> PutEndpoint<TMsg> for PutEndpointConfig<TMsg, TData>
33where
34 TData: 'static + Clone + Debug + DeserializeOwned + Serialize + Send + Sync,
35 TMsg: 'static + MsgDataBound,
36{
37 fn get_path(&self) -> &str {
38 self.path
39 }
40
41 fn fn_output(&self, request_body: &[u8]) -> Result<Option<Message<TMsg>>, serde_utils::Error> {
42 let serde_alg = SerdeAlg::new(self.serde_alg);
43 let data: TData = serde_alg.deserialize(request_body)?;
44 let msg = (self.fn_output)(data);
45 Ok(msg)
46 }
47
48 fn clone_dyn(&self) -> Box<dyn PutEndpoint<TMsg>> {
49 Box::new(self.clone())
50 }
51}
52
53pub trait PutEndpoint<TMsg>
58where
59 Self: Debug + Send + Sync,
60{
61 fn get_path(&self) -> &str;
63
64 fn fn_output(&self, request_body: &[u8]) -> Result<Option<Message<TMsg>>, serde_utils::Error>;
66
67 fn clone_dyn(&self) -> Box<dyn PutEndpoint<TMsg>>;
69}
70
71impl<TMsg> Clone for Box<dyn PutEndpoint<TMsg>> {
72 fn clone(&self) -> Self {
73 self.clone_dyn()
74 }
75}