diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 2351feb..2fbeeb5 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -1,5 +1,55 @@ -mod message; +use alloc::{boxed::Box, string::String, vec::Vec}; -pub mod symbols; +use crate::tree::request::{TreeRequest, TreeRequestType}; -pub struct Tree {} +mod request; + +pub mod types; + +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..search_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; +} diff --git a/src/tree/message.rs b/src/tree/request.rs similarity index 69% rename from src/tree/message.rs rename to src/tree/request.rs index c1a5c99..18c21f4 100644 --- a/src/tree/message.rs +++ b/src/tree/request.rs @@ -1,25 +1,24 @@ // use std::collections::VecDeque; -use alloc::{collections::vec_deque::VecDeque, string::String, vec::Vec}; +use alloc::{string::String, vec::Vec}; use rkyv::{Archive, Deserialize, Serialize}; #[derive(Archive, Deserialize, Serialize)] #[rkyv(compare(PartialEq), derive(Debug))] pub struct TreeRequest { // The exact path that this packet should be heading down to - destination_path: VecDeque, - // The list of previous paths that this packet came from - // This is the destination path added in reverse order - source_path: VecDeque, - - request_type: TreeRequestType, + pub path: Vec, + // // The list of previous paths that this packet came from + // // This is the destination path added in reverse order + // pub source_path: VecDeque, + pub request_type: TreeRequestType, // The data type of the payload, to determine how to deserialize and interpret it on the other side // This is equivalent to HTTP's content-type header - content_type: String, + pub content_type: String, // The payload of the packet - data: Vec, + pub data: Vec, } #[derive(Archive, Deserialize, Serialize)] diff --git a/src/tree/symbols.rs b/src/tree/symbols.rs deleted file mode 100644 index cf11e19..0000000 --- a/src/tree/symbols.rs +++ /dev/null @@ -1,15 +0,0 @@ -use crate::obfuscate::sym; - -pub const LOGGER: &'static str = sym!("Logger"); - -pub const TYPE_TREE: &'static str = sym!("Tree"); -pub const TYPE_QUEUE: &'static str = sym!("Queue"); - -pub const CMD_GET: &'static str = sym!("Get"); -pub const CMD_POLL: &'static str = sym!("Poll"); -pub const CMD_GET_LENGTH: &'static str = sym!("GetLength"); -pub const CMD_GET_CHILDREN: &'static str = sym!("GetChildren"); - -pub const ERR_UNSUPPORTED_METHOD: &'static str = sym!("UnsupportedMethod"); -pub const ERR_INVALID_COMMAND: &'static str = sym!("InvalidCommand"); -pub const ERR_INVALID_CHILD: &'static str = sym!("InvalidChild"); diff --git a/src/tree/types.rs b/src/tree/types.rs new file mode 100644 index 0000000..1ee6848 --- /dev/null +++ b/src/tree/types.rs @@ -0,0 +1,3 @@ +use crate::obfuscate::sym; + +pub const TYPE_NONE: &'static str = sym!("core/None");