fix: remove panics and expects, improve error handling

This commit is contained in:
objz committed 2025-07-26 21:17:52 +02:00
1 parent cc24c0a1d8
commit 9df5e9a78a
2 files changed
+70 -16

No files matched your search

+14 -5
View File
@@ -1,7 +1,7 @@
use crate::tui;
use clap::{Arg, ArgAction, Command};
pub fn init() {
pub async fn init() {
let matches = Command::new("mcl")
.about("Minecraft CLI Launcher")
.version("1.0.0")
@@ -79,14 +79,23 @@ pub fn init() {
.get_matches();
if matches.subcommand().is_none() {
tui::show().unwrap()
match tui::show().await {
Ok(_) => {}
Err(e) => {
tracing::error!("TUI error: {}", e);
}
}
}
match matches.subcommand() {
Some(("launch", launch_matches)) => {
let profile = launch_matches
.get_one::<String>("profile")
.expect("Profile is required");
let profile = match launch_matches.get_one::<String>("profile") {
Some(p) => p,
None => {
tracing::error!("Launch subcommand missing required --profile argument");
return;
}
};
let mem_default = String::from("Default");
let memory = launch_matches
+56 -11
View File
@@ -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()
}
}
});