mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
Work on customizable config tool
This commit is contained in:
@@ -1,18 +1,27 @@
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
pub struct PayloadConfig {}
|
||||
// use crate::payload_config::structs::ConfigStructField;
|
||||
|
||||
struct ServerConfigState {
|
||||
// config: Vec<PayloadConfig>
|
||||
mod structs;
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
pub struct PayloadConfig {
|
||||
config_struct: structs::Config,
|
||||
}
|
||||
|
||||
// struct ServerConfigState {
|
||||
// // config: Vec<PayloadConfig>
|
||||
// }
|
||||
|
||||
impl PayloadConfig {
|
||||
pub fn update(&mut self, ui: &mut egui::Ui) {
|
||||
ui.heading("Test");
|
||||
// ui.heading("Test");
|
||||
self.config_struct.update(ui);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PayloadConfig {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
Self {
|
||||
config_struct: structs::default_configurable(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use egui::TextEdit;
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
enum ConfigStructField {
|
||||
Header(String),
|
||||
Text(String),
|
||||
String {
|
||||
default: Option<String>,
|
||||
max_length: Option<usize>,
|
||||
protected: bool,
|
||||
},
|
||||
Integer {
|
||||
default: i32,
|
||||
min: Option<usize>,
|
||||
max: Option<usize>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
enum ConfigStructValue {
|
||||
String(String),
|
||||
Integer(i32),
|
||||
}
|
||||
|
||||
// #[derive(serde::Deserialize, serde::Serialize)]
|
||||
// struct ConfigStruct {
|
||||
// id: String,
|
||||
// field: ConfigStructField,
|
||||
// }
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
pub struct Config {
|
||||
config: Vec<(String, ConfigStructField)>,
|
||||
state: HashMap<String, ConfigStructValue>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn new(config: Vec<(String, ConfigStructField)>) -> Self {
|
||||
Self {
|
||||
config,
|
||||
state: HashMap::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, ui: &mut egui::Ui) {
|
||||
for (id, field) in &self.config {
|
||||
match field {
|
||||
ConfigStructField::Header(text) => {
|
||||
ui.heading(text);
|
||||
}
|
||||
ConfigStructField::Text(text) => {
|
||||
ui.label(text);
|
||||
}
|
||||
ConfigStructField::String {
|
||||
default,
|
||||
max_length,
|
||||
protected,
|
||||
} => {
|
||||
let value =
|
||||
if let Some(ConfigStructValue::String(value)) = self.state.get_mut(id) {
|
||||
value
|
||||
} else {
|
||||
self.state.insert(
|
||||
id.clone(),
|
||||
ConfigStructValue::String(default.clone().unwrap_or(String::new())),
|
||||
);
|
||||
if let Some(ConfigStructValue::String(value)) = self.state.get_mut(id) {
|
||||
value
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
};
|
||||
|
||||
let mut widget = TextEdit::singleline(value).password(*protected);
|
||||
|
||||
if let Some(limit) = &max_length {
|
||||
widget = widget.char_limit(*limit);
|
||||
}
|
||||
|
||||
ui.add(widget);
|
||||
}
|
||||
_ => {} // ConfigStructField::Integer { default, min, max } => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
// match &self.field {
|
||||
// ConfigStructField::Header(text) => {
|
||||
// ui.heading(text);
|
||||
// }
|
||||
// ConfigStructField::Text(text) => {
|
||||
// ui.label(text);
|
||||
// }
|
||||
// ConfigStructField::String {
|
||||
// default,
|
||||
// max_length,
|
||||
// protected,
|
||||
// } => ui.text_edit_singleline(),
|
||||
// ConfigStructField::Integer { default, min, max } => todo!(),
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_configurable() -> Config {
|
||||
Config::new(vec![
|
||||
(
|
||||
"Header".into(),
|
||||
ConfigStructField::Header("Test Header!".into()),
|
||||
),
|
||||
("text".into(), ConfigStructField::Text("Test Text!".into())),
|
||||
(
|
||||
"Config".into(),
|
||||
ConfigStructField::String {
|
||||
default: Some("Test String".into()),
|
||||
max_length: Some(30),
|
||||
protected: false,
|
||||
},
|
||||
),
|
||||
(
|
||||
"Protected".into(),
|
||||
ConfigStructField::String {
|
||||
default: Some("Protected String".into()),
|
||||
max_length: None,
|
||||
protected: true,
|
||||
},
|
||||
),
|
||||
])
|
||||
}
|
||||
Reference in New Issue
Block a user