From 95bfff193494a0cc6ac4bc50b6bd5773a2742aa5 Mon Sep 17 00:00:00 2001 From: Imbris <2002109+Imberflur@users.noreply.github.com> Date: Fri, 28 Apr 2023 10:53:46 -0400 Subject: [PATCH] fix(errors): add file path context to all IO errors in ConfigError (#2412) --- zellij-utils/src/input/config.rs | 3 --- zellij-utils/src/input/layout.rs | 4 +++- zellij-utils/src/kdl/mod.rs | 13 ++++++------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/zellij-utils/src/input/config.rs b/zellij-utils/src/input/config.rs index 8022ea28..4ad4ef89 100644 --- a/zellij-utils/src/input/config.rs +++ b/zellij-utils/src/input/config.rs @@ -83,9 +83,6 @@ pub enum ConfigError { KdlDeserializationError(#[from] kdl::KdlError), #[error("KdlDeserialization error: {0}")] KdlError(KdlError), // TODO: consolidate these - // Io error - #[error("IoError: {0}")] - Io(#[from] io::Error), #[error("Config error: {0}")] Std(#[from] Box), // Io error with path context diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index 713b2775..b4fcbe2c 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -730,7 +730,9 @@ impl Layout { let swap_layout_and_path = Layout::swap_layout_and_path(&layout_path); let mut kdl_layout = String::new(); - layout_file.read_to_string(&mut kdl_layout)?; + layout_file + .read_to_string(&mut kdl_layout) + .map_err(|e| ConfigError::IoPath(e, layout_path.into()))?; Ok(( layout_path.as_os_str().to_string_lossy().into(), kdl_layout, diff --git a/zellij-utils/src/kdl/mod.rs b/zellij-utils/src/kdl/mod.rs index 1d488f39..dceaaf36 100644 --- a/zellij-utils/src/kdl/mod.rs +++ b/zellij-utils/src/kdl/mod.rs @@ -10,8 +10,6 @@ use crate::input::theme::{FrameConfig, Theme, Themes, UiConfig}; use crate::setup::{find_default_config_dir, get_layout_dir}; use kdl_layout_parser::KdlLayoutParser; use std::collections::HashMap; -use std::fs::File; -use std::io::Read; use strum::IntoEnumIterator; use miette::NamedSource; @@ -1746,9 +1744,8 @@ impl Themes { pub fn from_path(path_to_theme_file: PathBuf) -> Result { // String is the theme name - let mut file = File::open(path_to_theme_file.clone())?; - let mut kdl_config = String::new(); - file.read_to_string(&mut kdl_config)?; + let kdl_config = std::fs::read_to_string(&path_to_theme_file) + .map_err(|e| ConfigError::IoPath(e, path_to_theme_file.into()))?; let kdl_config: KdlDocument = kdl_config.parse()?; let kdl_themes = kdl_config.get("themes").ok_or(ConfigError::new_kdl_error( "No theme node found in file".into(), @@ -1761,8 +1758,10 @@ impl Themes { pub fn from_dir(path_to_theme_dir: PathBuf) -> Result { let mut themes = Themes::default(); - for entry in std::fs::read_dir(path_to_theme_dir)? { - let entry = entry?; + for entry in std::fs::read_dir(&path_to_theme_dir) + .map_err(|e| ConfigError::IoPath(e, path_to_theme_dir.clone()))? + { + let entry = entry.map_err(|e| ConfigError::IoPath(e, path_to_theme_dir.clone()))?; let path = entry.path(); if let Some(extension) = path.extension() { if extension == "kdl" {