Skip to content

Commit 32c316f

Browse files
committed
Turn macros into regular code
1 parent 2e25889 commit 32c316f

File tree

4 files changed

+306
-99
lines changed

4 files changed

+306
-99
lines changed

src/agent.rs

+89-29
Original file line numberDiff line numberDiff line change
@@ -247,37 +247,97 @@ impl Agent {
247247
pub(crate) fn new_request_level_config(&self) -> RequestLevelConfig {
248248
RequestLevelConfig(self.config.as_ref().clone())
249249
}
250-
}
251250

252-
macro_rules! mk_method {
253-
($(($f:tt, $m:tt, $b:ty)),*) => {
254-
impl Agent {
255-
$(
256-
#[doc = concat!("Make a ", stringify!($m), " request using this agent.")]
257-
#[must_use]
258-
pub fn $f<T>(&self, uri: T) -> RequestBuilder<$b>
259-
where
260-
Uri: TryFrom<T>,
261-
<Uri as TryFrom<T>>::Error: Into<http::Error>,
262-
{
263-
RequestBuilder::<$b>::new(self.clone(), Method::$m, uri)
264-
}
265-
)*
266-
}
267-
};
268-
}
251+
/// Make a GET request using this agent.
252+
#[must_use]
253+
pub fn get<T>(&self, uri: T) -> RequestBuilder<WithoutBody>
254+
where
255+
Uri: TryFrom<T>,
256+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
257+
{
258+
RequestBuilder::<WithoutBody>::new(self.clone(), Method::GET, uri)
259+
}
260+
261+
/// Make a POST request using this agent.
262+
#[must_use]
263+
pub fn post<T>(&self, uri: T) -> RequestBuilder<WithBody>
264+
where
265+
Uri: TryFrom<T>,
266+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
267+
{
268+
RequestBuilder::<WithBody>::new(self.clone(), Method::POST, uri)
269+
}
270+
271+
/// Make a PUT request using this agent.
272+
#[must_use]
273+
pub fn put<T>(&self, uri: T) -> RequestBuilder<WithBody>
274+
where
275+
Uri: TryFrom<T>,
276+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
277+
{
278+
RequestBuilder::<WithBody>::new(self.clone(), Method::PUT, uri)
279+
}
280+
281+
/// Make a DELETE request using this agent.
282+
#[must_use]
283+
pub fn delete<T>(&self, uri: T) -> RequestBuilder<WithoutBody>
284+
where
285+
Uri: TryFrom<T>,
286+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
287+
{
288+
RequestBuilder::<WithoutBody>::new(self.clone(), Method::DELETE, uri)
289+
}
269290

270-
mk_method!(
271-
(get, GET, WithoutBody),
272-
(post, POST, WithBody),
273-
(put, PUT, WithBody),
274-
(delete, DELETE, WithoutBody),
275-
(head, HEAD, WithoutBody),
276-
(options, OPTIONS, WithoutBody),
277-
(connect, CONNECT, WithoutBody),
278-
(patch, PATCH, WithBody),
279-
(trace, TRACE, WithoutBody)
280-
);
291+
/// Make a HEAD request using this agent.
292+
#[must_use]
293+
pub fn head<T>(&self, uri: T) -> RequestBuilder<WithoutBody>
294+
where
295+
Uri: TryFrom<T>,
296+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
297+
{
298+
RequestBuilder::<WithoutBody>::new(self.clone(), Method::HEAD, uri)
299+
}
300+
301+
/// Make an OPTIONS request using this agent.
302+
#[must_use]
303+
pub fn options<T>(&self, uri: T) -> RequestBuilder<WithoutBody>
304+
where
305+
Uri: TryFrom<T>,
306+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
307+
{
308+
RequestBuilder::<WithoutBody>::new(self.clone(), Method::OPTIONS, uri)
309+
}
310+
311+
/// Make a CONNECT request using this agent.
312+
#[must_use]
313+
pub fn connect<T>(&self, uri: T) -> RequestBuilder<WithoutBody>
314+
where
315+
Uri: TryFrom<T>,
316+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
317+
{
318+
RequestBuilder::<WithoutBody>::new(self.clone(), Method::CONNECT, uri)
319+
}
320+
321+
/// Make a PATCH request using this agent.
322+
#[must_use]
323+
pub fn patch<T>(&self, uri: T) -> RequestBuilder<WithBody>
324+
where
325+
Uri: TryFrom<T>,
326+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
327+
{
328+
RequestBuilder::<WithBody>::new(self.clone(), Method::PATCH, uri)
329+
}
330+
331+
/// Make a TRACE request using this agent.
332+
#[must_use]
333+
pub fn trace<T>(&self, uri: T) -> RequestBuilder<WithoutBody>
334+
where
335+
Uri: TryFrom<T>,
336+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
337+
{
338+
RequestBuilder::<WithoutBody>::new(self.clone(), Method::TRACE, uri)
339+
}
340+
}
281341

282342
impl From<Config> for Agent {
283343
fn from(value: Config) -> Self {

src/lib.rs

+105-21
Original file line numberDiff line numberDiff line change
@@ -584,29 +584,113 @@ pub fn agent() -> Agent {
584584
Agent::new_with_defaults()
585585
}
586586

587-
macro_rules! mk_method {
588-
($f:tt, $m:tt, $b:ty) => {
589-
#[doc = concat!("Make a ", stringify!($m), " request.\n\nRun on a use-once [`Agent`].")]
590-
#[must_use]
591-
pub fn $f<T>(uri: T) -> RequestBuilder<$b>
592-
where
593-
Uri: TryFrom<T>,
594-
<Uri as TryFrom<T>>::Error: Into<http::Error>,
595-
{
596-
RequestBuilder::<$b>::new(Agent::new_with_defaults(), Method::$m, uri)
597-
}
598-
};
587+
/// Make a GET request.
588+
///
589+
/// Run on a use-once [`Agent`].
590+
#[must_use]
591+
pub fn get<T>(uri: T) -> RequestBuilder<WithoutBody>
592+
where
593+
Uri: TryFrom<T>,
594+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
595+
{
596+
RequestBuilder::<WithoutBody>::new(Agent::new_with_defaults(), Method::GET, uri)
599597
}
600598

601-
mk_method!(get, GET, WithoutBody);
602-
mk_method!(post, POST, WithBody);
603-
mk_method!(put, PUT, WithBody);
604-
mk_method!(delete, DELETE, WithoutBody);
605-
mk_method!(head, HEAD, WithoutBody);
606-
mk_method!(options, OPTIONS, WithoutBody);
607-
mk_method!(connect, CONNECT, WithoutBody);
608-
mk_method!(patch, PATCH, WithBody);
609-
mk_method!(trace, TRACE, WithoutBody);
599+
/// Make a POST request.
600+
///
601+
/// Run on a use-once [`Agent`].
602+
#[must_use]
603+
pub fn post<T>(uri: T) -> RequestBuilder<WithBody>
604+
where
605+
Uri: TryFrom<T>,
606+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
607+
{
608+
RequestBuilder::<WithBody>::new(Agent::new_with_defaults(), Method::POST, uri)
609+
}
610+
611+
/// Make a PUT request.
612+
///
613+
/// Run on a use-once [`Agent`].
614+
#[must_use]
615+
pub fn put<T>(uri: T) -> RequestBuilder<WithBody>
616+
where
617+
Uri: TryFrom<T>,
618+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
619+
{
620+
RequestBuilder::<WithBody>::new(Agent::new_with_defaults(), Method::PUT, uri)
621+
}
622+
623+
/// Make a DELETE request.
624+
///
625+
/// Run on a use-once [`Agent`].
626+
#[must_use]
627+
pub fn delete<T>(uri: T) -> RequestBuilder<WithoutBody>
628+
where
629+
Uri: TryFrom<T>,
630+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
631+
{
632+
RequestBuilder::<WithoutBody>::new(Agent::new_with_defaults(), Method::DELETE, uri)
633+
}
634+
635+
/// Make a HEAD request.
636+
///
637+
/// Run on a use-once [`Agent`].
638+
#[must_use]
639+
pub fn head<T>(uri: T) -> RequestBuilder<WithoutBody>
640+
where
641+
Uri: TryFrom<T>,
642+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
643+
{
644+
RequestBuilder::<WithoutBody>::new(Agent::new_with_defaults(), Method::HEAD, uri)
645+
}
646+
647+
/// Make an OPTIONS request.
648+
///
649+
/// Run on a use-once [`Agent`].
650+
#[must_use]
651+
pub fn options<T>(uri: T) -> RequestBuilder<WithoutBody>
652+
where
653+
Uri: TryFrom<T>,
654+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
655+
{
656+
RequestBuilder::<WithoutBody>::new(Agent::new_with_defaults(), Method::OPTIONS, uri)
657+
}
658+
659+
/// Make a CONNECT request.
660+
///
661+
/// Run on a use-once [`Agent`].
662+
#[must_use]
663+
pub fn connect<T>(uri: T) -> RequestBuilder<WithoutBody>
664+
where
665+
Uri: TryFrom<T>,
666+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
667+
{
668+
RequestBuilder::<WithoutBody>::new(Agent::new_with_defaults(), Method::CONNECT, uri)
669+
}
670+
671+
/// Make a PATCH request.
672+
///
673+
/// Run on a use-once [`Agent`].
674+
#[must_use]
675+
pub fn patch<T>(uri: T) -> RequestBuilder<WithBody>
676+
where
677+
Uri: TryFrom<T>,
678+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
679+
{
680+
RequestBuilder::<WithBody>::new(Agent::new_with_defaults(), Method::PATCH, uri)
681+
}
682+
683+
/// Make a TRACE request.
684+
///
685+
/// Run on a use-once [`Agent`].
686+
#[must_use]
687+
pub fn trace<T>(uri: T) -> RequestBuilder<WithoutBody>
688+
where
689+
Uri: TryFrom<T>,
690+
<Uri as TryFrom<T>>::Error: Into<http::Error>,
691+
{
692+
RequestBuilder::<WithoutBody>::new(Agent::new_with_defaults(), Method::TRACE, uri)
693+
}
610694

611695
#[cfg(test)]
612696
pub(crate) mod test {

src/proxy.rs

+33-18
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,41 @@ impl Proxy {
123123
///
124124
/// Returns `None` if no environment variable is set or the URI is invalid.
125125
pub fn try_from_env() -> Option<Self> {
126-
macro_rules! try_env {
127-
($($env:literal),+) => {
128-
$(
129-
if let Ok(env) = std::env::var($env) {
130-
if let Ok(proxy) = Self::new_with_flag(&env, true) {
131-
return Some(proxy);
132-
}
133-
}
134-
)+
135-
};
126+
if let Ok(env) = std::env::var("ALL_PROXY") {
127+
if let Ok(proxy) = Self::new_with_flag(&env, true) {
128+
return Some(proxy);
129+
}
130+
}
131+
132+
if let Ok(env) = std::env::var("all_proxy") {
133+
if let Ok(proxy) = Self::new_with_flag(&env, true) {
134+
return Some(proxy);
135+
}
136+
}
137+
138+
if let Ok(env) = std::env::var("HTTPS_PROXY") {
139+
if let Ok(proxy) = Self::new_with_flag(&env, true) {
140+
return Some(proxy);
141+
}
136142
}
137143

138-
try_env!(
139-
"ALL_PROXY",
140-
"all_proxy",
141-
"HTTPS_PROXY",
142-
"https_proxy",
143-
"HTTP_PROXY",
144-
"http_proxy"
145-
);
144+
if let Ok(env) = std::env::var("https_proxy") {
145+
if let Ok(proxy) = Self::new_with_flag(&env, true) {
146+
return Some(proxy);
147+
}
148+
}
149+
150+
if let Ok(env) = std::env::var("HTTP_PROXY") {
151+
if let Ok(proxy) = Self::new_with_flag(&env, true) {
152+
return Some(proxy);
153+
}
154+
}
155+
156+
if let Ok(env) = std::env::var("http_proxy") {
157+
if let Ok(proxy) = Self::new_with_flag(&env, true) {
158+
return Some(proxy);
159+
}
160+
}
146161
None
147162
}
148163

0 commit comments

Comments
 (0)