diff --git a/src/accounts/mod.rs b/src/accounts/mod.rs index 2119f3e..0b5a3a3 100644 --- a/src/accounts/mod.rs +++ b/src/accounts/mod.rs @@ -39,29 +39,29 @@ impl Account { } impl AccountsManager { - pub fn new(config: Option) -> Self { + pub fn new(config: Option) -> Result { return match config { Some(s) => { info!("Loading serialized AccountsManager"); let accounts_data: AccountsData = - serde_json::from_str(&s).expect("failed to deserialize json"); + serde_json::from_str(&s)?; let mut clients = Vec::new(); clients.resize(accounts_data.accounts.len(), None); - Self { + Ok(Self { current_account: accounts_data.current_account, num_accounts: accounts_data.accounts.len() as u32, accounts: accounts_data.accounts, clients, - } + }) } None => { info!("Creating empty AccountsManager"); - Self { + Ok(Self { current_account: 0, num_accounts: 0, accounts: Vec::new(), clients: Vec::new(), - } + }) } }; } @@ -92,16 +92,20 @@ impl AccountsManager { .send() .await?; - let session = client.session().expect("failed to get session"); + let session = match client.session() { + Some(s) => s, + None => return Err(Error::msg("Failed to get session")) + }; + + let name = match client.account().get_display_name().await? { + Some(n) => n, + None => return Err(Error::msg("Failed to get display name")) + }; let account = Account { homeserver: homeserver.to_string(), id, - name: client - .account() - .get_display_name() - .await? - .expect("failed to fetch display name"), + name, session: session.clone(), sync_token: None, }; @@ -128,7 +132,11 @@ impl AccountsManager { error!("Tried to log in with an invalid account ID {}", account_id); return Err(Error::msg("Invalid account ID")); } else { - self.get(account_id).expect("Account lookup failed") + if let Some(a) = self.get(account_id) { + a + } else { + return Err(Error::msg("Failed to get account")); + } }; if self diff --git a/src/app/mod.rs b/src/app/mod.rs index 93b3795..4cb6ed6 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -29,27 +29,27 @@ impl Drop for App<'_> { } impl App<'_> { - pub fn new() -> Self { + pub fn new() -> Result { let path: &std::path::Path = Path::new("userdata/accounts.json"); let config = if path.exists() { info!("Reading account config (userdata/accounts.json)"); - Some(std::fs::read_to_string(path).expect("failed to read accounts config")) + Some(std::fs::read_to_string(path)?) } else { None }; let (channel_tx, channel_rx) = mpsc::channel(256); - Self { - ui: ui::UI::new(), - accounts_manager: AccountsManager::new(config), + Ok(Self { + ui: ui::UI::new()?, + accounts_manager: AccountsManager::new(config)?, status: Status::new(None), channel_tx, channel_rx, input_listener_killer: CancellationToken::new(), matrix_listener_killer: CancellationToken::new(), - } + }) } pub async fn run(&mut self) -> Result<()> { diff --git a/src/main.rs b/src/main.rs index 0a46ccc..3ef9611 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ mod ui; async fn main() -> anyhow::Result<()> { cli_log::init_cli_log!(); - let mut app = app::App::new(); + let mut app = app::App::new()?; app.run().await?; Ok(()) diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 60b7ca0..f4d627f 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -243,12 +243,12 @@ impl SetupUI<'_> { } impl UI<'_> { - pub fn new() -> Self { - let stdout = terminal_prepare().expect("failed to prepare terminal"); + pub fn new() -> Result { + let stdout = terminal_prepare()?; let backend = CrosstermBackend::new(stdout); - let mut terminal = Terminal::new(backend).expect("failed to initialize terminal"); + let mut terminal = Terminal::new(backend)?; - terminal.clear().expect("failed to clear screen"); + terminal.clear()?; let mut message_compose = TextArea::default(); message_compose.set_block( @@ -259,14 +259,14 @@ impl UI<'_> { info!("Initialized UI"); - Self { + Ok(Self { terminal, input_position: MainInputPosition::Rooms, rooms_state: ListState::default(), message_compose, cli: None, setup_ui: None, - } + }) } pub fn cycle_main_input_position(&mut self) {