1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 //! `Action` is an enum describing what BITS action is being processed. This is 6 //! used mostly for logging and error reporting reasons. 7 //! The values of `Action` describe actions that could be in progress for 8 //! BitsService or BitsRequest. When specifying a type, `ServiceAction` or 9 //! `RequestAction`, can be used to restrict the action type to one of the two 10 //! categories. 11 //! A value of type `ServiceAction` or `RequestAction` can easily be converted 12 //! to an `Action` using the `into()` method. 13 14 use std::convert::From; 15 use xpcom::interfaces::nsIBits; 16 17 #[derive(Debug, PartialEq, Eq, Clone, Copy)] 18 pub enum Action { 19 StartDownload, 20 MonitorDownload, 21 Complete, 22 Cancel, 23 SetMonitorInterval, 24 SetPriority, 25 SetNoProgressTimeout, 26 Resume, 27 Suspend, 28 } 29 30 impl Action { description(&self) -> &'static str31 pub fn description(&self) -> &'static str { 32 match self { 33 Action::StartDownload => "starting download", 34 Action::MonitorDownload => "monitoring download", 35 Action::Complete => "completing download", 36 Action::Cancel => "cancelling download", 37 Action::SetMonitorInterval => "changing monitor interval", 38 Action::SetPriority => "setting download priority", 39 Action::SetNoProgressTimeout => "setting no progress timeout", 40 Action::Resume => "resuming download", 41 Action::Suspend => "suspending download", 42 } 43 } 44 as_error_code(&self) -> i3245 pub fn as_error_code(&self) -> i32 { 46 let val = match self { 47 Action::StartDownload => nsIBits::ERROR_ACTION_START_DOWNLOAD, 48 Action::MonitorDownload => nsIBits::ERROR_ACTION_MONITOR_DOWNLOAD, 49 Action::Complete => nsIBits::ERROR_ACTION_COMPLETE, 50 Action::Cancel => nsIBits::ERROR_ACTION_CANCEL, 51 Action::SetMonitorInterval => nsIBits::ERROR_ACTION_CHANGE_MONITOR_INTERVAL, 52 Action::SetPriority => nsIBits::ERROR_ACTION_SET_PRIORITY, 53 Action::SetNoProgressTimeout => nsIBits::ERROR_ACTION_SET_NO_PROGRESS_TIMEOUT, 54 Action::Resume => nsIBits::ERROR_ACTION_RESUME, 55 Action::Suspend => nsIBits::ERROR_ACTION_SUSPEND, 56 }; 57 val as i32 58 } 59 } 60 61 #[derive(Debug, PartialEq, Clone, Copy)] 62 pub enum ServiceAction { 63 StartDownload, 64 MonitorDownload, 65 } 66 67 impl From<ServiceAction> for Action { from(action: ServiceAction) -> Action68 fn from(action: ServiceAction) -> Action { 69 match action { 70 ServiceAction::StartDownload => Action::StartDownload, 71 ServiceAction::MonitorDownload => Action::MonitorDownload, 72 } 73 } 74 } 75 76 impl ServiceAction { as_error_code(&self) -> i3277 pub fn as_error_code(&self) -> i32 { 78 Action::as_error_code(&(self.clone()).into()) 79 } 80 description(&self) -> &'static str81 pub fn description(&self) -> &'static str { 82 Action::description(&(self.clone()).into()) 83 } 84 } 85 86 #[derive(Debug, PartialEq, Clone, Copy)] 87 pub enum RequestAction { 88 Complete, 89 Cancel, 90 SetMonitorInterval, 91 SetPriority, 92 SetNoProgressTimeout, 93 Resume, 94 Suspend, 95 } 96 97 impl From<RequestAction> for Action { from(action: RequestAction) -> Action98 fn from(action: RequestAction) -> Action { 99 match action { 100 RequestAction::Complete => Action::Complete, 101 RequestAction::Cancel => Action::Cancel, 102 RequestAction::SetMonitorInterval => Action::SetMonitorInterval, 103 RequestAction::SetPriority => Action::SetPriority, 104 RequestAction::SetNoProgressTimeout => Action::SetNoProgressTimeout, 105 RequestAction::Resume => Action::Resume, 106 RequestAction::Suspend => Action::Suspend, 107 } 108 } 109 } 110 111 impl RequestAction { as_error_code(&self) -> i32112 pub fn as_error_code(&self) -> i32 { 113 Action::as_error_code(&(self.clone()).into()) 114 } 115 description(&self) -> &'static str116 pub fn description(&self) -> &'static str { 117 Action::description(&(self.clone()).into()) 118 } 119 } 120