use alloc::{boxed::Box, string::String, vec::Vec}; mod request; pub use request::{TreeRequest, TreeRequestType}; pub mod types; #[derive(Default)] pub struct Tree { endpoints: Vec<(Box, Vec)>, } impl Tree { pub fn add_endpoint(&mut self, endpoint: T, path: Vec) { self.add_endpoint_box(Box::new(endpoint), path); } pub fn add_endpoint_box(&mut self, endpoint: Box, path: Vec) { self.endpoints.push((endpoint, path)); } pub fn get_endpoint(&mut self, search_path: &Vec) -> Option<&mut Box> { for (endpoint, endpoint_path) in &mut self.endpoints { if search_path.len() < endpoint_path.len() { return None; } for i in 0..endpoint_path.len() { if search_path[i] != endpoint_path[i] { return None; } } return Some(endpoint); } return None; } pub fn request(&mut self, request: TreeRequest) -> TreeRequest { if let Some(endpoint) = self.get_endpoint(&request.path) { endpoint.request(request) } else { TreeRequest { path: request.path, request_type: TreeRequestType::NoBranchError, content_type: types::TYPE_NONE.into(), data: Vec::with_capacity(0), } } } } pub trait Endpoint { fn request(&mut self, request: TreeRequest) -> TreeRequest; }