Skip to content

Commit 82d32de

Browse files
committed
Use a conventional struct instead of a Tuple struct
The Response struct contains a reqwest::Request and a Method fields, and accessing them by position may be a bit confusing. Giving an actual name to the fields brings us the benefit of easily understanding the methods we are reading, without the need to go to the top to remember which field we are referring to. Reference: https://doc.rust-lang.org/1.9.0/book/structs.html#tuple-structs
1 parent 269eed4 commit 82d32de

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

elasticsearch/src/http/response.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ use std::{collections::BTreeMap, fmt, str::FromStr};
3131
use void::Void;
3232

3333
/// A response from Elasticsearch
34-
pub struct Response(reqwest::Response, Method);
34+
pub struct Response {
35+
response: reqwest::Response,
36+
method: Method,
37+
}
3538

3639
impl Response {
3740
/// Creates a new instance of an Elasticsearch response
3841
pub fn new(response: reqwest::Response, method: Method) -> Self {
39-
Self(response, method)
42+
Self {
43+
response: response,
44+
method: method,
45+
}
4046
}
4147

4248
/// Get the response content-length, if known.
@@ -47,12 +53,12 @@ impl Response {
4753
/// - The response is compressed and automatically decoded (thus changing
4854
/// the actual decoded length).
4955
pub fn content_length(&self) -> Option<u64> {
50-
self.0.content_length()
56+
self.response.content_length()
5157
}
5258

5359
/// Gets the response content-type.
5460
pub fn content_type(&self) -> &str {
55-
self.0
61+
self.response
5662
.headers()
5763
.get(crate::http::headers::CONTENT_TYPE)
5864
.and_then(|value| value.to_str().ok())
@@ -61,15 +67,15 @@ impl Response {
6167

6268
/// Turn the response into an [Error] if Elasticsearch returned an error.
6369
pub fn error_for_status_code(self) -> Result<Self, ClientError> {
64-
match self.0.error_for_status_ref() {
70+
match self.response.error_for_status_ref() {
6571
Ok(_) => Ok(self),
6672
Err(err) => Err(err.into()),
6773
}
6874
}
6975

7076
/// Turn the response into an [Error] if Elasticsearch returned an error.
7177
pub fn error_for_status_code_ref(&self) -> Result<&Self, ClientError> {
72-
match self.0.error_for_status_ref() {
78+
match self.response.error_for_status_ref() {
7379
Ok(_) => Ok(self),
7480
Err(err) => Err(err.into()),
7581
}
@@ -95,44 +101,44 @@ impl Response {
95101
where
96102
B: DeserializeOwned,
97103
{
98-
let body = self.0.json::<B>().await?;
104+
let body = self.response.json::<B>().await?;
99105
Ok(body)
100106
}
101107

102108
/// Gets the response headers.
103109
pub fn headers(&self) -> &HeaderMap {
104-
self.0.headers()
110+
self.response.headers()
105111
}
106112

107113
/// Gets the request method.
108114
pub fn method(&self) -> Method {
109-
self.1
115+
self.method
110116
}
111117

112118
/// Get the HTTP status code of the response
113119
pub fn status_code(&self) -> StatusCode {
114-
self.0.status()
120+
self.response.status()
115121
}
116122

117123
/// Asynchronously reads the response body as plain text
118124
///
119125
/// Reading the response body consumes `self`
120126
pub async fn text(self) -> Result<String, ClientError> {
121-
let body = self.0.text().await?;
127+
let body = self.response.text().await?;
122128
Ok(body)
123129
}
124130

125131
/// Gets the request URL
126132
pub fn url(&self) -> &Url {
127-
self.0.url()
133+
self.response.url()
128134
}
129135

130136
/// Gets the Deprecation warning response headers
131137
///
132138
/// Deprecation headers signal the use of Elasticsearch functionality
133139
/// or features that are deprecated and will be removed in a future release.
134140
pub fn warning_headers(&self) -> impl Iterator<Item = &str> {
135-
self.0.headers().get_all("Warning").iter().map(|w| {
141+
self.response.headers().get_all("Warning").iter().map(|w| {
136142
let s = w.to_str().unwrap();
137143
let first_quote = s.find('"').unwrap();
138144
let last_quote = s.len() - 1;

0 commit comments

Comments
 (0)