Integrate browse API into TUI MB daemon #230
6
src/tui/lib/external/musicbrainz/api/mod.rs
vendored
6
src/tui/lib/external/musicbrainz/api/mod.rs
vendored
@ -98,16 +98,16 @@ impl<Http: IMusicBrainzHttp> IMusicBrainz for MusicBrainz<Http> {
|
||||
&mut self,
|
||||
artist: &Mbid,
|
||||
paging: &mut PageSettings,
|
||||
) -> Result<Paged<Vec<Entity<AlbumMeta>>>, Error> {
|
||||
) -> Result<Paged<Entity<AlbumMeta>>, Error> {
|
||||
let request = BrowseReleaseGroupRequest::artist(artist);
|
||||
|
||||
let mb_response = self.client.browse_release_group(&request, paging)?;
|
||||
|
||||
let page_count = mb_response.release_groups.len();
|
||||
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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
16
src/tui/lib/external/musicbrainz/daemon/mod.rs
vendored
16
src/tui/lib/external/musicbrainz/daemon/mod.rs
vendored
@ -285,20 +285,22 @@ impl JobInstance {
|
||||
}
|
||||
.map(MbReturn::Match),
|
||||
MbParams::Browse(browse) => match browse {
|
||||
BrowseParams::ReleaseGroup(params) => Paged::map_paged_result(
|
||||
musicbrainz.browse_release_group(¶ms.artist, &mut paging),
|
||||
|ents| EntityList::Album(ents.into_iter().map(|rg| rg.entity).collect()),
|
||||
&mut next_page,
|
||||
),
|
||||
BrowseParams::ReleaseGroup(params) => {
|
||||
let paged = musicbrainz.browse_release_group(¶ms.artist, &mut paging);
|
||||
let result = Paged::map_paged_result(paged, |rg| rg.entity, &mut next_page);
|
||||
result.map(EntityList::Album)
|
||||
}
|
||||
}
|
||||
.map(MbReturn::Fetch),
|
||||
};
|
||||
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::Complete => None,
|
||||
})
|
||||
};
|
||||
|
||||
Ok(next_page_settings)
|
||||
}
|
||||
|
||||
fn return_result(
|
||||
|
@ -14,6 +14,7 @@ use musichoard::{
|
||||
pub trait IMusicBrainz {
|
||||
fn lookup_artist(&mut self, mbid: &Mbid) -> Result<Entity<ArtistMeta>, 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_release_group(
|
||||
&mut self,
|
||||
@ -24,7 +25,7 @@ pub trait IMusicBrainz {
|
||||
&mut self,
|
||||
artist: &Mbid,
|
||||
paging: &mut PageSettings,
|
||||
) -> Result<Paged<Vec<Entity<AlbumMeta>>>, Error>;
|
||||
) -> Result<Paged<Entity<AlbumMeta>>, Error>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
@ -45,20 +46,20 @@ impl<T> Entity<T> {
|
||||
}
|
||||
|
||||
pub struct Paged<T> {
|
||||
pub item: T,
|
||||
pub items: Vec<T>,
|
||||
pub next: NextPage,
|
||||
}
|
||||
|
||||
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>,
|
||||
op: F,
|
||||
next: &mut NextPage,
|
||||
) -> Result<U, E> {
|
||||
) -> Result<Vec<U>, E> {
|
||||
match result {
|
||||
Ok(paged) => {
|
||||
_ = mem::replace(next, paged.next);
|
||||
Ok(op(paged.item))
|
||||
Ok(paged.items.into_iter().map(op).collect())
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user