Skip to content

Commit

Permalink
refactor: consolidate Error::ResponseDecode and `Error::ResponseEnc…
Browse files Browse the repository at this point in the history
…ode` into just `Error::Reqwest`
  • Loading branch information
Rolv-Apneseth committed Nov 16, 2024
1 parent a90bd15 commit 646b626
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 104 deletions.
127 changes: 52 additions & 75 deletions src/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,38 +369,35 @@ impl ServerClient {

/// Send a check request to the server and await for the response.
pub async fn check(&self, request: &Request<'_>) -> Result<Response> {
match self
let resp = self
.client
.post(format!("{0}/check", self.api))
.query(request)
.send()
.await
{
Ok(resp) => {
match resp.error_for_status_ref() {
Ok(_) => {
resp.json::<Response>()
.await
.map_err(Error::ResponseDecode)
.map(|mut resp| {
if self.max_suggestions > 0 {
let max = self.max_suggestions as usize;
resp.matches.iter_mut().for_each(|m| {
let len = m.replacements.len();
if max < len {
m.replacements[max] =
format!("... ({} not shown)", len - max).into();
m.replacements.truncate(max + 1);
}
});
.map_err(Error::Reqwest)?;

match resp.error_for_status_ref() {
Ok(_) => {
resp.json::<Response>()
.await
.map_err(Into::into)
.map(|mut resp| {
if self.max_suggestions > 0 {
let max = self.max_suggestions as usize;
resp.matches.iter_mut().for_each(|m| {
let len = m.replacements.len();
if max < len {
m.replacements[max] =
format!("... ({} not shown)", len - max).into();
m.replacements.truncate(max + 1);
}
resp
})
},
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
});
}
resp
})
},
Err(e) => Err(Error::RequestEncode(e)),
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
}

Expand Down Expand Up @@ -467,69 +464,52 @@ impl ServerClient {

/// Send a languages request to the server and await for the response.
pub async fn languages(&self) -> Result<languages::Response> {
match self
let resp = self
.client
.get(format!("{}/languages", self.api))
.send()
.await
{
Ok(resp) => {
match resp.error_for_status_ref() {
Ok(_) => {
resp.json::<languages::Response>()
.await
.map_err(Error::ResponseDecode)
},
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
},
Err(e) => Err(Error::RequestEncode(e)),
.map_err(Error::Reqwest)?;

match resp.error_for_status_ref() {
Ok(_) => resp.json::<languages::Response>().await.map_err(Into::into),
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
}

/// Send a words request to the server and await for the response.
pub async fn words(&self, request: &words::Request) -> Result<words::Response> {
match self
let resp = self
.client
.get(format!("{}/words", self.api))
.query(request)
.send()
.await
{
Ok(resp) => {
match resp.error_for_status_ref() {
Ok(_) => {
resp.json::<words::Response>()
.await
.map_err(Error::ResponseDecode)
},
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
},
Err(e) => Err(Error::RequestEncode(e)),
.map_err(Error::Reqwest)?;

match resp.error_for_status_ref() {
Ok(_) => resp.json::<words::Response>().await.map_err(Error::Reqwest),
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
}

/// Send a words/add request to the server and await for the response.
pub async fn words_add(&self, request: &words::add::Request) -> Result<words::add::Response> {
match self
let resp = self
.client
.post(format!("{}/words/add", self.api))
.query(request)
.send()
.await
{
Ok(resp) => {
match resp.error_for_status_ref() {
Ok(_) => {
resp.json::<words::add::Response>()
.await
.map_err(Error::ResponseDecode)
},
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
.map_err(Error::Reqwest)?;

match resp.error_for_status_ref() {
Ok(_) => {
resp.json::<words::add::Response>()
.await
.map_err(Error::Reqwest)
},
Err(e) => Err(Error::RequestEncode(e)),
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
}

Expand All @@ -538,24 +518,21 @@ impl ServerClient {
&self,
request: &words::delete::Request,
) -> Result<words::delete::Response> {
match self
let resp = self
.client
.post(format!("{}/words/delete", self.api))
.query(request)
.send()
.await
{
Ok(resp) => {
match resp.error_for_status_ref() {
Ok(_) => {
resp.json::<words::delete::Response>()
.await
.map_err(Error::ResponseDecode)
},
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
.map_err(Error::Reqwest)?;

match resp.error_for_status_ref() {
Ok(_) => {
resp.json::<words::delete::Response>()
.await
.map_err(Error::Reqwest)
},
Err(e) => Err(Error::RequestEncode(e)),
Err(_) => Err(Error::InvalidRequest(resp.text().await?)),
}
}

Expand Down
29 changes: 0 additions & 29 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,10 @@ pub enum Error {
#[error("could not parse {0:?} in a Docker action")]
ParseAction(String),

/// Error from request encoding.
#[error("request could not be properly encoded: {0}")]
RequestEncode(reqwest::Error),

/// Any other error from requests (see [`reqwest::Error`]).
#[error(transparent)]
Reqwest(#[from] reqwest::Error),

/// Error from request decoding.
#[error("response could not be properly decoded: {0}")]
ResponseDecode(reqwest::Error),

/// Error from reading environ variable (see [`std::env::VarError`]).
#[error(transparent)]
VarError(#[from] std::env::VarError),
Expand Down Expand Up @@ -155,27 +147,6 @@ mod tests {
}

#[ignore]
#[test]
fn test_error_request_encode() {
let result = std::fs::read_to_string(""); // TODO
assert!(result.is_err());

let error: Error = result.unwrap_err().into();

assert!(matches!(error, Error::RequestEncode(_)));
}

#[ignore]
#[test]
fn test_error_response_decode() {
let result = std::fs::read_to_string(""); // TODO
assert!(result.is_err());

let error: Error = result.unwrap_err().into();

assert!(matches!(error, Error::ResponseDecode(_)));
}

#[ignore]
#[test]
fn test_error_reqwest() {
Expand Down

0 comments on commit 646b626

Please sign in to comment.