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
#[macro_export]
macro_rules! dbg_as_curl {
($req:expr) => {
match $req {
tmp => {
match tmp.try_clone().map(|b| b.build()) {
Some(Ok(req)) => eprintln!(
"[{}:{}] {} = {}",
file!(),
line!(),
stringify!($req),
$crate::AsCurl::new(&req)
),
Some(Err(err)) => eprintln!(
"[{}:{}] {} = *Error*: {}",
file!(),
line!(),
stringify!($req),
err
),
None => eprintln!(
"[{}:{}] {} = *Error*: request not cloneable",
file!(),
line!(),
stringify!($req)
),
}
tmp
}
}
};
}
pub struct AsCurl<'a> {
req: &'a reqwest::Request,
compress: bool,
verbose: bool,
}
impl<'a> AsCurl<'a> {
pub fn new(req: &'a reqwest::Request) -> AsCurl<'a> {
Self {
req,
compress: false,
verbose: false,
}
}
pub fn compress(self) -> Self {
Self {
compress: true,
..self
}
}
pub fn verbose(self) -> Self {
Self {
verbose: true,
..self
}
}
}
impl<'a> std::fmt::Debug for AsCurl<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
<Self as std::fmt::Display>::fmt(self, f)
}
}
impl<'a> std::fmt::Display for AsCurl<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let AsCurl {
req,
compress,
verbose,
} = *self;
write!(f, "curl ")?;
if compress {
write!(f, "--compress ")?;
}
if verbose {
write!(f, "--verbose ")?;
}
let method = req.method();
if method != "GET" {
write!(f, "-X {} ", method)?;
}
for (name, value) in req.headers() {
let value = value
.to_str()
.expect("Headers must contain only visible ASCII characters")
.replace("'", r"'\''");
write!(f, "--header '{}: {}' ", name, value)?;
}
if let Some(_) = req.body() {
log::warn!("dbg_as_curl cannot show request's body");
}
write!(f, "'{}'", req.url().to_string().replace("'", "%27"))?;
Ok(())
}
}