Skip to content

Commit

Permalink
Refactor some requests that might return no field
Browse files Browse the repository at this point in the history
  • Loading branch information
jecaro committed Sep 23, 2024
1 parent bdb4ca7 commit 3f91a00
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/lms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,7 @@ impl LmsClient {
(|| async {
let (request, field) = LmsRequest::artist(name);
let lms_response = self.post(&request).await?;
as_string(lms_response, &field).map(Some).or_else(|e| {
match e.downcast_ref::<ResultError>() {
Some(ResultError::NoField { .. }) => Ok(None),
_ => Err(e),
}
})
as_string_or_not_there(lms_response, &field)
})()
.await,
anyhow!("Error get_artist"),
Expand All @@ -190,12 +185,7 @@ impl LmsClient {
(|| async {
let (request, field) = LmsRequest::current_title(name);
let lms_response = self.post(&request).await?;
as_string(lms_response, &field).map(Some).or_else(|e| {
match e.downcast_ref::<ResultError>() {
Some(ResultError::NoField { .. }) => Ok(None),
_ => Err(e),
}
})
as_string_or_not_there(lms_response, &field)
})()
.await,
anyhow!("Error get_current_title"),
Expand All @@ -209,12 +199,7 @@ impl LmsClient {
(|| async {
let (request, field) = LmsRequest::album(name);
let lms_response = self.post(&request).await?;
as_string(lms_response, &field).map(Some).or_else(|e| {
match e.downcast_ref::<ResultError>() {
Some(ResultError::NoField { .. }) => Ok(None),
_ => Err(e),
}
})
as_string_or_not_there(lms_response, &field)
})()
.await,
anyhow!("Error get_album"),
Expand Down Expand Up @@ -349,6 +334,15 @@ fn as_string(response: LmsResponse, field: &String) -> Result<String> {
}
}

fn as_string_or_not_there(response: LmsResponse, field: &String) -> Result<Option<String>> {
as_string(response, &field)
.map(Some)
.or_else(|e| match e.downcast_ref::<ResultError>() {
Some(ResultError::NoField { .. }) => Ok(None),
_ => Err(e),
})
}

fn as_mode(response: LmsResponse, field: &String) -> Result<Mode> {
let value = result_field(response, &field)?;
match value {
Expand Down

0 comments on commit 3f91a00

Please sign in to comment.