Skip to content

Commit

Permalink
refactor: remove manual map_errs
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Mar 6, 2024
1 parent 8243333 commit 093dec0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
3 changes: 2 additions & 1 deletion edu-sync/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ impl Config {
if matches!(&string_result, Err(err) if err.kind() == ErrorKind::NotFound) {
return Ok(Self::default());
}
toml::from_str(&string_result?).map_err(Into::into)
let config = toml::from_str(&string_result?)?;
Ok(config)
}

pub async fn write(&self) -> io::Result<()> {
Expand Down
30 changes: 15 additions & 15 deletions edu-ws/src/ajax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ impl Client {
.post(self.ajax_url.clone())
.json(&requests)
.send()
.await
.map_err(ReceiveError::HttpError)?
.await?
.json::<UntaggedResultHelper<Vec<AjaxResult<T>>, RequestError>>()
.await
.map_err(ReceiveError::HttpError)?
.await?
.0?
.into_iter()
.map(Into::into)
Expand All @@ -87,15 +85,17 @@ impl Client {
}

pub async fn get_config(&self) -> Result<Config, Error> {
self.call_ajax(&[Request {
method: "tool_mobile_get_public_config",
arguments: HashMap::new(),
}])
.await?
.into_iter()
.next()
.unwrap()
.map_err(Into::into)
let config = self
.call_ajax(&[Request {
method: "tool_mobile_get_public_config",
arguments: HashMap::new(),
}])
.await?
.into_iter()
.next()
.unwrap()?;

Ok(config)
}
}

Expand All @@ -122,8 +122,8 @@ pub enum Error {
pub enum ReceiveError {
#[error(transparent)]
RequestError(#[from] RequestError),
#[error("{0}")]
HttpError(reqwest::Error),
#[error(transparent)]
HttpError(#[from] reqwest::Error),
}

#[cfg(test)]
Expand Down
18 changes: 9 additions & 9 deletions edu-ws/src/token/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,30 @@ impl Client {
service: &'a str,
}

self.http_client
let response = self
.http_client
.post(self.login_url.clone())
.query(&LoginRequest {
username,
password,
service: "moodle_mobile_app",
})
.send()
.await
.map_err(ReceiveError::HttpError)?
.await?
.json::<UntaggedResultHelper<Response, Error>>()
.await
.map_err(ReceiveError::HttpError)?
.0
.map_err(ReceiveError::LoginRequestError)
.await?
.0?;

Ok(response)
}
}

#[derive(Error, Debug)]
pub enum ReceiveError {
#[error(transparent)]
LoginRequestError(#[from] Error),
#[error("{0}")]
HttpError(reqwest::Error),
#[error(transparent)]
HttpError(#[from] reqwest::Error),
}

pub type Result<T> = result::Result<T, ReceiveError>;
Expand Down
18 changes: 9 additions & 9 deletions edu-ws/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub enum Error {
pub enum RequestError {
#[error(transparent)]
WsError(#[from] Error),
#[error("{0}")]
HttpError(reqwest::Error),
#[error(transparent)]
HttpError(#[from] reqwest::Error),
}

pub type Result<T> = result::Result<T, RequestError>;
Expand Down Expand Up @@ -94,7 +94,8 @@ impl Client {
lang: Option<&'a str>,
}

self.http_client
let ret = self
.http_client
.post(self.ws_url.clone())
.query(&WsQuery {
token: &self.token,
Expand All @@ -107,13 +108,12 @@ impl Client {
lang: self.lang.as_deref(),
})
.send()
.await
.map_err(RequestError::HttpError)?
.await?
.json::<UntaggedResultHelper<T, Error>>()
.await
.map_err(RequestError::HttpError)?
.0
.map_err(RequestError::WsError)
.await?
.0?;

Ok(ret)
}

pub async fn get_info(&self) -> Result<Info> {
Expand Down

0 comments on commit 093dec0

Please sign in to comment.