fix: remove panics and expects, improve error handling
This commit is contained in:
1 parent
cc24c0a1d8
commit
9df5e9a78a
2 files changed
+70
-16
No files matched your search
+56
-11
@@ -55,22 +55,22 @@ pub fn parse_color(color: &str) -> Color {
|
||||
"reset" => Color::Reset,
|
||||
hex if hex.starts_with('#') && hex.len() == 7 => {
|
||||
let r = u8::from_str_radix(&hex[1..3], 16).unwrap_or_else(|_| {
|
||||
eprintln!("Invalid hex color format: {}", color);
|
||||
tracing::warn!("Invalid hex color format: {}", color);
|
||||
255
|
||||
});
|
||||
let g = u8::from_str_radix(&hex[3..5], 16).unwrap_or_else(|_| {
|
||||
eprintln!("Invalid hex color format: {}", color);
|
||||
tracing::warn!("Invalid hex color format: {}", color);
|
||||
255
|
||||
});
|
||||
let b = u8::from_str_radix(&hex[5..7], 16).unwrap_or_else(|_| {
|
||||
eprintln!("Invalid hex color format: {}", color);
|
||||
tracing::warn!("Invalid hex color format: {}", color);
|
||||
255
|
||||
});
|
||||
Color::Rgb(r, g, b)
|
||||
}
|
||||
_ => {
|
||||
eprintln!("Invalid color name or hex value: {}", color);
|
||||
panic!("Invalid config");
|
||||
tracing::warn!("Unknown color '{}', using White as fallback", color);
|
||||
Color::White
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,16 +79,61 @@ fn deserialize_color<'de, D>(deserializer: D) -> Result<Color, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let color_str: String = Deserialize::deserialize(deserializer)?;
|
||||
let color_str: String = match Deserialize::deserialize(deserializer) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to deserialize color field: {}", e);
|
||||
return Ok(Color::White);
|
||||
}
|
||||
};
|
||||
Ok(parse_color(&color_str))
|
||||
}
|
||||
|
||||
impl Default for Colors {
|
||||
fn default() -> Self {
|
||||
Colors {
|
||||
background: Color::Reset,
|
||||
foreground: Color::White,
|
||||
border_focused: Color::White,
|
||||
border_unfocused: Color::DarkGray,
|
||||
row_highlight: Color::White,
|
||||
row_background: Color::Reset,
|
||||
row_alternate_bg: Color::Reset,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ThemeConfig {
|
||||
fn default() -> Self {
|
||||
ThemeConfig {
|
||||
colors: Colors::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub static THEME: Lazy<ThemeConfig> = Lazy::new(|| {
|
||||
let config_path = get_config_path().join("config.toml");
|
||||
ConfigLoader::builder()
|
||||
.add_source(File::from(config_path))
|
||||
|
||||
let built = match ConfigLoader::builder()
|
||||
.add_source(File::from(config_path.clone()).required(false))
|
||||
.build()
|
||||
.expect("Failed to load theme config")
|
||||
.try_deserialize()
|
||||
.expect("Failed to parse theme config")
|
||||
{
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
"Failed to load theme config from '{}': {}. Using default colors.",
|
||||
config_path.display(),
|
||||
e
|
||||
);
|
||||
return ThemeConfig::default();
|
||||
}
|
||||
};
|
||||
|
||||
match built.try_deserialize::<ThemeConfig>() {
|
||||
Ok(theme) => theme,
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to parse theme config: {}. Using default colors.", e);
|
||||
ThemeConfig::default()
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user