More ergonomic code

This commit is contained in:
Wojciech Kozlowski 2025-01-03 20:42:05 +01:00
parent 5e4baf7191
commit 493b8a3928
2 changed files with 19 additions and 17 deletions

View File

@ -40,18 +40,11 @@ impl<Http: IMusicBrainzHttp> MusicBrainzClient<Http> {
) -> Result<BrowseReleaseGroupResponse, Error> { ) -> Result<BrowseReleaseGroupResponse, Error> {
let entity = &request.entity; let entity = &request.entity;
let mbid = request.mbid.uuid().as_hyphenated(); let mbid = request.mbid.uuid().as_hyphenated();
let status = match request.release_group_status { let status = request
Some(ref value) => { .release_group_status
let value_str = match value { .as_ref()
BrowseReleaseGroupRequestReleaseGroupStatus::WebsiteDefault => { .map(|s| format!("&release-group-status={s}"))
"website-default" .unwrap_or_default();
}
BrowseReleaseGroupRequestReleaseGroupStatus::All => "all",
};
format!("&release-group-status={value_str}")
}
None => String::from(""),
};
let page = ApiDisplay::format_page_settings(paging); let page = ApiDisplay::format_page_settings(paging);
let url = format!("{MB_BASE_URL}/release-group?{entity}={mbid}{status}{page}"); let url = format!("{MB_BASE_URL}/release-group?{entity}={mbid}{status}{page}");
@ -71,15 +64,24 @@ enum BrowseReleaseGroupRequestEntity {
Artist, Artist,
} }
impl fmt::Display for BrowseReleaseGroupRequestEntity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Artist => write!(f, "artist"),
}
}
}
enum BrowseReleaseGroupRequestReleaseGroupStatus { enum BrowseReleaseGroupRequestReleaseGroupStatus {
WebsiteDefault, WebsiteDefault,
All, All,
} }
impl fmt::Display for BrowseReleaseGroupRequestEntity { impl fmt::Display for BrowseReleaseGroupRequestReleaseGroupStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
BrowseReleaseGroupRequestEntity::Artist => write!(f, "artist"), Self::WebsiteDefault => write!(f, "website-default"),
Self::All => write!(f, "all"),
} }
} }
} }
@ -93,13 +95,13 @@ impl<'a> BrowseReleaseGroupRequest<'a> {
} }
} }
pub fn status_website_default(mut self) -> Self { pub fn filter_status_website_default(mut self) -> Self {
self.release_group_status = self.release_group_status =
Some(BrowseReleaseGroupRequestReleaseGroupStatus::WebsiteDefault); Some(BrowseReleaseGroupRequestReleaseGroupStatus::WebsiteDefault);
self self
} }
pub fn status_all(mut self) -> Self { pub fn filter_status_all(mut self) -> Self {
self.release_group_status = Some(BrowseReleaseGroupRequestReleaseGroupStatus::All); self.release_group_status = Some(BrowseReleaseGroupRequestReleaseGroupStatus::All);
self self
} }

View File

@ -99,7 +99,7 @@ impl<Http: IMusicBrainzHttp> IMusicBrainz for MusicBrainz<Http> {
artist: &Mbid, artist: &Mbid,
paging: &mut Option<PageSettings>, paging: &mut Option<PageSettings>,
) -> Result<Vec<Entity<AlbumMeta>>, Error> { ) -> Result<Vec<Entity<AlbumMeta>>, Error> {
let request = BrowseReleaseGroupRequest::artist(artist).status_website_default(); let request = BrowseReleaseGroupRequest::artist(artist).filter_status_website_default();
let page = paging.take().unwrap_or_default(); let page = paging.take().unwrap_or_default();
let mb_response = self.client.browse_release_group(&request, &page)?; let mb_response = self.client.browse_release_group(&request, &page)?;