Fix(treewide): Replaced needless expect statements

This commit is contained in:
antifallobst 2023-07-10 22:10:54 +02:00
parent ce59c504bd
commit a30229b763
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
4 changed files with 34 additions and 26 deletions

View File

@ -39,29 +39,29 @@ impl Account {
} }
impl AccountsManager { impl AccountsManager {
pub fn new(config: Option<String>) -> Self { pub fn new(config: Option<String>) -> Result<Self> {
return match config { return match config {
Some(s) => { Some(s) => {
info!("Loading serialized AccountsManager"); info!("Loading serialized AccountsManager");
let accounts_data: AccountsData = let accounts_data: AccountsData =
serde_json::from_str(&s).expect("failed to deserialize json"); serde_json::from_str(&s)?;
let mut clients = Vec::new(); let mut clients = Vec::new();
clients.resize(accounts_data.accounts.len(), None); clients.resize(accounts_data.accounts.len(), None);
Self { Ok(Self {
current_account: accounts_data.current_account, current_account: accounts_data.current_account,
num_accounts: accounts_data.accounts.len() as u32, num_accounts: accounts_data.accounts.len() as u32,
accounts: accounts_data.accounts, accounts: accounts_data.accounts,
clients, clients,
} })
} }
None => { None => {
info!("Creating empty AccountsManager"); info!("Creating empty AccountsManager");
Self { Ok(Self {
current_account: 0, current_account: 0,
num_accounts: 0, num_accounts: 0,
accounts: Vec::new(), accounts: Vec::new(),
clients: Vec::new(), clients: Vec::new(),
} })
} }
}; };
} }
@ -92,16 +92,20 @@ impl AccountsManager {
.send() .send()
.await?; .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 { let account = Account {
homeserver: homeserver.to_string(), homeserver: homeserver.to_string(),
id, id,
name: client name,
.account()
.get_display_name()
.await?
.expect("failed to fetch display name"),
session: session.clone(), session: session.clone(),
sync_token: None, sync_token: None,
}; };
@ -128,7 +132,11 @@ impl AccountsManager {
error!("Tried to log in with an invalid account ID {}", account_id); error!("Tried to log in with an invalid account ID {}", account_id);
return Err(Error::msg("Invalid account ID")); return Err(Error::msg("Invalid account ID"));
} else { } 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 if self

View File

@ -29,27 +29,27 @@ impl Drop for App<'_> {
} }
impl App<'_> { impl App<'_> {
pub fn new() -> Self { pub fn new() -> Result<Self> {
let path: &std::path::Path = Path::new("userdata/accounts.json"); let path: &std::path::Path = Path::new("userdata/accounts.json");
let config = if path.exists() { let config = if path.exists() {
info!("Reading account config (userdata/accounts.json)"); 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 { } else {
None None
}; };
let (channel_tx, channel_rx) = mpsc::channel(256); let (channel_tx, channel_rx) = mpsc::channel(256);
Self { Ok(Self {
ui: ui::UI::new(), ui: ui::UI::new()?,
accounts_manager: AccountsManager::new(config), accounts_manager: AccountsManager::new(config)?,
status: Status::new(None), status: Status::new(None),
channel_tx, channel_tx,
channel_rx, channel_rx,
input_listener_killer: CancellationToken::new(), input_listener_killer: CancellationToken::new(),
matrix_listener_killer: CancellationToken::new(), matrix_listener_killer: CancellationToken::new(),
} })
} }
pub async fn run(&mut self) -> Result<()> { pub async fn run(&mut self) -> Result<()> {

View File

@ -6,7 +6,7 @@ mod ui;
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
cli_log::init_cli_log!(); cli_log::init_cli_log!();
let mut app = app::App::new(); let mut app = app::App::new()?;
app.run().await?; app.run().await?;
Ok(()) Ok(())

View File

@ -243,12 +243,12 @@ impl SetupUI<'_> {
} }
impl UI<'_> { impl UI<'_> {
pub fn new() -> Self { pub fn new() -> Result<Self> {
let stdout = terminal_prepare().expect("failed to prepare terminal"); let stdout = terminal_prepare()?;
let backend = CrosstermBackend::new(stdout); 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(); let mut message_compose = TextArea::default();
message_compose.set_block( message_compose.set_block(
@ -259,14 +259,14 @@ impl UI<'_> {
info!("Initialized UI"); info!("Initialized UI");
Self { Ok(Self {
terminal, terminal,
input_position: MainInputPosition::Rooms, input_position: MainInputPosition::Rooms,
rooms_state: ListState::default(), rooms_state: ListState::default(),
message_compose, message_compose,
cli: None, cli: None,
setup_ui: None, setup_ui: None,
} })
} }
pub fn cycle_main_input_position(&mut self) { pub fn cycle_main_input_position(&mut self) {