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

View File

@ -99,7 +99,7 @@ impl<Http: IMusicBrainzHttp> IMusicBrainz for MusicBrainz<Http> {
artist: &Mbid,
paging: &mut Option<PageSettings>,
) -> 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 mb_response = self.client.browse_release_group(&request, &page)?;