Integrate browse API into TUI MB daemon #230

Merged
wojtek merged 15 commits from 160---provide-a-keyboard-shortcut-to-pull-all-release-groups-of-an-artist into main 2024-10-06 15:32:46 +02:00
3 changed files with 18 additions and 15 deletions
Showing only changes of commit f61b357845 - Show all commits

View File

@ -98,16 +98,16 @@ impl<Http: IMusicBrainzHttp> IMusicBrainz for MusicBrainz<Http> {
&mut self, &mut self,
artist: &Mbid, artist: &Mbid,
paging: &mut PageSettings, paging: &mut PageSettings,
) -> Result<Paged<Vec<Entity<AlbumMeta>>>, Error> { ) -> Result<Paged<Entity<AlbumMeta>>, Error> {
let request = BrowseReleaseGroupRequest::artist(artist); let request = BrowseReleaseGroupRequest::artist(artist);
let mb_response = self.client.browse_release_group(&request, paging)?; let mb_response = self.client.browse_release_group(&request, paging)?;
let page_count = mb_response.release_groups.len(); let page_count = mb_response.release_groups.len();
let next = mb_response.page.next_page_offset(page_count); let next = mb_response.page.next_page_offset(page_count);
let item = from_browse_release_group_response(mb_response); let items = from_browse_release_group_response(mb_response);
Ok(Paged { item, next }) Ok(Paged { items, next })
} }
} }

View File

@ -285,20 +285,22 @@ impl JobInstance {
} }
.map(MbReturn::Match), .map(MbReturn::Match),
MbParams::Browse(browse) => match browse { MbParams::Browse(browse) => match browse {
BrowseParams::ReleaseGroup(params) => Paged::map_paged_result( BrowseParams::ReleaseGroup(params) => {
musicbrainz.browse_release_group(&params.artist, &mut paging), let paged = musicbrainz.browse_release_group(&params.artist, &mut paging);
|ents| EntityList::Album(ents.into_iter().map(|rg| rg.entity).collect()), let result = Paged::map_paged_result(paged, |rg| rg.entity, &mut next_page);
&mut next_page, result.map(EntityList::Album)
), }
} }
.map(MbReturn::Fetch), .map(MbReturn::Fetch),
}; };
Self::return_result(result_sender, event_sender, result)?; Self::return_result(result_sender, event_sender, result)?;
Ok(match next_page { let next_page_settings = match next_page {
NextPage::Offset(offset) => Some(paging.with_offset(offset)), NextPage::Offset(offset) => Some(paging.with_offset(offset)),
NextPage::Complete => None, NextPage::Complete => None,
}) };
Ok(next_page_settings)
} }
fn return_result( fn return_result(

View File

@ -14,6 +14,7 @@ use musichoard::{
pub trait IMusicBrainz { pub trait IMusicBrainz {
fn lookup_artist(&mut self, mbid: &Mbid) -> Result<Entity<ArtistMeta>, Error>; fn lookup_artist(&mut self, mbid: &Mbid) -> Result<Entity<ArtistMeta>, Error>;
fn lookup_release_group(&mut self, mbid: &Mbid) -> Result<Entity<AlbumMeta>, Error>; fn lookup_release_group(&mut self, mbid: &Mbid) -> Result<Entity<AlbumMeta>, Error>;
// TODO: Also make it out Paged
fn search_artist(&mut self, artist: &ArtistMeta) -> Result<Vec<Entity<ArtistMeta>>, Error>; fn search_artist(&mut self, artist: &ArtistMeta) -> Result<Vec<Entity<ArtistMeta>>, Error>;
fn search_release_group( fn search_release_group(
&mut self, &mut self,
@ -24,7 +25,7 @@ pub trait IMusicBrainz {
&mut self, &mut self,
artist: &Mbid, artist: &Mbid,
paging: &mut PageSettings, paging: &mut PageSettings,
) -> Result<Paged<Vec<Entity<AlbumMeta>>>, Error>; ) -> Result<Paged<Entity<AlbumMeta>>, Error>;
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
@ -45,20 +46,20 @@ impl<T> Entity<T> {
} }
pub struct Paged<T> { pub struct Paged<T> {
pub item: T, pub items: Vec<T>,
pub next: NextPage, pub next: NextPage,
} }
impl<T> Paged<T> { impl<T> Paged<T> {
pub fn map_paged_result<E, U, F: FnOnce(T) -> U>( pub fn map_paged_result<E, U, F: FnMut(T) -> U>(
result: Result<Paged<T>, E>, result: Result<Paged<T>, E>,
op: F, op: F,
next: &mut NextPage, next: &mut NextPage,
) -> Result<U, E> { ) -> Result<Vec<U>, E> {
match result { match result {
Ok(paged) => { Ok(paged) => {
_ = mem::replace(next, paged.next); _ = mem::replace(next, paged.next);
Ok(op(paged.item)) Ok(paged.items.into_iter().map(op).collect())
} }
Err(err) => Err(err), Err(err) => Err(err),
} }