diff --git a/src/app/command.rs b/src/app/command.rs index a248eb3..e755683 100644 --- a/src/app/command.rs +++ b/src/app/command.rs @@ -1,6 +1,7 @@ -use std::{collections::HashSet, path::Path, vec}; +use std::{collections::HashSet, path::Path}; -use transmission_rpc::types::{Id, Torrent, TorrentAction, TorrentStatus}; +use tracing::error; +use transmission_rpc::types::{Torrent, TorrentAction, TorrentStatus}; use super::{types::Selected, Torrents}; @@ -20,77 +21,81 @@ impl Torrents { _ => TorrentAction::Stop, }; if let Some(id) = torrent.id() { - self.client.torrent_action(action, vec![id]).await.unwrap(); + if let Err(e) = self.client.torrent_action(action, vec![id]).await { + error!("{:?}", e); + } } } } pub async fn toggle_all(&mut self) { - let torrents: Vec<(Id, TorrentAction)> = self + let torrents: Vec<_> = self .torrents .iter() - .map(|torrent| { - ( - torrent.id().unwrap(), - match torrent.status { - Some(TorrentStatus::Stopped) => TorrentAction::StartNow, - _ => TorrentAction::Stop, - }, - ) + .filter_map(|torrent| { + torrent.id().map(|id| { + ( + id, + match torrent.status { + Some(TorrentStatus::Stopped) => TorrentAction::StartNow, + _ => TorrentAction::Stop, + }, + ) + }) }) .collect(); for (id, action) in torrents { - self.client - .torrent_action(action, vec![id]) - .await - .expect("Error toggling torrent"); + if let Err(e) = self.client.torrent_action(action, vec![id]).await { + error!("{:?}", e); + } } } pub async fn start_all(&mut self) { - self.action_all(TorrentAction::StartNow) - .await - .expect("Error starting all torrents"); + if let Err(e) = self.action_all(TorrentAction::StartNow).await { + error!("{:?}", e); + } } pub async fn stop_all(&mut self) { - self.action_all(TorrentAction::Stop) - .await - .expect("Error stopping all torrents"); + if let Err(e) = self.action_all(TorrentAction::Stop).await { + error!("{:?}", e); + } } - pub async fn move_dir( - &mut self, - torrent: &Torrent, - location: &Path, - move_from: Option, - ) -> transmission_rpc::types::Result<()> { - let id = torrent.id().expect("ID not found"); - self.client - .torrent_set_location(vec![id], location.to_string_lossy().into(), move_from) - .await?; - Ok(()) + pub async fn move_dir(&mut self, torrent: &Torrent, location: &Path, move_from: Option) { + if let Some(id) = torrent.id() { + if let Err(e) = self + .client + .torrent_set_location(vec![id], location.to_string_lossy().into(), move_from) + .await + { + error!("{:?}", e); + } + } } pub async fn delete(&mut self, ids: Selected, delete_local_data: bool) { - self.client + if let Err(e) = self + .client .torrent_remove(ids.into(), delete_local_data) .await - .unwrap(); + { + error!("{:?}", e); + } } - pub async fn rename( - &mut self, - torrent: &Torrent, - name: &Path, - ) -> transmission_rpc::types::Result<()> { - let id = torrent.id().expect("ID not found"); - let old_name = torrent.name.clone().unwrap(); - self.client - .torrent_rename_path(vec![id], old_name, name.to_string_lossy().into()) - .await?; - Ok(()) + pub async fn rename(&mut self, torrent: &Torrent, name: &Path) { + if let (Some(id), Some(old_name)) = (torrent.id(), torrent.name.clone()) { + if let Err(e) = self + .client + .torrent_rename_path(vec![id], old_name, name.to_string_lossy().into()) + .await + { + error!("{:?}", e); + } + } } async fn action_all(&mut self, action: TorrentAction) -> transmission_rpc::types::Result<()> { diff --git a/src/app/torrent.rs b/src/app/torrent.rs index 8889bda..d2a2f93 100644 --- a/src/app/torrent.rs +++ b/src/app/torrent.rs @@ -13,7 +13,7 @@ pub struct Torrents { /// Constructs a new instance of [`Torrents`]. pub client: TransClient, pub torrents: Vec, - pub selected: HashSet, // Torrent::id + pub selected: HashSet, pub fields: Option>, } diff --git a/src/handler.rs b/src/handler.rs index 4b707ee..476bfe7 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -21,6 +21,8 @@ pub fn get_action(key_event: KeyEvent) -> Option { KeyCode::Char('3') => Some(Action::SwitchTab(2)), KeyCode::Char('t') | KeyCode::Enter | KeyCode::Menu => Some(Action::ToggleTorrent), KeyCode::Char('a') => Some(Action::ToggleAll), + KeyCode::Char('d') => Some(Action::Delete(false)), + KeyCode::Char('D') => Some(Action::Delete(true)), KeyCode::Char(' ') => Some(Action::Select), // Other handlers you could add here. _ => None,