mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
Add trait for tree object
This commit is contained in:
@@ -18,9 +18,9 @@ pub static DEFAULT_HOST: String = "localhost".to_string();
|
||||
#[static_init::dynamic]
|
||||
pub static DATABASE_NAME: String = "database".to_string();
|
||||
|
||||
// #[static_init::dynamic]
|
||||
// pub static SERVER_CONFIG: unshell_lib::config::PayloadConfig = unshell_lib::config::PayloadConfig {
|
||||
// id: "Server",
|
||||
// components: unshell_lib::get_components(),
|
||||
// runtime_config: Vec::new(),
|
||||
// };
|
||||
#[static_init::dynamic]
|
||||
pub static SERVER_CONFIG: unshell_lib::config::PayloadConfig = unshell_lib::config::PayloadConfig {
|
||||
id: "Server",
|
||||
components: Vec::new(),
|
||||
runtime_config: Vec::new(),
|
||||
};
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
use std::{error::Error, path::PathBuf};
|
||||
use std::{
|
||||
error::Error,
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::server::tree2::Tree2;
|
||||
use unshell_lib::module::Manager;
|
||||
|
||||
use crate::server::tree2::{Tree, Tree2Repr};
|
||||
|
||||
mod blobs;
|
||||
mod database;
|
||||
@@ -10,9 +16,9 @@ mod tree2;
|
||||
pub struct Server {
|
||||
pub component_configs: Vec<crate::config::ComponentState>,
|
||||
// pub interface: InterfaceWrapper,
|
||||
// pub manager: Arc<Mutex<Manager>>,
|
||||
pub manager: Arc<Mutex<Manager>>,
|
||||
pub db: sled::Db,
|
||||
pub tree: Tree2,
|
||||
// pub tree: Tree2,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
@@ -25,14 +31,34 @@ impl Server {
|
||||
|
||||
Ok(Self {
|
||||
component_configs,
|
||||
// manager: Manager::start(&SERVER_CONFIG, Vec::new()),
|
||||
manager: Manager::start(&crate::SERVER_CONFIG, Vec::new()),
|
||||
db: sled::open(database)?,
|
||||
tree: Tree2::default(),
|
||||
// tree: Tree2::default(),
|
||||
// interface: get_test_interface(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Tree for Server {
|
||||
fn is_folder() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn get_children_string(&self) -> Vec<String> {
|
||||
vec!["connection_count".into()]
|
||||
}
|
||||
|
||||
fn select_child(&self, child: &str) -> Result<Tree2Repr, String> {
|
||||
match child {
|
||||
"connection_count" => Ok(Tree2Repr::File(format!(
|
||||
"Connection count: {}",
|
||||
self.manager.lock().unwrap().connections.len()
|
||||
))),
|
||||
_ => Err("No such child".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Server {
|
||||
fn drop(&mut self) {
|
||||
self.db.flush().expect("Failed to flush database on drop");
|
||||
|
||||
@@ -10,89 +10,54 @@ use unshell_lib::debug;
|
||||
|
||||
use crate::{Server, api::CurrentUser};
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Tree2Branch {
|
||||
File(String),
|
||||
Folder(String, HashMap<String, Tree2Branch>),
|
||||
}
|
||||
pub trait Tree {
|
||||
fn is_folder() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
enum Tree2Repr {
|
||||
File(String),
|
||||
Folder(Vec<String>),
|
||||
}
|
||||
fn get_children_string(&self) -> Vec<String> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Tree2 {
|
||||
root: Tree2Branch,
|
||||
}
|
||||
fn select_child(&self, child: &str) -> Result<Tree2Repr, String>;
|
||||
|
||||
impl Tree2Branch {
|
||||
pub fn get_path(&self, elements: &Vec<&str>, depth: usize) -> Result<&Tree2Branch, String> {
|
||||
if depth == elements.len() {
|
||||
return Ok(self);
|
||||
}
|
||||
fn get_value(&self) -> String {
|
||||
"DEFAULT".into()
|
||||
}
|
||||
|
||||
let element = elements[depth];
|
||||
|
||||
if let Tree2Branch::Folder(_, hash_map) = self {
|
||||
if let Some(branch) = hash_map.get(element) {
|
||||
branch.get_path(elements, depth + 1)
|
||||
fn get_path(&self, elements: &mut Vec<&str>) -> Result<Tree2Repr, String> {
|
||||
if elements.is_empty() {
|
||||
return if Self::is_folder() {
|
||||
Ok(Tree2Repr::Folder(self.get_children_string()))
|
||||
} else {
|
||||
Err("Invalid Path".into())
|
||||
}
|
||||
Ok(Tree2Repr::File(self.get_value()))
|
||||
};
|
||||
}
|
||||
|
||||
let child = elements.remove(0);
|
||||
|
||||
if Self::is_folder() {
|
||||
self.select_child(child)
|
||||
} else {
|
||||
return Err("This is a folder, not a file".into());
|
||||
Err("This is a folder, not a file".into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_repr(&self) -> Tree2Repr {
|
||||
match self {
|
||||
Tree2Branch::File(name) => Tree2Repr::File(name.clone()),
|
||||
Tree2Branch::Folder(_, hash_map) => {
|
||||
Tree2Repr::Folder(hash_map.keys().cloned().collect::<Vec<String>>())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Tree2 {
|
||||
fn get(&self, path: &str) -> Result<Tree2Repr, String> {
|
||||
let elements = if path.is_empty() {
|
||||
let mut path = if path.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
path.split("/").collect::<Vec<&str>>()
|
||||
};
|
||||
|
||||
// let elements = path.split("/").collect::<Vec<&str>>();
|
||||
|
||||
let branch = self.root.get_path(&elements, 0)?;
|
||||
Ok(branch.to_repr())
|
||||
self.get_path(&mut path)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Tree2 {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
root: Tree2Branch::Folder(
|
||||
"ROOT".into(),
|
||||
HashMap::from([
|
||||
("File A".into(), Tree2Branch::File("A".into())),
|
||||
("File B".into(), Tree2Branch::File("B".into())),
|
||||
(
|
||||
"Folder C".into(),
|
||||
Tree2Branch::Folder(
|
||||
"A".into(),
|
||||
HashMap::from([
|
||||
("File A".into(), Tree2Branch::File("A".into())),
|
||||
("File B".into(), Tree2Branch::File("B".into())),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub enum Tree2Repr {
|
||||
File(String),
|
||||
Folder(Vec<String>),
|
||||
}
|
||||
|
||||
impl Server {
|
||||
@@ -104,7 +69,7 @@ impl Server {
|
||||
debug!("GET /api/interface/{}", path);
|
||||
|
||||
let result = (|| -> Result<_, String> {
|
||||
let interface = server.tree.get(&path)?;
|
||||
let interface = server.get(&path)?;
|
||||
|
||||
Ok(interface)
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user