File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11use core:: fmt;
2- use std:: path:: PathBuf ;
2+ use std:: { path:: PathBuf , process :: exit } ;
33
4- use anyhow:: Context ;
54use ratatui:: layout:: Flex ;
65use 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 ) ]
1714pub struct Config {
1815 #[ serde( default = "default_layout" , deserialize_with = "deserialize_layout" ) ]
@@ -205,17 +202,23 @@ fn default_toggle_device_favorite() -> char {
205202}
206203
207204impl 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}
Original file line number Diff line number Diff 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 ( ) ;
Original file line number Diff line number Diff line change @@ -9,15 +9,24 @@ use bluetui::{
99} ;
1010use clap:: Parser ;
1111use ratatui:: { Terminal , backend:: CrosstermBackend } ;
12- use std:: { io, sync:: Arc } ;
12+ use std:: { io, process :: exit , sync:: Arc } ;
1313
1414#[ tokio:: main]
1515async 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) ?;
You can’t perform that action at this time.
0 commit comments