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:
@@ -58,8 +58,6 @@ impl InterfaceWindow {
|
|||||||
let mut state_lock = self.state.lock().unwrap();
|
let mut state_lock = self.state.lock().unwrap();
|
||||||
|
|
||||||
state_lock.is_request = true;
|
state_lock.is_request = true;
|
||||||
|
|
||||||
drop(state_lock)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let state_clone = self.state.clone();
|
let state_clone = self.state.clone();
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ pub static DEFAULT_HOST: String = "localhost".to_string();
|
|||||||
#[static_init::dynamic]
|
#[static_init::dynamic]
|
||||||
pub static DATABASE_NAME: String = "database".to_string();
|
pub static DATABASE_NAME: String = "database".to_string();
|
||||||
|
|
||||||
// #[static_init::dynamic]
|
#[static_init::dynamic]
|
||||||
// pub static SERVER_CONFIG: unshell_lib::config::PayloadConfig = unshell_lib::config::PayloadConfig {
|
pub static SERVER_CONFIG: unshell_lib::config::PayloadConfig = unshell_lib::config::PayloadConfig {
|
||||||
// id: "Server",
|
id: "Server",
|
||||||
// components: unshell_lib::get_components(),
|
components: Vec::new(),
|
||||||
// runtime_config: 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 blobs;
|
||||||
mod database;
|
mod database;
|
||||||
@@ -10,9 +16,9 @@ mod tree2;
|
|||||||
pub struct Server {
|
pub struct Server {
|
||||||
pub component_configs: Vec<crate::config::ComponentState>,
|
pub component_configs: Vec<crate::config::ComponentState>,
|
||||||
// pub interface: InterfaceWrapper,
|
// pub interface: InterfaceWrapper,
|
||||||
// pub manager: Arc<Mutex<Manager>>,
|
pub manager: Arc<Mutex<Manager>>,
|
||||||
pub db: sled::Db,
|
pub db: sled::Db,
|
||||||
pub tree: Tree2,
|
// pub tree: Tree2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
@@ -25,14 +31,34 @@ impl Server {
|
|||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
component_configs,
|
component_configs,
|
||||||
// manager: Manager::start(&SERVER_CONFIG, Vec::new()),
|
manager: Manager::start(&crate::SERVER_CONFIG, Vec::new()),
|
||||||
db: sled::open(database)?,
|
db: sled::open(database)?,
|
||||||
tree: Tree2::default(),
|
// tree: Tree2::default(),
|
||||||
// interface: get_test_interface(),
|
// 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 {
|
impl Drop for Server {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.db.flush().expect("Failed to flush database on drop");
|
self.db.flush().expect("Failed to flush database on drop");
|
||||||
|
|||||||
@@ -10,89 +10,54 @@ use unshell_lib::debug;
|
|||||||
|
|
||||||
use crate::{Server, api::CurrentUser};
|
use crate::{Server, api::CurrentUser};
|
||||||
|
|
||||||
#[derive(Clone)]
|
pub trait Tree {
|
||||||
enum Tree2Branch {
|
fn is_folder() -> bool {
|
||||||
File(String),
|
false
|
||||||
Folder(String, HashMap<String, Tree2Branch>),
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize)]
|
fn get_children_string(&self) -> Vec<String> {
|
||||||
enum Tree2Repr {
|
Vec::new()
|
||||||
File(String),
|
}
|
||||||
Folder(Vec<String>),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
fn select_child(&self, child: &str) -> Result<Tree2Repr, String>;
|
||||||
pub struct Tree2 {
|
|
||||||
root: Tree2Branch,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Tree2Branch {
|
fn get_value(&self) -> String {
|
||||||
pub fn get_path(&self, elements: &Vec<&str>, depth: usize) -> Result<&Tree2Branch, String> {
|
"DEFAULT".into()
|
||||||
if depth == elements.len() {
|
}
|
||||||
return Ok(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
let element = elements[depth];
|
fn get_path(&self, elements: &mut Vec<&str>) -> Result<Tree2Repr, String> {
|
||||||
|
if elements.is_empty() {
|
||||||
if let Tree2Branch::Folder(_, hash_map) = self {
|
return if Self::is_folder() {
|
||||||
if let Some(branch) = hash_map.get(element) {
|
Ok(Tree2Repr::Folder(self.get_children_string()))
|
||||||
branch.get_path(elements, depth + 1)
|
|
||||||
} else {
|
} 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 {
|
} 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> {
|
fn get(&self, path: &str) -> Result<Tree2Repr, String> {
|
||||||
let elements = if path.is_empty() {
|
let mut path = if path.is_empty() {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
} else {
|
} else {
|
||||||
path.split("/").collect::<Vec<&str>>()
|
path.split("/").collect::<Vec<&str>>()
|
||||||
};
|
};
|
||||||
|
|
||||||
// let elements = path.split("/").collect::<Vec<&str>>();
|
self.get_path(&mut path)
|
||||||
|
|
||||||
let branch = self.root.get_path(&elements, 0)?;
|
|
||||||
Ok(branch.to_repr())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Tree2 {
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
fn default() -> Self {
|
pub enum Tree2Repr {
|
||||||
Self {
|
File(String),
|
||||||
root: Tree2Branch::Folder(
|
Folder(Vec<String>),
|
||||||
"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())),
|
|
||||||
]),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
@@ -104,7 +69,7 @@ impl Server {
|
|||||||
debug!("GET /api/interface/{}", path);
|
debug!("GET /api/interface/{}", path);
|
||||||
|
|
||||||
let result = (|| -> Result<_, String> {
|
let result = (|| -> Result<_, String> {
|
||||||
let interface = server.tree.get(&path)?;
|
let interface = server.get(&path)?;
|
||||||
|
|
||||||
Ok(interface)
|
Ok(interface)
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user