rsiot/components_config/http_server/
get_endpoint.rsuse std::{collections::HashMap, fmt::Debug};
use serde::{de::DeserializeOwned, Serialize};
use serde_json::to_string;
use crate::message::{Message, MsgDataBound};
#[derive(Clone, Debug)]
pub struct GetEndpointConfig<TMsg, TData> {
pub path: &'static str,
pub data: TData,
pub fn_input: fn(&Message<TMsg>, &mut TData),
}
impl<TMsg, TData> GetEndpoint<TMsg> for GetEndpointConfig<TMsg, TData>
where
TMsg: 'static + MsgDataBound,
TData: 'static + Clone + Debug + DeserializeOwned + Serialize + Send + Sync,
{
fn get_path(&self) -> &str {
self.path
}
fn fn_input(&mut self, msg: &Message<TMsg>) {
(self.fn_input)(msg, &mut self.data)
}
fn get_json_data(&self) -> Result<String, serde_json::Error> {
to_string(&self.data)
}
fn clone_dyn(&self) -> Box<dyn GetEndpoint<TMsg>> {
Box::new(self.clone())
}
}
pub trait GetEndpoint<TMsg>
where
Self: Debug + Send + Sync,
{
fn get_path(&self) -> &str;
fn get_json_data(&self) -> Result<String, serde_json::Error>;
fn fn_input(&mut self, msg: &Message<TMsg>);
fn clone_dyn(&self) -> Box<dyn GetEndpoint<TMsg>>;
}
impl<TMsg> Clone for Box<dyn GetEndpoint<TMsg>> {
fn clone(&self) -> Self {
self.clone_dyn()
}
}
pub fn create_get_endpoints_hashmap<TMsg>(
config_endpoints: &[Box<dyn GetEndpoint<TMsg>>],
) -> HashMap<String, Box<dyn GetEndpoint<TMsg>>>
where
TMsg: MsgDataBound,
{
let mut endpoints = HashMap::new();
for endpoint in config_endpoints {
endpoints.insert(endpoint.get_path().to_string(), endpoint.clone());
}
endpoints
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::message::example_message::Custom;
#[test]
fn test1() {
let mut vec_trait: Vec<Box<dyn GetEndpoint<Custom>>> = vec![];
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Data1 {}
#[derive(Clone, Debug, Deserialize, Serialize)]
struct Data2 {}
let end1 = GetEndpointConfig {
path: "/1",
data: Data1 {},
fn_input: |_, _| (),
};
let end2 = GetEndpointConfig {
path: "/2",
data: Data2 {},
fn_input: |_, _| (),
};
vec_trait.push(Box::new(end1));
vec_trait.push(Box::new(end2));
let mut map = HashMap::new();
for e in vec_trait.into_iter() {
let endpoint = e.get_path().to_string();
map.insert(endpoint, e);
}
}
}