completed app.rs

This commit is contained in:
2025-06-26 16:56:10 +03:00
parent 548ca8400a
commit 97f31f95f4
3 changed files with 759 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
pub enum CurrentScreen {
Main,
Editing,
Exiting,
}
pub enum CurrentlyEditing {
Key,
Value,
}
pub struct App {
pub key_input: String,
pub value_input: String,
pub pairs: HashMap<String, String>,
pub current_screen: CurrentScreen,
pub currently_editing: Option<CurrentlyEditing>,
}
impl App {
pub fn new() -> App {
App {
key_input: String::new(),
value_input: String::new(),
pairs: HashMap::new(),
current_screen: CurrentScreen::Main,
currently_editing: None,
}
}
pub fn save_key_value(&mut self) {
self.pairs
.insert(self.key_input.clone(), self.value_input.clone());
self.key_input = String::new();
self.value_input = String::new();
self.currently_editing = None;
}
pub fn toggle_editing(&mut self) {
if let Some(edit_mode) = &self.currently_editing {
match edit_mode {
CurrentlyEditing::Key => self.currently_editing = Some(CurrentlyEditing::Value),
CurrentlyEditing::Value => self.currently_editing = Some(CurrentlyEditing::Key),
};
} else {
self.currently_editing = Some(CurrentlyEditing::Key)
}
}
pub fn print_json(&self) -> serde::Result<()> {
let output = serde_json::to_string(&self.pairs)?;
println!("{}", output);
Ok(())
}
}