From c77752c05c1e11b692d18aa7964b4446d7694586 Mon Sep 17 00:00:00 2001 From: Penwing Date: Fri, 26 Jan 2024 08:07:47 +0100 Subject: [PATCH] better error handeling when opening/reloading files --- Cargo.lock | 1 - Cargo.toml | 1 - src/main.rs | 2 +- src/tools/mod.rs | 23 ----------------------- src/tools/tabs.rs | 39 ++++++++++++++++++++++++++++++--------- 5 files changed, 31 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4edfff..4c44fa9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -579,7 +579,6 @@ dependencies = [ "rfd", "serde", "serde_json", - "toml", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 979c019..51b5313 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -toml = "0.8.8" eframe = "0.25.0" rfd = "0.12.1" egui_extras = "0.25.0" diff --git a/src/main.rs b/src/main.rs index e211bd2..321c5ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,7 +56,7 @@ fn main() -> Result<(), eframe::Error> { }; eframe::run_native( - &format!("Calcifer{}{}", tools::version(), TITLE), + &format!("Calcifer{}", TITLE), options, Box::new(move |_cc| Box::from(Calcifer::from_app_state(app_state))), ) diff --git a/src/tools/mod.rs b/src/tools/mod.rs index c502e9f..0ec0f2d 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -6,7 +6,6 @@ use std::{ cmp::Ordering, ffi::OsStr, fs, fs::read_to_string, fs::OpenOptions, io::Write, path::Component, path::Path, path::PathBuf, }; -use toml::Value; //my tools; pub mod confirm; @@ -115,27 +114,5 @@ pub fn format_path(path: &Path) -> String { ) } -pub fn version() -> String { - // Read the contents of the Cargo.toml file - if !Path::new("Cargo.toml").exists() { - return "".to_string(); - } - let toml_content = fs::read_to_string("Cargo.toml").expect("Failed to read Cargo.toml"); - - // Parse the TOML content - let toml: Value = toml::from_str(&toml_content).expect("Failed to parse TOML"); - - // Extract version information - if let Some(package) = toml.get("package") { - if let Some(version) = package.get("version") { - if let Some(version_string) = version.as_str() { - println!("Version: {}", version_string); - return format!(" v{}", version_string); - } - } - } - "".to_string() -} - #[cfg(test)] mod tests; diff --git a/src/tools/tabs.rs b/src/tools/tabs.rs index 8aeac2d..2f27f4d 100644 --- a/src/tools/tabs.rs +++ b/src/tools/tabs.rs @@ -50,17 +50,23 @@ impl Default for Tab { impl Tab { pub fn new(path: PathBuf) -> Self { + let text = read_file_contents(&path).replace(&" ".repeat(4), "\t"); + let file_path = format_file_path(&path, &text); + let extension = file_path + .extension() + .and_then(|ext| ext.to_str()) + .unwrap_or_default(); + Self { - path: path.clone(), - code: read_to_string(path.clone()) - .expect("Not able to read the file") - .replace(&" ".repeat(4), "\t"), - language: path.to_str().unwrap().split('.').last().unwrap().into(), + path: file_path.clone(), + code: text, + language: extension.into(), saved: true, scroll_offset: 0.0, last_cursor: None, } } + pub fn get_name(&self) -> String { self.path .file_name() @@ -70,10 +76,25 @@ impl Tab { } pub fn refresh(&mut self) { - self.code = read_to_string(self.path.clone()) - .expect("Not able to read the file") - .replace(&" ".repeat(4), "\t"); + let text = read_file_contents(&self.path).replace(&" ".repeat(4), "\t"); + let file_path = format_file_path(&self.path, &text); + + self.code = text; + self.path = file_path; self.saved = true; - println!("refreshed {}", self.path.display()); + } +} + +fn read_file_contents(path: &PathBuf) -> String { + read_to_string(path.clone()) + .map_err(|err| format!("// Error reading file: {}", err)) + .unwrap_or_else(|err_msg| err_msg) +} + +fn format_file_path(path: &PathBuf, contents: &str) -> PathBuf { + if contents.contains("Error reading file") { + "untitled".into() + } else { + path.clone() } }