Skip to content

Commit 5eeb772

Browse files
authored
Fix error handling (#71)
* Change the return type of main function * Fix the return type of decide_cell_width_type * Fix the return type of Config::load * Fix Deserialize implementation of KeyBind * Fix the return type of Repository::load * Define crate Result type * Remove color-eyre
1 parent 1199ae7 commit 5eeb772

File tree

9 files changed

+53
-204
lines changed

9 files changed

+53
-204
lines changed

Cargo.lock

-161
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ arboard = "3.4.1"
1919
base64 = "0.22.1"
2020
chrono = "0.4.39"
2121
clap = { version = "4.5.29", features = ["derive"] }
22-
color-eyre = "0.6.3"
2322
console = "0.15.10"
2423
fxhash = "0.2.1"
2524
image = "0.25.5"

src/check.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
11
use ratatui::crossterm::terminal;
22

3-
use crate::graph::{CellWidthType, Graph};
3+
use crate::{
4+
graph::{CellWidthType, Graph},
5+
Result,
6+
};
47

58
pub fn decide_cell_width_type(
69
graph: &Graph,
710
cell_width_type: Option<CellWidthType>,
8-
) -> std::io::Result<CellWidthType> {
11+
) -> Result<CellWidthType> {
912
let (w, h) = terminal::size()?;
10-
let cell_width_type =
11-
decide_cell_width_type_from(graph.max_pos_x, w as usize, h as usize, cell_width_type);
12-
Ok(cell_width_type)
13+
decide_cell_width_type_from(graph.max_pos_x, w as usize, h as usize, cell_width_type)
1314
}
1415

1516
fn decide_cell_width_type_from(
1617
max_pos_x: usize,
1718
term_width: usize,
1819
term_height: usize,
1920
cell_width_type: Option<CellWidthType>,
20-
) -> CellWidthType {
21+
) -> Result<CellWidthType> {
2122
let single_image_cell_width = max_pos_x + 1;
2223
let double_image_cell_width = single_image_cell_width * 2;
2324

2425
match cell_width_type {
2526
Some(CellWidthType::Double) => {
2627
let required_width = double_image_cell_width + 2;
2728
if required_width > term_width {
28-
panic!("Terminal size {term_width}x{term_height} is too small. Required width is {required_width} (graph_width = double).");
29+
let msg = format!("Terminal size {term_width}x{term_height} is too small. Required width is {required_width} (graph_width = double).");
30+
return Err(msg.into());
2931
}
30-
CellWidthType::Double
32+
Ok(CellWidthType::Double)
3133
}
3234
Some(CellWidthType::Single) => {
3335
let required_width = single_image_cell_width + 2;
3436
if required_width > term_width {
35-
panic!("Terminal size {term_width}x{term_height} is too small. Required width is {required_width} (graph_width = single).");
37+
let msg = format!("Terminal size {term_width}x{term_height} is too small. Required width is {required_width} (graph_width = single).");
38+
return Err(msg.into());
3639
}
37-
CellWidthType::Single
40+
Ok(CellWidthType::Single)
3841
}
3942
None => {
4043
let double_required_width = double_image_cell_width + 2;
4144
if double_required_width <= term_width {
42-
return CellWidthType::Double;
45+
return Ok(CellWidthType::Double);
4346
}
4447
let single_required_width = single_image_cell_width + 2;
4548
if single_required_width <= term_width {
46-
return CellWidthType::Single;
49+
return Ok(CellWidthType::Single);
4750
}
48-
panic!(
49-
"Terminal size {term_width}x{term_height} is too small. Required width is {single_required_width} (graph_width = single) or {double_required_width} (graph_width = double)."
50-
);
51+
let msg = format!("Terminal size {term_width}x{term_height} is too small. Required width is {single_required_width} (graph_width = single) or {double_required_width} (graph_width = double).");
52+
Err(msg.into())
5153
}
5254
}
5355
}

src/config.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ use serde::Deserialize;
77
use smart_default::SmartDefault;
88
use umbra::optional;
99

10-
use crate::keybind::KeyBind;
10+
use crate::{keybind::KeyBind, Result};
1111

1212
const APP_DIR_NAME: &str = "serie";
1313
const CONFIG_FILE_NAME: &str = "config.toml";
1414
const CONFIG_FILE_ENV_NAME: &str = "SERIE_CONFIG_FILE";
1515

16-
pub fn load() -> (UiConfig, GraphConfig, Option<KeyBind>) {
16+
pub fn load() -> Result<(UiConfig, GraphConfig, Option<KeyBind>)> {
1717
let config = match config_file_path_from_env() {
1818
Some(user_path) => {
1919
if !user_path.exists() {
20-
panic!("Config file not found: {:?}", user_path);
20+
let msg = format!("Config file not found: {:?}", user_path);
21+
return Err(msg.into());
2122
}
2223
read_config_from_path(&user_path)
2324
}
@@ -26,11 +27,11 @@ pub fn load() -> (UiConfig, GraphConfig, Option<KeyBind>) {
2627
if default_path.exists() {
2728
read_config_from_path(&default_path)
2829
} else {
29-
Config::default()
30+
Ok(Config::default())
3031
}
3132
}
32-
};
33-
(config.ui, config.graph, config.keybind)
33+
}?;
34+
Ok((config.ui, config.graph, config.keybind))
3435
}
3536

3637
fn config_file_path_from_env() -> Option<PathBuf> {
@@ -43,10 +44,10 @@ fn xdg_config_file_path() -> PathBuf {
4344
.get_config_file(CONFIG_FILE_NAME)
4445
}
4546

46-
fn read_config_from_path(path: &Path) -> Config {
47-
let content = std::fs::read_to_string(path).unwrap();
48-
let config: OptionalConfig = toml::from_str(&content).unwrap();
49-
config.into()
47+
fn read_config_from_path(path: &Path) -> Result<Config> {
48+
let content = std::fs::read_to_string(path)?;
49+
let config: OptionalConfig = toml::from_str(&content)?;
50+
Ok(config.into())
5051
}
5152

5253
#[optional(derives = [Deserialize])]

0 commit comments

Comments
 (0)