minor improvements for better 'rustifying' of it

This commit is contained in:
Trevor Merritt 2025-04-30 20:31:19 -04:00
parent 21b41f5593
commit 022359d3fd

View File

@ -1,9 +1,14 @@
use crate::tui::ratatui_screens::RatatuiScreens;
use crate::manager::Manager; use crate::manager::Manager;
use crate::ping_result::PingResult; use crate::ping_result::PingResult;
use crate::target_state::TargetState; use crate::target_state::TargetState;
use crate::tui::mode_adding::RatatuiAddingMode;
use crate::tui::mode_deleting::RatatuiDeletingMode;
use crate::tui::mode_editing::RatatuiEditingMode;
use crate::tui::mode_monitoring::RatatuiMonitoringMode;
use crate::tui::ratatui_screens::RatatuiScreens;
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
use color_eyre::Result; use color_eyre::Result;
use log::debug;
use ratatui::prelude::*; use ratatui::prelude::*;
use ratatui::{DefaultTerminal, Frame}; use ratatui::{DefaultTerminal, Frame};
use std::collections::BTreeMap; use std::collections::BTreeMap;
@ -15,11 +20,6 @@ use std::sync::mpsc;
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
use std::thread; use std::thread;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
use log::debug;
use crate::tui::mode_adding::RatatuiAddingMode;
use crate::tui::mode_deleting::RatatuiDeletingMode;
use crate::tui::mode_editing::RatatuiEditingMode;
use crate::tui::mode_monitoring::RatatuiMonitoringMode;
#[derive(Default)] #[derive(Default)]
pub struct RatatuiApp { pub struct RatatuiApp {
@ -33,12 +33,11 @@ pub struct RatatuiApp {
pub trying_to_exit: bool, pub trying_to_exit: bool,
pub trying_to_delete: bool, pub trying_to_delete: bool,
pub add_host_cursor_position: usize, pub add_host_cursor_position: usize,
pub add_host_name: String pub add_host_name: String,
} }
/// Private Methods /// Private Methods
impl RatatuiApp { impl RatatuiApp {
pub fn set_filename(mut self, new_filename: String) { pub fn set_filename(mut self, new_filename: String) {
self.filename = Some(new_filename) self.filename = Some(new_filename)
} }
@ -61,10 +60,8 @@ impl RatatuiApp {
// check for any waiting ping results... // check for any waiting ping results...
self.consume_waiting_results(&receiver); self.consume_waiting_results(&receiver);
terminal terminal.draw(|frame| RatatuiMonitoringMode::render(frame, &mut self))?;
.draw(|frame| RatatuiMonitoringMode::render(frame, &mut self))?;
RatatuiMonitoringMode::handle_crossterm_events(&mut self)?; RatatuiMonitoringMode::handle_crossterm_events(&mut self)?;
} }
Ok(()) Ok(())
} }
@ -133,29 +130,36 @@ impl RatatuiApp {
fn get_default_targets() -> BTreeMap<String, TargetState> { fn get_default_targets() -> BTreeMap<String, TargetState> {
let mut working = BTreeMap::new(); let mut working = BTreeMap::new();
working.insert("1111 DNS".to_string(), TargetState { working.insert(
name: "1111 DNS".to_string(), "1111 DNS".to_string(),
target: Ipv4Addr::new(1, 1, 1, 1), TargetState {
..TargetState::default() name: "1111 DNS".to_string(),
}); target: Ipv4Addr::new(1, 1, 1, 1),
working.insert("Google DNS".to_string(), TargetState { ..TargetState::default()
name: "Google DNS".to_string(), },
target: Ipv4Addr::new(8, 8, 8, 8), );
..TargetState::default() working.insert(
}); "Google DNS".to_string(),
working.insert("Test Site 1".to_string(), TargetState { TargetState {
name: "Test Site 1".to_string(), name: "Google DNS".to_string(),
target: Ipv4Addr::new(216, 234, 202, 122), target: Ipv4Addr::new(8, 8, 8, 8),
..TargetState::default() ..TargetState::default()
}); },
);
working.insert(
"Test Site 1".to_string(),
TargetState {
name: "Test Site 1".to_string(),
target: Ipv4Addr::new(216, 234, 202, 122),
..TargetState::default()
},
);
working working
} }
} }
/// Public Methods /// Public Methods
impl RatatuiApp { impl RatatuiApp {
fn load_hosts_from_file(file_to_load_from: PathBuf) -> BTreeMap<String, TargetState> { fn load_hosts_from_file(file_to_load_from: PathBuf) -> BTreeMap<String, TargetState> {
let mut working = BTreeMap::new(); let mut working = BTreeMap::new();
@ -163,11 +167,14 @@ impl RatatuiApp {
let mut rdr = csv::Reader::from_reader(the_file.unwrap()); let mut rdr = csv::Reader::from_reader(the_file.unwrap());
for result in rdr.records() { for result in rdr.records() {
let record = result.unwrap(); let record = result.unwrap();
working.insert(record[1].to_string(), TargetState { working.insert(
name: record[1].to_string(), record[1].to_string(),
target: Ipv4Addr::from_str(&record[0]).unwrap(), TargetState {
..TargetState::default() name: record[1].to_string(),
}); target: Ipv4Addr::from_str(&record[0]).unwrap(),
..TargetState::default()
},
);
} }
working working
@ -180,17 +187,17 @@ impl RatatuiApp {
working.filename = Some(file.as_os_str().to_string_lossy().parse().unwrap()); working.filename = Some(file.as_os_str().to_string_lossy().parse().unwrap());
} else { } else {
// working.log_event("Passed file doesnt exist looking for hosts.txt".to_string()); // working.log_event("Passed file doesnt exist looking for hosts.txt".to_string());
if !&PathBuf::from_str("hosts.txt").unwrap().exists() { if PathBuf::from_str("hosts.txt").unwrap().exists() {
// working.log_event("Didnt find hosts.txt".to_string());
working.filename = None;
} else {
// working.log_event("Found hosts.txt. using it as the default".to_string()); // working.log_event("Found hosts.txt. using it as the default".to_string());
working.filename = Some("hosts.txt".to_string()); working.filename = Some("hosts.txt".to_string());
} else {
// working.log_event("Didnt find hosts.txt".to_string());
working.filename = None;
} }
} }
if let Some(file) = working.filename.clone() { if let Some(file) = working.filename.clone() {
RatatuiApp::load_hosts_from_file(PathBuf::from_str(&*working.filename.clone().unwrap()).unwrap()) RatatuiApp::load_hosts_from_file(PathBuf::from_str(file.as_str()).unwrap())
} else { } else {
RatatuiApp::get_default_targets() RatatuiApp::get_default_targets()
} }
@ -199,7 +206,9 @@ impl RatatuiApp {
working.filename = Some("hosts.txt".to_string()); working.filename = Some("hosts.txt".to_string());
if let Some(ref hosts_file) = working.filename { if let Some(ref hosts_file) = working.filename {
if PathBuf::from_str(hosts_file).unwrap().exists() { if PathBuf::from_str(hosts_file).unwrap().exists() {
RatatuiApp::load_hosts_from_file(PathBuf::from_str(&*hosts_file.clone()).unwrap()) RatatuiApp::load_hosts_from_file(
PathBuf::from_str(&*hosts_file.clone()).unwrap(),
)
} else { } else {
working.filename = None; working.filename = None;
RatatuiApp::get_default_targets() RatatuiApp::get_default_targets()
@ -228,10 +237,11 @@ impl RatatuiApp {
} }
pub fn get_log_entries(&self, how_many: u32) -> Vec<String> { pub fn get_log_entries(&self, how_many: u32) -> Vec<String> {
let mut return_value = vec![]; self.log_entries
for current in self.log_entries.clone().into_iter().rev() { .iter()
return_value.push(current); .rev()
} .take(how_many as usize)
return_value .cloned()
.collect()
} }
} }