2026-03-17 17:29:36 -06:00
|
|
|
use alloc::{boxed::Box, string::String, vec::Vec};
|
2026-02-09 10:27:15 -07:00
|
|
|
|
2026-03-17 17:29:36 -06:00
|
|
|
mod request;
|
|
|
|
|
|
2026-03-18 12:01:21 -06:00
|
|
|
pub use request::{TreeRequest, TreeRequestType};
|
|
|
|
|
|
2026-03-17 17:29:36 -06:00
|
|
|
pub mod types;
|
|
|
|
|
|
2026-03-18 12:01:21 -06:00
|
|
|
#[derive(Default)]
|
2026-03-17 17:29:36 -06:00
|
|
|
pub struct Tree {
|
|
|
|
|
endpoints: Vec<(Box<dyn Endpoint>, Vec<String>)>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Tree {
|
|
|
|
|
pub fn add_endpoint<T: Endpoint + 'static>(&mut self, endpoint: T, path: Vec<String>) {
|
|
|
|
|
self.add_endpoint_box(Box::new(endpoint), path);
|
|
|
|
|
}
|
|
|
|
|
pub fn add_endpoint_box(&mut self, endpoint: Box<dyn Endpoint>, path: Vec<String>) {
|
|
|
|
|
self.endpoints.push((endpoint, path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_endpoint(&mut self, search_path: &Vec<String>) -> Option<&mut Box<dyn Endpoint>> {
|
|
|
|
|
for (endpoint, endpoint_path) in &mut self.endpoints {
|
|
|
|
|
if search_path.len() < endpoint_path.len() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 12:01:21 -06:00
|
|
|
for i in 0..endpoint_path.len() {
|
2026-03-17 17:29:36 -06:00
|
|
|
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;
|
|
|
|
|
}
|