mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
Add tree type
This commit is contained in:
+53
-3
@@ -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<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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,25 +1,24 @@
|
|||||||
// use std::collections::VecDeque;
|
// 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};
|
use rkyv::{Archive, Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Archive, Deserialize, Serialize)]
|
#[derive(Archive, Deserialize, Serialize)]
|
||||||
#[rkyv(compare(PartialEq), derive(Debug))]
|
#[rkyv(compare(PartialEq), derive(Debug))]
|
||||||
pub struct TreeRequest {
|
pub struct TreeRequest {
|
||||||
// The exact path that this packet should be heading down to
|
// The exact path that this packet should be heading down to
|
||||||
destination_path: VecDeque<String>,
|
pub path: Vec<String>,
|
||||||
// The list of previous paths that this packet came from
|
// // The list of previous paths that this packet came from
|
||||||
// This is the destination path added in reverse order
|
// // This is the destination path added in reverse order
|
||||||
source_path: VecDeque<String>,
|
// pub source_path: VecDeque<String>,
|
||||||
|
pub request_type: TreeRequestType,
|
||||||
request_type: TreeRequestType,
|
|
||||||
|
|
||||||
// The data type of the payload, to determine how to deserialize and interpret it on the other side
|
// 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
|
// This is equivalent to HTTP's content-type header
|
||||||
content_type: String,
|
pub content_type: String,
|
||||||
|
|
||||||
// The payload of the packet
|
// The payload of the packet
|
||||||
data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Archive, Deserialize, Serialize)]
|
#[derive(Archive, Deserialize, Serialize)]
|
||||||
@@ -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");
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
use crate::obfuscate::sym;
|
||||||
|
|
||||||
|
pub const TYPE_NONE: &'static str = sym!("core/None");
|
||||||
Reference in New Issue
Block a user