1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! Command, response, and status types.
use std::error::Error as StdError;
use std::ffi::OsString;
use std::fmt;
use std::result;
use guid_win::Guid;
use thiserror::Error;
use super::{BitsErrorContext, BitsJobProgress, BitsJobState, BitsJobTimes, BitsProxyUsage};
type HRESULT = i32;
/// An HRESULT with a descriptive message
#[derive(Clone, Debug)]
pub struct HResultMessage {
pub hr: HRESULT,
pub message: String,
}
impl fmt::Display for HResultMessage {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
self.message.fmt(f)
}
}
impl StdError for HResultMessage {}
/// Commands which can be sent to the server.
///
/// This is currently unused as the out-of-process Local Service server is not finished.
#[doc(hidden)]
#[derive(Clone, Debug)]
pub enum Command {
StartJob(StartJobCommand),
MonitorJob(MonitorJobCommand),
SuspendJob(SuspendJobCommand),
ResumeJob(ResumeJobCommand),
SetJobPriority(SetJobPriorityCommand),
SetNoProgressTimeout(SetNoProgressTimeoutCommand),
SetUpdateInterval(SetUpdateIntervalCommand),
CompleteJob(CompleteJobCommand),
CancelJob(CancelJobCommand),
}
/// Combine a [`Command`](enum.Command.html) with its success and failure result types.
#[doc(hidden)]
pub trait CommandType {
type Success;
type Failure: StdError;
fn wrap(command: Self) -> Command;
}
// Start Job
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct StartJobCommand {
pub url: OsString,
pub save_path: OsString,
pub proxy_usage: BitsProxyUsage,
pub no_progress_timeout_secs: u32,
pub monitor: Option<MonitorConfig>,
}
impl CommandType for StartJobCommand {
type Success = StartJobSuccess;
type Failure = StartJobFailure;
fn wrap(cmd: Self) -> Command {
Command::StartJob(cmd)
}
}
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct MonitorConfig {
pub pipe_name: OsString,
pub interval_millis: u32,
}
#[derive(Clone, Debug)]
pub struct StartJobSuccess {
pub guid: Guid,
}
#[derive(Clone, Debug, Error)]
pub enum StartJobFailure {
#[error("Argument validation failed: {0}")]
ArgumentValidation(String),
#[error("Create job: {0}")]
Create(HResultMessage),
#[error("Add file to job: {0}")]
AddFile(HResultMessage),
#[error("Apply settings to job: {0}")]
ApplySettings(HResultMessage),
#[error("Resume job: {0}")]
Resume(HResultMessage),
#[error("Connect to BackgroundCopyManager: {0}")]
ConnectBcm(HResultMessage),
#[error("BITS error: {0}")]
OtherBITS(HResultMessage),
#[error("Other failure: {0}")]
Other(String),
}
// Monitor Job
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct MonitorJobCommand {
pub guid: Guid,
pub monitor: MonitorConfig,
}
impl CommandType for MonitorJobCommand {
type Success = ();
type Failure = MonitorJobFailure;
fn wrap(cmd: Self) -> Command {
Command::MonitorJob(cmd)
}
}
#[derive(Clone, Debug, Error)]
pub enum MonitorJobFailure {
#[error("Argument validation failed: {0}")]
ArgumentValidation(String),
#[error("Job not found")]
NotFound,
#[error("Get job: {0}")]
GetJob(HResultMessage),
#[error("Connect to BackgroundCopyManager: {0}")]
ConnectBcm(HResultMessage),
#[error("BITS error: {0}")]
OtherBITS(HResultMessage),
#[error("Other failure: {0}")]
Other(String),
}
// Suspend Job
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct SuspendJobCommand {
pub guid: Guid,
}
impl CommandType for SuspendJobCommand {
type Success = ();
type Failure = SuspendJobFailure;
fn wrap(cmd: Self) -> Command {
Command::SuspendJob(cmd)
}
}
#[derive(Clone, Debug, Error)]
pub enum SuspendJobFailure {
#[error("Job not found")]
NotFound,
#[error("Get job: {0}")]
GetJob(HResultMessage),
#[error("Suspend job: {0}")]
SuspendJob(HResultMessage),
#[error("Connect to BackgroundCopyManager: {0}")]
ConnectBcm(HResultMessage),
#[error("BITS error: {0}")]
OtherBITS(HResultMessage),
#[error("Other failure: {0}")]
Other(String),
}
// Resume Job
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct ResumeJobCommand {
pub guid: Guid,
}
impl CommandType for ResumeJobCommand {
type Success = ();
type Failure = ResumeJobFailure;
fn wrap(cmd: Self) -> Command {
Command::ResumeJob(cmd)
}
}
#[derive(Clone, Debug, Error)]
pub enum ResumeJobFailure {
#[error("Job not found")]
NotFound,
#[error("Get job: {0}")]
GetJob(HResultMessage),
#[error("Resume job: {0}")]
ResumeJob(HResultMessage),
#[error("Connect to BackgroundCopyManager: {0}")]
ConnectBcm(HResultMessage),
#[error("BITS error: {0}")]
OtherBITS(HResultMessage),
#[error("Other failure: {0}")]
Other(String),
}
// Set Job Priority
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct SetJobPriorityCommand {
pub guid: Guid,
pub foreground: bool,
}
impl CommandType for SetJobPriorityCommand {
type Success = ();
type Failure = SetJobPriorityFailure;
fn wrap(cmd: Self) -> Command {
Command::SetJobPriority(cmd)
}
}
#[derive(Clone, Debug, Error)]
pub enum SetJobPriorityFailure {
#[error("Job not found")]
NotFound,
#[error("Get job: {0}")]
GetJob(HResultMessage),
#[error("Apply settings to job: {0}")]
ApplySettings(HResultMessage),
#[error("Connect to BackgroundCopyManager: {0}")]
ConnectBcm(HResultMessage),
#[error("BITS error: {0}")]
OtherBITS(HResultMessage),
#[error("Other failure: {0}")]
Other(String),
}
// Set No Progress Timeout
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct SetNoProgressTimeoutCommand {
pub guid: Guid,
pub timeout_secs: u32,
}
impl CommandType for SetNoProgressTimeoutCommand {
type Success = ();
type Failure = SetNoProgressTimeoutFailure;
fn wrap(cmd: Self) -> Command {
Command::SetNoProgressTimeout(cmd)
}
}
#[derive(Clone, Debug, Error)]
pub enum SetNoProgressTimeoutFailure {
#[error("Job not found")]
NotFound,
#[error("Get job: {0}")]
GetJob(HResultMessage),
#[error("Apply settings to job: {0}")]
ApplySettings(HResultMessage),
#[error("Connect to BackgroundCopyManager: {0}")]
ConnectBcm(HResultMessage),
#[error("BITS error: {0}")]
OtherBITS(HResultMessage),
#[error("Other failure: {0}")]
Other(String),
}
// Set Update Interval
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct SetUpdateIntervalCommand {
pub guid: Guid,
pub interval_millis: u32,
}
impl CommandType for SetUpdateIntervalCommand {
type Success = ();
type Failure = SetUpdateIntervalFailure;
fn wrap(cmd: Self) -> Command {
Command::SetUpdateInterval(cmd)
}
}
#[derive(Clone, Debug, Error)]
pub enum SetUpdateIntervalFailure {
#[error("Argument validation: {0}")]
ArgumentValidation(String),
#[error("Monitor not found")]
NotFound,
#[error("Other failure: {0}")]
Other(String),
}
// Complete Job
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct CompleteJobCommand {
pub guid: Guid,
}
impl CommandType for CompleteJobCommand {
type Success = ();
type Failure = CompleteJobFailure;
fn wrap(cmd: Self) -> Command {
Command::CompleteJob(cmd)
}
}
#[derive(Clone, Debug, Error)]
pub enum CompleteJobFailure {
#[error("Job not found")]
NotFound,
#[error("Get job: {0}")]
GetJob(HResultMessage),
#[error("Complete job: {0}")]
CompleteJob(HResultMessage),
#[error("Job only partially completed")]
PartialComplete,
#[error("Connect to BackgroundCopyManager: {0}")]
ConnectBcm(HResultMessage),
#[error("BITS error: {0}")]
OtherBITS(HResultMessage),
#[error("Other failure: {0}")]
Other(String),
}
// Cancel Job
#[doc(hidden)]
#[derive(Clone, Debug)]
pub struct CancelJobCommand {
pub guid: Guid,
}
impl CommandType for CancelJobCommand {
type Success = ();
type Failure = CancelJobFailure;
fn wrap(cmd: Self) -> Command {
Command::CancelJob(cmd)
}
}
#[derive(Clone, Debug, Error)]
pub enum CancelJobFailure {
#[error("Job not found")]
NotFound,
#[error("Get job: {0}")]
GetJob(HResultMessage),
#[error("Cancel job: {0}")]
CancelJob(HResultMessage),
#[error("Connect to BackgroundCopyManager: {0}")]
ConnectBcm(HResultMessage),
#[error("BITS error: {0}")]
OtherBITS(HResultMessage),
#[error("Other failure: {0}")]
Other(String),
}
/// Job status report
///
/// This includes a URL which updates with redirect but is otherwise the same as
/// `bits::status::BitsJobStatus`.
#[derive(Clone, Debug)]
pub struct JobStatus {
pub state: BitsJobState,
pub progress: BitsJobProgress,
pub error_count: u32,
pub error: Option<JobError>,
pub times: BitsJobTimes,
/// None means same as last time
pub url: Option<OsString>,
}
/// Job error report
#[derive(Clone, Debug, Error)]
#[error("Job error in context {context_str}: {error}")]
pub struct JobError {
pub context: BitsErrorContext,
pub context_str: String,
pub error: HResultMessage,
}
|