rm_shared/
action.rs

1use std::{error::Error, sync::Arc};
2
3use crossterm::event::KeyEvent;
4use magnetease::{MagneteaseError, MagneteaseResult};
5use transmission_rpc::types::{FreeSpace, SessionStats, Torrent};
6
7use crate::status_task::StatusTask;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum Action {
11    // General
12    HardQuit,
13    Quit,
14    Close,
15    Render,
16    Up,
17    Down,
18    Left,
19    Right,
20    ScrollUpBy(u8),
21    ScrollDownBy(u8),
22    ScrollUpPage,
23    ScrollDownPage,
24    Home,
25    End,
26    Confirm,
27    Select,
28    ShowHelp,
29    Search,
30    ChangeFocus,
31    ChangeTab(u8),
32    XdgOpen,
33    Input(KeyEvent),
34    MoveToColumnLeft,
35    MoveToColumnRight,
36    // Torrents Tab
37    ShowStats,
38    ShowFiles,
39    Pause,
40    Delete,
41    AddMagnet,
42    MoveTorrent,
43    ChangeCategory,
44    Rename,
45    // Search Tab
46    ShowProvidersInfo,
47}
48
49pub enum UpdateAction {
50    // General
51    SwitchToInputMode,
52    SwitchToNormalMode,
53    Error(Box<ErrorMessage>),
54    // Torrents Tab
55    SessionStats(Arc<SessionStats>),
56    FreeSpace(Arc<FreeSpace>),
57    UpdateTorrents(Vec<Torrent>),
58    UpdateCurrentTorrent(Box<Torrent>),
59    SearchFilterApply(String),
60    SearchFilterClear,
61    CancelTorrentTask,
62    // Search Tab
63    SearchStarted,
64    ProviderResult(MagneteaseResult),
65    ProviderError(MagneteaseError),
66    SearchFinished,
67    // Task Manager's Status Task
68    StatusTaskClear,
69    StatusTaskSuccess,
70    StatusTaskFailure,
71    StatusTaskSet(StatusTask),
72    StatusTaskSetSuccess(StatusTask),
73}
74
75#[derive(Debug, Clone, PartialEq, Eq)]
76pub struct ErrorMessage {
77    pub title: String,
78    pub description: String,
79    pub source: String,
80}
81
82impl ErrorMessage {
83    pub fn new(
84        title: impl Into<String>,
85        message: impl Into<String>,
86        error: Box<dyn Error>,
87    ) -> Self {
88        Self {
89            title: title.into(),
90            description: message.into(),
91            source: error.to_string(),
92        }
93    }
94}
95
96impl Action {
97    pub fn is_render(&self) -> bool {
98        *self == Self::Render
99    }
100
101    pub fn is_hard_quit(&self) -> bool {
102        *self == Self::HardQuit
103    }
104
105    pub fn is_quit(&self) -> bool {
106        *self == Self::HardQuit || *self == Self::Quit
107    }
108
109    pub fn is_soft_quit(&self) -> bool {
110        self.is_quit() || *self == Self::Close
111    }
112}