From de29f8292af3f711285e16f57be7b4d9b066ea4d Mon Sep 17 00:00:00 2001 From: Yahddyyp Date: Wed, 22 Jul 2026 19:11:13 +0530 Subject: [PATCH] feat: Made TOML parse error message that omits the config file path --- yazi-config/src/keymap/keymap.rs | 9 +++++---- yazi-config/src/lib.rs | 28 ++++++++++++++++++++-------- yazi-config/src/theme/flavor.rs | 8 +++++--- yazi-config/src/theme/theme.rs | 7 ++++--- yazi-config/src/vfs/vfs.rs | 9 +++++---- yazi-config/src/yazi.rs | 9 ++++++--- 6 files changed, 45 insertions(+), 25 deletions(-) diff --git a/yazi-config/src/keymap/keymap.rs b/yazi-config/src/keymap/keymap.rs index c0fe0ff6..4ba19e65 100644 --- a/yazi-config/src/keymap/keymap.rs +++ b/yazi-config/src/keymap/keymap.rs @@ -1,4 +1,4 @@ -use std::sync::Arc; +use std::{path::PathBuf, sync::Arc}; use anyhow::{Context, Result}; use serde::Deserialize; @@ -49,9 +49,10 @@ impl Keymap { } impl Keymap { - pub(crate) fn read() -> Result { + pub(crate) fn read() -> Result<(PathBuf, String)> { let p = Xdg::config_dir().join("keymap.toml"); - ok_or_not_found(std::fs::read_to_string(&p)) - .with_context(|| format!("Failed to read keymap {p:?}")) + let s = ok_or_not_found(std::fs::read_to_string(&p)) + .with_context(|| format!("Failed to read keymap {p:?}"))?; + Ok((p, s)) } } diff --git a/yazi-config/src/lib.rs b/yazi-config/src/lib.rs index 6a42d274..f760cefe 100644 --- a/yazi-config/src/lib.rs +++ b/yazi-config/src/lib.rs @@ -4,6 +4,7 @@ yazi_macro::mod_flat!(icon inject layout mixing pattern platform preset priority use std::io::{Read, Write}; +use anyhow::Context; use yazi_macro::writef; use yazi_shim::{cell::{RoCell, SyncCell}, toml::{DeserializeOver, DeserializeOverWith}}; use yazi_tty::{TTY, sequence::SetSgr}; @@ -28,9 +29,14 @@ fn try_init(merge: bool) -> anyhow::Result<()> { let mut vfs = Preset::vfs()?; if merge { - yazi = yazi.deserialize_over(&yazi::Yazi::read()?)?; - keymap = keymap.deserialize_over(&keymap::Keymap::read()?)?; - vfs = vfs.deserialize_over(&vfs::Vfs::read()?)?; + let (p, s) = yazi::Yazi::read()?; + yazi = yazi.deserialize_over(&s).with_context(|| format!("TOML parse error in {p:?}"))?; + + let (p, s) = keymap::Keymap::read()?; + keymap = keymap.deserialize_over(&s).with_context(|| format!("TOML parse error in {p:?}"))?; + + let (p, s) = vfs::Vfs::read()?; + vfs = vfs.deserialize_over(&s).with_context(|| format!("TOML parse error in {p:?}"))?; } YAZI.init(yazi); @@ -56,16 +62,22 @@ pub fn build_flavor(light: bool, merge: bool) -> anyhow::Result { let mut preset = Preset::theme(light)?; if merge { - let theme_str = theme::Theme::read()?; - let theme = toml::de::DeTable::parse(&theme_str)?; + let (theme_p, theme_str) = theme::Theme::read()?; + let theme = toml::de::DeTable::parse(&theme_str) + .with_context(|| format!("TOML parse error in {theme_p:?}"))?; - let flavor_str = theme::Flavor::from_theme(&theme, &theme_str)?.read(light)?; + let (flavor_p, flavor_str) = theme::Flavor::from_theme(&theme, &theme_str) + .with_context(|| format!("TOML parse error in {theme_p:?}"))? + .read(light)?; - preset = preset.deserialize_over(&flavor_str)?; + preset = preset.deserialize_over(&flavor_str).with_context(|| { + format!("TOML parse error in {:?}", flavor_p.unwrap_or_else(|| theme_p.clone())) + })?; preset = error_with_input( preset.deserialize_over_with(toml::de::Deserializer::from(theme)), &theme_str, - )?; + ) + .with_context(|| format!("TOML parse error in {theme_p:?}"))?; } preset.reshape(light) diff --git a/yazi-config/src/theme/flavor.rs b/yazi-config/src/theme/flavor.rs index 6ae7bb6f..a9330747 100644 --- a/yazi-config/src/theme/flavor.rs +++ b/yazi-config/src/theme/flavor.rs @@ -27,12 +27,14 @@ impl Flavor { } } - pub(crate) fn read(&self, light: bool) -> Result { + pub(crate) fn read(&self, light: bool) -> Result<(Option, String)> { Ok(match if light { self.light.load() } else { self.dark.load() }.as_str() { - "" => String::new(), + "" => (None, String::new()), name => { let p = Xdg::config_dir().join(format!("flavors/{name}.yazi/flavor.toml")); - std::fs::read_to_string(&p).with_context(|| format!("Failed to read flavor {p:?}"))? + let s = + std::fs::read_to_string(&p).with_context(|| format!("Failed to read flavor {p:?}"))?; + (Some(p), s) } }) } diff --git a/yazi-config/src/theme/theme.rs b/yazi-config/src/theme/theme.rs index 4998eae1..134c7b4e 100644 --- a/yazi-config/src/theme/theme.rs +++ b/yazi-config/src/theme/theme.rs @@ -40,10 +40,11 @@ pub struct Theme { } impl Theme { - pub(crate) fn read() -> Result { + pub(crate) fn read() -> Result<(PathBuf, String)> { let p = Xdg::config_dir().join("theme.toml"); - ok_or_not_found(std::fs::read_to_string(&p)) - .with_context(|| format!("Failed to read theme {p:?}")) + let s = ok_or_not_found(std::fs::read_to_string(&p)) + .with_context(|| format!("Failed to read theme {p:?}"))?; + Ok((p, s)) } // FIXME: remove diff --git a/yazi-config/src/vfs/vfs.rs b/yazi-config/src/vfs/vfs.rs index 902b8357..491c6f8b 100644 --- a/yazi-config/src/vfs/vfs.rs +++ b/yazi-config/src/vfs/vfs.rs @@ -1,4 +1,4 @@ -use std::io; +use std::{io, path::PathBuf}; use anyhow::{Context, Result}; use serde::{Deserialize, Deserializer}; @@ -31,10 +31,11 @@ impl Vfs { } } - pub(crate) fn read() -> Result { + pub(crate) fn read() -> Result<(PathBuf, String)> { let p = Xdg::config_dir().join("vfs.toml"); - ok_or_not_found(std::fs::read_to_string(&p)) - .with_context(|| format!("Failed to read config {p:?}")) + let s = ok_or_not_found(std::fs::read_to_string(&p)) + .with_context(|| format!("Failed to read config {p:?}"))?; + Ok((p, s)) } } diff --git a/yazi-config/src/yazi.rs b/yazi-config/src/yazi.rs index 766629ea..ec07e464 100644 --- a/yazi-config/src/yazi.rs +++ b/yazi-config/src/yazi.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use anyhow::{Context, Result}; use serde::Deserialize; use yazi_codegen::{DeserializeOver, DeserializeOver1}; @@ -20,9 +22,10 @@ pub struct Yazi { } impl Yazi { - pub(super) fn read() -> Result { + pub(super) fn read() -> Result<(PathBuf, String)> { let p = Xdg::config_dir().join("yazi.toml"); - ok_or_not_found(std::fs::read_to_string(&p)) - .with_context(|| format!("Failed to read config {p:?}")) + let s = ok_or_not_found(std::fs::read_to_string(&p)) + .with_context(|| format!("Failed to read config {p:?}"))?; + Ok((p, s)) } }