95 lines
2.7 KiB
Rust
95 lines
2.7 KiB
Rust
pub mod entry;
|
|
|
|
mod settings;
|
|
|
|
pub mod status;
|
|
use crate::app::{
|
|
entry::Entry,
|
|
settings::Settings,
|
|
status::{CurrentScreen, EditingField, EntryCreation},
|
|
};
|
|
|
|
pub struct AppState {
|
|
pub fromIP: String,
|
|
pub fromPort: String,
|
|
pub toIP: String,
|
|
pub toPort: String,
|
|
pub screen: CurrentScreen,
|
|
pub currentlyEditing: Option<EditingField>,
|
|
pub entries: Vec<Entry>,
|
|
pub confDir: String,
|
|
}
|
|
|
|
impl AppState {
|
|
pub fn new(confDir: String) -> Self {
|
|
let settings = Settings::new(&confDir);
|
|
AppState {
|
|
fromIP: String::new(),
|
|
fromPort: String::new(),
|
|
toIP: String::new(),
|
|
toPort: String::new(),
|
|
currentlyEditing: None,
|
|
screen: CurrentScreen::Main,
|
|
entries: settings.entries,
|
|
confDir,
|
|
}
|
|
}
|
|
|
|
pub fn store(&mut self) -> EntryCreation {
|
|
match Entry::new(
|
|
self.fromIP.clone(),
|
|
self.toIP.clone(),
|
|
self.fromPort.clone(),
|
|
self.toPort.clone(),
|
|
) {
|
|
Ok(entry) => {
|
|
self.entries.push(entry);
|
|
self.fromIP = String::new();
|
|
self.toIP = String::new();
|
|
self.fromPort = String::new();
|
|
self.toPort = String::new();
|
|
self.currentlyEditing = None;
|
|
|
|
EntryCreation::Success
|
|
}
|
|
_ => EntryCreation::PortValidationError,
|
|
}
|
|
}
|
|
|
|
pub fn nextField(&mut self) {
|
|
if let Some(currentField) = &self.currentlyEditing {
|
|
self.currentlyEditing = match currentField {
|
|
EditingField::FromIP => Some(EditingField::FromPort),
|
|
EditingField::FromPort => Some(EditingField::ToIP),
|
|
EditingField::ToIP => Some(EditingField::ToPort),
|
|
EditingField::ToPort => Some(EditingField::FromIP),
|
|
};
|
|
}
|
|
}
|
|
|
|
pub fn prevField(&mut self) {
|
|
if let Some(currentField) = &self.currentlyEditing {
|
|
self.currentlyEditing = match currentField {
|
|
EditingField::FromIP => Some(EditingField::ToPort),
|
|
EditingField::FromPort => Some(EditingField::FromIP),
|
|
EditingField::ToIP => Some(EditingField::FromPort),
|
|
EditingField::ToPort => Some(EditingField::ToIP),
|
|
};
|
|
}
|
|
}
|
|
|
|
pub fn print(&self) {
|
|
let tempEntry = &self.entries[0];
|
|
let res = serde_json::to_string(tempEntry).unwrap();
|
|
println!("{res}");
|
|
let resString = tempEntry.to_string();
|
|
println!("{resString}");
|
|
}
|
|
|
|
pub fn save(&self) {
|
|
let mut settings = Settings::new(&self.confDir);
|
|
settings.entries = self.entries.clone();
|
|
settings.save(&self.confDir);
|
|
}
|
|
}
|