diff --git a/rm-config/src/lib.rs b/rm-config/src/lib.rs index 9fddaec..8de59d7 100644 --- a/rm-config/src/lib.rs +++ b/rm-config/src/lib.rs @@ -113,7 +113,7 @@ impl Config { XDG_DIRS.get_or_init(|| xdg::BaseDirectories::with_prefix("rustmission").unwrap()) } - fn get_config_path() -> &'static PathBuf { + pub fn get_config_path() -> &'static PathBuf { CONFIG_PATH.get_or_init(|| { Self::get_xdg_dirs() .place_config_file("config.toml") diff --git a/rm-main/src/app.rs b/rm-main/src/app.rs index acf2dc8..89f8620 100644 --- a/rm-main/src/app.rs +++ b/rm-main/src/app.rs @@ -8,7 +8,7 @@ use crate::{ ui::{components::Component, MainWindow}, }; -use anyhow::Result; +use anyhow::{Error, Result}; use tokio::sync::{ mpsc::{self, UnboundedReceiver, UnboundedSender}, Mutex, @@ -30,15 +30,25 @@ impl Ctx { config: Config, action_tx: UnboundedSender, trans_tx: UnboundedSender, - ) -> Self { - let session_info = Arc::new(client.lock().await.session_get().await.unwrap().arguments); - - Self { - client, - config: Arc::new(config), - action_tx, - trans_tx, - session_info, + ) -> Result { + let response = client.lock().await.session_get().await; + match response { + Ok(res) => { + let session_info = Arc::new(res.arguments); + return Ok(Self { + client, + config: Arc::new(config), + action_tx, + trans_tx, + session_info, + }); + } + Err(e) => { + let config_path = Config::get_config_path().to_str().unwrap(); + return Err(Error::msg(format!( + "{e}\nIs the connection info in {config_path} correct?" + ))); + } } } @@ -60,23 +70,22 @@ pub struct App { } impl App { - pub async fn new(config: Config) -> Self { + pub async fn new(config: Config) -> Result { let (action_tx, action_rx) = mpsc::unbounded_channel(); let client = Arc::new(Mutex::new(transmission::utils::client_from_config(&config))); let (trans_tx, trans_rx) = mpsc::unbounded_channel(); - let ctx = Ctx::new(client, config, action_tx, trans_tx).await; + let ctx = Ctx::new(client, config, action_tx, trans_tx).await?; tokio::spawn(transmission::action_handler(ctx.clone(), trans_rx)); - - Self { + Ok(Self { should_quit: false, main_window: MainWindow::new(ctx.clone()), action_rx, ctx, mode: Mode::Normal, - } + }) } pub async fn run(&mut self) -> Result<()> { diff --git a/rm-main/src/main.rs b/rm-main/src/main.rs index bf95067..7168fa1 100644 --- a/rm-main/src/main.rs +++ b/rm-main/src/main.rs @@ -28,7 +28,7 @@ async fn main() -> Result<()> { } async fn run_tui(config: Config) -> Result<()> { - let mut app = App::new(config).await; + let mut app = App::new(config).await?; app.run().await?; Ok(()) }