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
use std::ops::Not;
use http::{header::HeaderName, HeaderValue, StatusCode};
use smart_default::SmartDefault;
pub(super) type OnProgress<'r> = &'r (dyn Fn(&TransferProgressInfo) -> CallbackResult + Send + Sync);
pub(super) type OnStatusCode<'r> = &'r (dyn Fn(StatusCode) -> CallbackResult + Send + Sync);
pub(super) type OnHeader<'r> = &'r (dyn Fn(&HeaderName, &HeaderValue) -> CallbackResult + Send + Sync);
#[derive(Debug)]
pub struct TransferProgressInfo<'b> {
transferred_bytes: u64,
total_bytes: u64,
body: &'b [u8],
}
impl<'b> TransferProgressInfo<'b> {
#[inline]
pub fn new(transferred_bytes: u64, total_bytes: u64, body: &'b [u8]) -> Self {
Self {
transferred_bytes,
total_bytes,
body,
}
}
#[inline]
pub fn transferred_bytes(&self) -> u64 {
self.transferred_bytes
}
#[inline]
pub fn total_bytes(&self) -> u64 {
self.total_bytes
}
#[inline]
pub fn body(&self) -> &[u8] {
self.body
}
}
#[must_use]
#[derive(SmartDefault, Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum CallbackResult {
#[default]
Continue,
Cancel,
}
impl CallbackResult {
#[inline]
pub fn is_continue(self) -> bool {
self == Self::Continue
}
#[inline]
pub fn is_cancelled(self) -> bool {
self == Self::Cancel
}
}
impl FromIterator<CallbackResult> for CallbackResult {
#[inline]
fn from_iter<T: IntoIterator<Item = CallbackResult>>(iter: T) -> Self {
if iter.into_iter().any(|result| result.is_cancelled()) {
Self::Cancel
} else {
Self::Continue
}
}
}
impl Not for CallbackResult {
type Output = Self;
#[inline]
fn not(self) -> Self::Output {
if self.is_cancelled() {
Self::Continue
} else {
Self::Cancel
}
}
}