summaryrefslogtreecommitdiffstats
path: root/toolkit/components/bitsdownload/bits_client/bits/src/status.rs
blob: 648a26866e6bee86d9fdd4ccbe231e6afda315dd (plain)
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
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// All files in the project carrying such notice may not be copied, modified, or distributed
// except according to those terms.

//! Data types for reporting a job's status

use filetime_win::FileTime;
use winapi::shared::winerror::HRESULT;
use winapi::um::bits::{BG_ERROR_CONTEXT, BG_JOB_STATE};

#[cfg(feature = "status_serde")]
use serde_derive::{Deserialize, Serialize};

#[derive(Clone, Debug)]
#[cfg_attr(feature = "status_serde", derive(Serialize, Deserialize))]
pub struct BitsJobStatus {
    pub state: BitsJobState,
    pub progress: BitsJobProgress,
    pub error_count: u32,
    pub error: Option<BitsJobError>,
    pub times: BitsJobTimes,
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "status_serde", derive(Serialize, Deserialize))]
pub struct BitsJobError {
    pub context: BitsErrorContext,
    pub context_str: String,
    pub error: HRESULT,
    pub error_str: String,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "status_serde", derive(Serialize, Deserialize))]
pub enum BitsErrorContext {
    None,
    Unknown,
    GeneralQueueManager,
    QueueManagerNotification,
    LocalFile,
    RemoteFile,
    GeneralTransport,
    RemoteApplication,
    /// No other values are documented
    Other(BG_ERROR_CONTEXT),
}

impl From<BG_ERROR_CONTEXT> for BitsErrorContext {
    fn from(ec: BG_ERROR_CONTEXT) -> BitsErrorContext {
        use self::BitsErrorContext::*;
        use winapi::um::bits;
        match ec {
            bits::BG_ERROR_CONTEXT_NONE => None,
            bits::BG_ERROR_CONTEXT_UNKNOWN => Unknown,
            bits::BG_ERROR_CONTEXT_GENERAL_QUEUE_MANAGER => GeneralQueueManager,
            bits::BG_ERROR_CONTEXT_QUEUE_MANAGER_NOTIFICATION => QueueManagerNotification,
            bits::BG_ERROR_CONTEXT_LOCAL_FILE => LocalFile,
            bits::BG_ERROR_CONTEXT_REMOTE_FILE => RemoteFile,
            bits::BG_ERROR_CONTEXT_GENERAL_TRANSPORT => GeneralTransport,
            bits::BG_ERROR_CONTEXT_REMOTE_APPLICATION => RemoteApplication,
            ec => Other(ec),
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "status_serde", derive(Serialize, Deserialize))]
pub enum BitsJobState {
    Queued,
    Connecting,
    Transferring,
    Suspended,
    Error,
    TransientError,
    Transferred,
    Acknowledged,
    Cancelled,
    /// No other values are documented
    Other(BG_JOB_STATE),
}

impl From<BG_JOB_STATE> for BitsJobState {
    fn from(s: BG_JOB_STATE) -> BitsJobState {
        use self::BitsJobState::*;
        use winapi::um::bits;
        match s {
            bits::BG_JOB_STATE_QUEUED => Queued,
            bits::BG_JOB_STATE_CONNECTING => Connecting,
            bits::BG_JOB_STATE_TRANSFERRING => Transferring,
            bits::BG_JOB_STATE_SUSPENDED => Suspended,
            bits::BG_JOB_STATE_ERROR => Error,
            bits::BG_JOB_STATE_TRANSIENT_ERROR => TransientError,
            bits::BG_JOB_STATE_TRANSFERRED => Transferred,
            bits::BG_JOB_STATE_ACKNOWLEDGED => Acknowledged,
            bits::BG_JOB_STATE_CANCELLED => Cancelled,
            s => Other(s),
        }
    }
}

#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "status_serde", derive(Serialize, Deserialize))]
pub struct BitsJobProgress {
    pub total_bytes: Option<u64>,
    pub transferred_bytes: u64,
    pub total_files: u32,
    pub transferred_files: u32,
}

#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "status_serde", derive(Serialize, Deserialize))]
pub struct BitsJobTimes {
    pub creation: FileTime,
    pub modification: FileTime,
    pub transfer_completion: Option<FileTime>,
}