From 95a6d3483a847cfffdea476d92ff2dc39c5cb997 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Tue, 30 Jan 2024 18:34:53 +0200 Subject: [PATCH] refactor(toggle): add `toggle` method to `Torrents` --- src/app/command.rs | 32 ++++++++++++++++++++++++++++++++ src/app/mod.rs | 15 ++++----------- 2 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 src/app/command.rs diff --git a/src/app/command.rs b/src/app/command.rs new file mode 100644 index 0000000..2acada9 --- /dev/null +++ b/src/app/command.rs @@ -0,0 +1,32 @@ +use anyhow::Result; +use transmission_rpc::types::{Torrent, TorrentAction, TorrentStatus}; + +use super::Torrents; + +impl Torrents { + pub async fn toggle(&mut self, torrent: &Torrent) -> transmission_rpc::types::Result<()> { + let id = torrent.id().ok_or_else(|| "ID not found")?; + let action = match torrent.status.ok_or_else(|| "Torrent status not found")? { + TorrentStatus::Stopped => TorrentAction::StartNow, + _ => TorrentAction::Stop, + }; + self.client.torrent_action(action, vec![id]).await?; + Ok(()) + } + + pub async fn toggle_all(&mut self) -> transmission_rpc::types::Result<()> { + todo!() + } + + pub fn move_dir(&mut self) -> Result<()> { + todo!() + } + + pub fn delete(&mut self) -> Result<()> { + todo!() + } + + pub fn rename(&mut self) -> Result<()> { + todo!() + } +} diff --git a/src/app/mod.rs b/src/app/mod.rs index f86265f..2acaabd 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -3,7 +3,8 @@ mod torrent; pub mod utils; use ratatui::widgets::TableState; -use transmission_rpc::types::{Result, Torrent, TorrentAction, TorrentStatus}; +use transmission_rpc::types::Torrent; +mod command; pub use self::{tab::Tab, torrent::Torrents}; @@ -117,17 +118,9 @@ impl<'a> App<'a> { self.show_popup = true; } - pub async fn toggle_torrent(&mut self) -> Result<()> { + pub async fn toggle_torrent(&mut self) -> transmission_rpc::types::Result<()> { let torrent = self.selected().ok_or_else(|| "Torrent not found")?; - let id = torrent.id().ok_or_else(|| "ID not found")?; - let action = match torrent.status.ok_or_else(|| "Torrent status not found")? { - TorrentStatus::Stopped => TorrentAction::StartNow, - _ => TorrentAction::Stop, - }; - self.torrents - .client - .torrent_action(action, vec![id]) - .await?; + self.torrents.toggle(&torrent.clone()).await?; self.close_popup(); Ok(()) }