Skip to content

Commit d704e65

Browse files
committed
Revert "refactor: make Config::new fallible"
This reverts commit 0a4f5e3.
1 parent 0a4f5e3 commit d704e65

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/config.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::fmt;
2-
use std::path::PathBuf;
2+
use std::{path::PathBuf, process::exit};
33

4-
use anyhow::Context;
54
use ratatui::layout::Flex;
65
use toml;
76

@@ -11,8 +10,6 @@ use serde::{
1110
de::{self, Unexpected, Visitor},
1211
};
1312

14-
use crate::app::AppResult;
15-
1613
#[derive(Deserialize, Debug)]
1714
pub struct Config {
1815
#[serde(default = "default_layout", deserialize_with = "deserialize_layout")]
@@ -205,17 +202,23 @@ fn default_toggle_device_favorite() -> char {
205202
}
206203

207204
impl Config {
208-
pub fn new(config_file_path: Option<PathBuf>) -> AppResult<Self> {
205+
pub fn new(config_file_path: Option<PathBuf>) -> Self {
209206
let conf_path = config_file_path.unwrap_or(
210207
dirs::config_dir()
211208
.unwrap()
212209
.join("bluetui")
213210
.join("config.toml"),
214211
);
215212

216-
let config_file_contents =
217-
std::fs::read_to_string(conf_path).context("failed reading contents of config file")?;
213+
let config = std::fs::read_to_string(conf_path).unwrap_or_default();
214+
let app_config: Config = match toml::from_str(&config) {
215+
Ok(c) => c,
216+
Err(e) => {
217+
eprintln!("{}", e);
218+
exit(1);
219+
}
220+
};
218221

219-
toml::from_str(&config_file_contents).context("failed deserializing config file")
222+
app_config
220223
}
221224
}

src/help.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ mod tests {
223223
frame.area(),
224224
focused_block,
225225
frame.area(),
226-
Config::new(None).unwrap().into(),
226+
Config::new(None).into(),
227227
);
228228
})
229229
.unwrap();

src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,24 @@ use bluetui::{
99
};
1010
use clap::Parser;
1111
use ratatui::{Terminal, backend::CrosstermBackend};
12-
use std::{io, sync::Arc};
12+
use std::{io, process::exit, sync::Arc};
1313

1414
#[tokio::main]
1515
async fn main() -> AppResult<()> {
1616
let args = cli::Args::parse();
1717

18+
let config_file_path = args.config_path.map(|config_path| {
19+
if config_path.exists() {
20+
config_path.to_owned()
21+
} else {
22+
eprintln!("Config file not found");
23+
exit(1);
24+
}
25+
});
26+
1827
rfkill::check()?;
1928

20-
let config = Arc::new(Config::new(args.config_path)?);
29+
let config = Arc::new(Config::new(config_file_path));
2130

2231
let backend = CrosstermBackend::new(io::stdout());
2332
let terminal = Terminal::new(backend)?;

0 commit comments

Comments
 (0)