mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
Convert to no_std, add request type
This commit is contained in:
+4
-1
@@ -1,8 +1,11 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
pub mod logger;
|
||||
pub mod tree;
|
||||
|
||||
// Re-exports
|
||||
pub use serde_json::{Value, json};
|
||||
// pub use serde_json::{Value, json};
|
||||
pub use ush_obfuscate as obfuscate;
|
||||
|
||||
+6
-6
@@ -7,15 +7,15 @@ mod log_disabled;
|
||||
|
||||
mod pretty_logger;
|
||||
|
||||
use std::time::SystemTime;
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use alloc::string::String;
|
||||
pub use pretty_logger::PrettyLogger;
|
||||
pub use pretty_logger::log;
|
||||
|
||||
pub static mut IS_DEFAULT_LOGGER: bool = true;
|
||||
static mut LOGGER: &dyn Logger = &DefaultLogger;
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[derive(Debug)]
|
||||
pub enum LogLevel {
|
||||
Debug,
|
||||
Info,
|
||||
@@ -23,12 +23,12 @@ pub enum LogLevel {
|
||||
Error,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[derive(Debug)]
|
||||
pub struct Record {
|
||||
log_level: LogLevel,
|
||||
location: Option<String>,
|
||||
// line: u32,
|
||||
time: Option<SystemTime>,
|
||||
time: Option<u64>,
|
||||
message: String,
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ pub fn set_logger(logger: &'static dyn Logger) {
|
||||
pub fn add_record(
|
||||
log_level: LogLevel,
|
||||
location: Option<String>,
|
||||
time: Option<SystemTime>,
|
||||
time: Option<u64>,
|
||||
message: String,
|
||||
) {
|
||||
logger().log(Record {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use alloc::{boxed::Box, format};
|
||||
|
||||
use crate::logger::{LogLevel, Logger, Record};
|
||||
|
||||
pub struct PrettyLogger {
|
||||
@@ -31,7 +33,7 @@ pub fn log(message: &Record) {
|
||||
(None, None) => {
|
||||
static WHITE: &str = "\x1b[97m";
|
||||
|
||||
println!("{} {WHITE}{}", log_level, message.message,);
|
||||
unix_print::unix_println!("{} {WHITE}{}", log_level, message.message);
|
||||
}
|
||||
|
||||
#[cfg(feature = "log_debug")]
|
||||
@@ -45,9 +47,12 @@ pub fn log(message: &Record) {
|
||||
static TIME_COLOR: &str = "\x1b[36m";
|
||||
static GREY: &str = "\x1b[90m";
|
||||
|
||||
println!(
|
||||
unix_print::unix_println!(
|
||||
"{OFF_WHITE}[{TIME_COLOR}{}{OFF_WHITE}] {} {WHITE}{} {GREY}{}{WHITE}",
|
||||
date, log_level, message.message, location
|
||||
date,
|
||||
log_level,
|
||||
message.message,
|
||||
location
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
// use std::collections::VecDeque;
|
||||
|
||||
use alloc::{collections::vec_deque::VecDeque, 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<String>,
|
||||
// The list of previous paths that this packet came from
|
||||
// This is the destination path added in reverse order
|
||||
source_path: VecDeque<String>,
|
||||
|
||||
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,
|
||||
|
||||
// The payload of the packet
|
||||
data: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Archive, Deserialize, Serialize)]
|
||||
#[rkyv(compare(PartialEq), derive(Debug))]
|
||||
pub enum TreeRequestType {
|
||||
Return = 0,
|
||||
Read = 1,
|
||||
Write = 2,
|
||||
Submit = 3,
|
||||
|
||||
ListBranches = 10,
|
||||
|
||||
// CreateField = 3,
|
||||
// DeleteField = 4,
|
||||
UnnamedError = 100,
|
||||
NoBranchError = 101,
|
||||
ProtocolError = 102,
|
||||
ExecutionError = 103,
|
||||
}
|
||||
+2
-78
@@ -1,81 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
mod message;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
mod log;
|
||||
pub mod symbols;
|
||||
|
||||
pub trait TreeElement {
|
||||
// fn get_children(&self) -> HashMap<TreeType, TreeType>;
|
||||
fn get_type(&self) -> Value;
|
||||
fn send_message(&mut self, target: Value, message: Value) -> Value;
|
||||
}
|
||||
|
||||
pub struct Tree {
|
||||
elements: HashMap<String, Box<dyn TreeElement>>,
|
||||
}
|
||||
|
||||
impl Tree {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
elements: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_element(&mut self, name: String, element: Box<dyn TreeElement>) {
|
||||
self.elements.insert(name, element);
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeElement for Tree {
|
||||
fn get_type(&self) -> Value {
|
||||
json!(symbols::TYPE_TREE)
|
||||
}
|
||||
|
||||
// fn send_message_child(&mut self, element: Value, message: TreeMessage) -> TreeMessage {
|
||||
// let name = if let TreeType::String(name) = element {
|
||||
// name
|
||||
// } else {
|
||||
// return TreeMessage::Error(ModuleError::InvalidType);
|
||||
// };
|
||||
|
||||
// if let Some(element) = self.elements.get_mut(&name) {
|
||||
// element.send_message(message)
|
||||
// } else {
|
||||
// TreeMessage::Error(ModuleError::TreeNotExist)
|
||||
// }
|
||||
// }
|
||||
|
||||
fn send_message(&mut self, target: Value, message: Value) -> Value {
|
||||
match target {
|
||||
Value::Null => {
|
||||
if let Some(message) = message.as_str() {
|
||||
match message {
|
||||
"GetChildren" => {
|
||||
let children = self
|
||||
.elements
|
||||
.iter()
|
||||
.map(|c| (Value::String(c.0.clone()), c.1.get_type()))
|
||||
.into_iter()
|
||||
.collect::<HashMap<Value, Value>>();
|
||||
|
||||
json!(children)
|
||||
}
|
||||
|
||||
_ => Value::String("UnsupportedMethod".to_owned()),
|
||||
}
|
||||
} else {
|
||||
Value::String("UnsupportedMethod".to_owned())
|
||||
}
|
||||
}
|
||||
Value::String(target) => {
|
||||
if let Some(child) = self.elements.get_mut(&target) {
|
||||
child.send_message(Value::Null, message)
|
||||
} else {
|
||||
Value::String("UnsupportedMethod".to_owned())
|
||||
}
|
||||
}
|
||||
_ => Value::String("UnsupportedMethod".to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub struct Tree {}
|
||||
|
||||
Reference in New Issue
Block a user