From 898d2e1469041e302e1a2b909d7f90e58b227f78 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sat, 5 Oct 2024 21:06:04 +0200 Subject: [PATCH] Rename Match to Entity to broaden applicability --- src/tui/app/machine/fetch_state.rs | 10 ++-- src/tui/app/machine/match_state.rs | 30 +++++------ src/tui/app/machine/mod.rs | 7 ++- src/tui/app/mod.rs | 8 +-- src/tui/lib/external/musicbrainz/api/mod.rs | 50 +++++++++---------- .../lib/external/musicbrainz/daemon/mod.rs | 32 ++++++------ src/tui/lib/interface/musicbrainz/api/mod.rs | 22 ++++---- src/tui/ui/display.rs | 2 +- src/tui/ui/match_state.rs | 4 +- src/tui/ui/mod.rs | 10 ++-- 10 files changed, 89 insertions(+), 86 deletions(-) diff --git a/src/tui/app/machine/fetch_state.rs b/src/tui/app/machine/fetch_state.rs index 94aaefc..bd48b2a 100644 --- a/src/tui/app/machine/fetch_state.rs +++ b/src/tui/app/machine/fetch_state.rs @@ -290,7 +290,7 @@ mod tests { machine::tests::{inner, music_hoard}, Delta, IApp, IAppAccess, IAppInteractBrowse, MatchOption, MatchStateInfo, }, - lib::interface::musicbrainz::{self, api::Match, daemon::MockIMbJobSender}, + lib::interface::musicbrainz::{self, api::Entity, daemon::MockIMbJobSender}, testmod::COLLECTION, }; @@ -310,13 +310,13 @@ mod tests { let artist = COLLECTION[3].meta.clone(); - let matches: Vec> = vec![]; + let matches: Vec> = vec![]; let fetch_result = MatchStateInfo::artist_search(artist.clone(), matches); fetch_tx.send(Ok(fetch_result.clone())).unwrap(); assert_eq!(fetch.try_recv(), Err(TryRecvError::Empty)); - let lookup = Match::item(artist.clone()); + let lookup = Entity::new(artist.clone()); let lookup_result = MatchStateInfo::artist_lookup(artist.clone(), lookup); lookup_tx.send(Ok(lookup_result.clone())).unwrap(); @@ -601,7 +601,7 @@ mod tests { let (tx, rx) = mpsc::channel::(); let artist = COLLECTION[3].meta.clone(); - let artist_match = Match::with_score(COLLECTION[2].meta.clone(), 80); + let artist_match = Entity::with_score(COLLECTION[2].meta.clone(), 80); let artist_match_info = MatchStateInfo::artist_search(artist.clone(), vec![artist_match.clone()]); let fetch_result = Ok(artist_match_info); @@ -677,7 +677,7 @@ mod tests { assert!(matches!(app, AppState::Fetch(_))); let artist = COLLECTION[3].meta.clone(); - let match_info = MatchStateInfo::artist_search::>(artist, vec![]); + let match_info = MatchStateInfo::artist_search::>(artist, vec![]); let fetch_result = Ok(match_info); tx.send(fetch_result).unwrap(); diff --git a/src/tui/app/machine/match_state.rs b/src/tui/app/machine/match_state.rs index 844c862..82f497e 100644 --- a/src/tui/app/machine/match_state.rs +++ b/src/tui/app/machine/match_state.rs @@ -37,7 +37,7 @@ impl GetInfo for MatchOption { fn into_info(&self, mut info: Self::InfoType) -> InfoOption { match self { - MatchOption::Some(option) => info.musicbrainz = option.item.info.musicbrainz.clone(), + MatchOption::Some(option) => info.musicbrainz = option.entity.info.musicbrainz.clone(), MatchOption::CannotHaveMbid => info.musicbrainz = MbRefOption::CannotHaveMbid, MatchOption::ManualInputMbid => return InfoOption::NeedInput, } @@ -50,7 +50,7 @@ impl GetInfo for MatchOption { fn into_info(&self, mut info: Self::InfoType) -> InfoOption { match self { - MatchOption::Some(option) => info = option.item.info.clone(), + MatchOption::Some(option) => info = option.entity.info.clone(), MatchOption::CannotHaveMbid => info.musicbrainz = MbRefOption::CannotHaveMbid, MatchOption::ManualInputMbid => return InfoOption::NeedInput, } @@ -271,18 +271,18 @@ mod tests { IApp, IAppAccess, IAppInput, }, lib::interface::musicbrainz::{ - api::Match, + api::Entity, daemon::{MbParams, MockIMbJobSender}, }, }; use super::*; - impl Match { - pub fn with_score(item: T, score: u8) -> Self { - Match { + impl Entity { + pub fn with_score(entity: T, score: u8) -> Self { + Entity { score: Some(score), - item, + entity, disambiguation: None, } } @@ -302,10 +302,10 @@ mod tests { let artist = artist_meta(); let artist_1 = artist.clone(); - let artist_match_1 = Match::with_score(artist_1, 100); + let artist_match_1 = Entity::with_score(artist_1, 100); let artist_2 = artist.clone(); - let mut artist_match_2 = Match::with_score(artist_2, 100); + let mut artist_match_2 = Entity::with_score(artist_2, 100); artist_match_2.disambiguation = Some(String::from("some disambiguation")); let list = vec![artist_match_1.clone(), artist_match_2.clone()]; @@ -314,7 +314,7 @@ mod tests { fn artist_lookup() -> MatchStateInfo { let artist = artist_meta(); - let lookup = Match::item(artist.clone()); + let lookup = Entity::new(artist.clone()); MatchStateInfo::artist_lookup(artist, lookup) } @@ -335,12 +335,12 @@ mod tests { let album = album_meta(); let album_1 = album.clone(); - let album_match_1 = Match::with_score(album_1, 100); + let album_match_1 = Entity::with_score(album_1, 100); let mut album_2 = album.clone(); album_2.id.title.push_str(" extra title part"); album_2.info.secondary_types.pop(); - let album_match_2 = Match::with_score(album_2, 100); + let album_match_2 = Entity::with_score(album_2, 100); let list = vec![album_match_1.clone(), album_match_2.clone()]; MatchStateInfo::album_search(artist_id, album, list) @@ -349,7 +349,7 @@ mod tests { fn album_lookup() -> MatchStateInfo { let artist_id = ArtistId::new("Artist"); let album = album_meta(); - let lookup = Match::item(album.clone()); + let lookup = Entity::new(album.clone()); MatchStateInfo::album_lookup(artist_id, album, lookup) } @@ -597,7 +597,7 @@ mod tests { .with(predicate::always(), predicate::eq(requests)) .return_once(|_, _| Ok(())); - let matches_vec: Vec> = vec![]; + let matches_vec: Vec> = vec![]; let artist_match = MatchStateInfo::artist_search(artist.clone(), matches_vec); let matches = AppMachine::match_state( inner_with_mb(music_hoard(vec![]), mb_job_sender), @@ -630,7 +630,7 @@ mod tests { .with(predicate::always(), predicate::eq(requests)) .return_once(|_, _| Ok(())); - let matches_vec: Vec> = vec![]; + let matches_vec: Vec> = vec![]; let album_match = MatchStateInfo::album_search(artist_id.clone(), album.clone(), matches_vec); let matches = AppMachine::match_state( diff --git a/src/tui/app/machine/mod.rs b/src/tui/app/machine/mod.rs index 6747d01..2f270f9 100644 --- a/src/tui/app/machine/mod.rs +++ b/src/tui/app/machine/mod.rs @@ -226,7 +226,10 @@ mod tests { use crate::tui::{ app::{AppState, IApp, IAppInput, IAppInteractBrowse, InputEvent, MatchStateInfo}, - lib::{interface::musicbrainz::{api::Match, daemon::MockIMbJobSender}, MockIMusicHoard}, + lib::{ + interface::musicbrainz::{api::Entity, daemon::MockIMbJobSender}, + MockIMusicHoard, + }, }; use super::*; @@ -517,7 +520,7 @@ mod tests { let (_, rx) = mpsc::channel(); let fetch = FetchState::new(rx); let artist = ArtistMeta::new(ArtistId::new("Artist")); - let info = MatchStateInfo::artist_lookup(artist.clone(), Match::item(artist.clone())); + let info = MatchStateInfo::artist_lookup(artist.clone(), Entity::new(artist.clone())); app = AppMachine::match_state(app.unwrap_browse().inner, MatchState::new(info, fetch)).into(); diff --git a/src/tui/app/mod.rs b/src/tui/app/mod.rs index 637c8df..10e3b22 100644 --- a/src/tui/app/mod.rs +++ b/src/tui/app/mod.rs @@ -11,7 +11,7 @@ use musichoard::collection::{ Collection, }; -use crate::tui::lib::interface::musicbrainz::api::Match; +use crate::tui::lib::interface::musicbrainz::api::Entity; pub enum AppState { Browse(B), @@ -215,13 +215,13 @@ pub type InputPublic<'app> = &'app tui_input::Input; #[derive(Clone, Debug, PartialEq, Eq)] pub enum MatchOption { - Some(Match), + Some(Entity), CannotHaveMbid, ManualInputMbid, } -impl From> for MatchOption { - fn from(value: Match) -> Self { +impl From> for MatchOption { + fn from(value: Entity) -> Self { MatchOption::Some(value) } } diff --git a/src/tui/lib/external/musicbrainz/api/mod.rs b/src/tui/lib/external/musicbrainz/api/mod.rs index 6724289..115e0be 100644 --- a/src/tui/lib/external/musicbrainz/api/mod.rs +++ b/src/tui/lib/external/musicbrainz/api/mod.rs @@ -25,7 +25,7 @@ use musichoard::{ }, }; -use crate::tui::lib::interface::musicbrainz::api::{Error, IMusicBrainz, Match, Paged}; +use crate::tui::lib::interface::musicbrainz::api::{Entity, Error, IMusicBrainz, Paged}; // GRCOV_EXCL_START pub struct MusicBrainz { @@ -39,7 +39,7 @@ impl MusicBrainz { } impl IMusicBrainz for MusicBrainz { - fn lookup_artist(&mut self, mbid: &Mbid) -> Result, Error> { + fn lookup_artist(&mut self, mbid: &Mbid) -> Result, Error> { let request = LookupArtistRequest::new(mbid); let mb_response = self.client.lookup_artist(&request)?; @@ -47,7 +47,7 @@ impl IMusicBrainz for MusicBrainz { Ok(from_lookup_artist_response(mb_response)) } - fn lookup_release_group(&mut self, mbid: &Mbid) -> Result, Error> { + fn lookup_release_group(&mut self, mbid: &Mbid) -> Result, Error> { let request = LookupReleaseGroupRequest::new(mbid); let mb_response = self.client.lookup_release_group(&request)?; @@ -55,7 +55,7 @@ impl IMusicBrainz for MusicBrainz { Ok(from_lookup_release_group_response(mb_response)) } - fn search_artist(&mut self, artist: &ArtistMeta) -> Result>, Error> { + fn search_artist(&mut self, artist: &ArtistMeta) -> Result>, Error> { let query = SearchArtistRequest::new().string(&artist.id.name); let paging = PageSettings::default(); @@ -72,7 +72,7 @@ impl IMusicBrainz for MusicBrainz { &mut self, arid: &Mbid, album: &AlbumMeta, - ) -> Result>, Error> { + ) -> Result>, Error> { // Some release groups may have a promotional early release messing up the search. Searching // with just the year should be enough anyway. let date = AlbumDate::new(album.date.year, None, None); @@ -98,7 +98,7 @@ impl IMusicBrainz for MusicBrainz { &mut self, artist: &Mbid, paging: &mut PageSettings, - ) -> Result>>, Error> { + ) -> Result>>, Error> { let request = BrowseReleaseGroupRequest::artist(artist); let mb_response = self.client.browse_release_group(&request, paging)?; @@ -139,47 +139,47 @@ fn from_mb_release_group_meta(meta: MbReleaseGroupMeta) -> AlbumMeta { } } -fn from_lookup_artist_response(entity: LookupArtistResponse) -> Match { - let (item, disambiguation) = from_mb_artist_meta(entity.meta); - Match { +fn from_lookup_artist_response(response: LookupArtistResponse) -> Entity { + let (entity, disambiguation) = from_mb_artist_meta(response.meta); + Entity { score: None, - item, + entity, disambiguation, } } -fn from_lookup_release_group_response(entity: LookupReleaseGroupResponse) -> Match { - Match { +fn from_lookup_release_group_response(response: LookupReleaseGroupResponse) -> Entity { + Entity { score: None, - item: from_mb_release_group_meta(entity.meta), + entity: from_mb_release_group_meta(response.meta), disambiguation: None, } } -fn from_search_artist_response_artist(entity: SearchArtistResponseArtist) -> Match { - let (item, disambiguation) = from_mb_artist_meta(entity.meta); - Match { - score: Some(entity.score), - item, +fn from_search_artist_response_artist(response: SearchArtistResponseArtist) -> Entity { + let (entity, disambiguation) = from_mb_artist_meta(response.meta); + Entity { + score: Some(response.score), + entity, disambiguation, } } fn from_search_release_group_response_release_group( - entity: SearchReleaseGroupResponseReleaseGroup, -) -> Match { - Match { - score: Some(entity.score), - item: from_mb_release_group_meta(entity.meta), + response: SearchReleaseGroupResponseReleaseGroup, +) -> Entity { + Entity { + score: Some(response.score), + entity: from_mb_release_group_meta(response.meta), disambiguation: None, } } fn from_browse_release_group_response( entity: BrowseReleaseGroupResponse, -) -> Vec> { +) -> Vec> { let rgs = entity.release_groups.into_iter(); let metas = rgs.map(from_mb_release_group_meta); - metas.map(|meta| Match::item(meta)).collect() + metas.map(Entity::new).collect() } // GRCOV_EXCL_STOP diff --git a/src/tui/lib/external/musicbrainz/daemon/mod.rs b/src/tui/lib/external/musicbrainz/daemon/mod.rs index b793322..2007f35 100644 --- a/src/tui/lib/external/musicbrainz/daemon/mod.rs +++ b/src/tui/lib/external/musicbrainz/daemon/mod.rs @@ -321,7 +321,7 @@ mod tests { use crate::tui::{ event::{Event, EventError, MockIFetchCompleteEventSender}, - lib::interface::musicbrainz::api::{Match, MockIMusicBrainz}, + lib::interface::musicbrainz::api::{Entity, MockIMusicBrainz}, testmod::COLLECTION, }; @@ -408,11 +408,11 @@ mod tests { VecDeque::from([MbParams::search_artist(artist)]) } - fn search_artist_expectations() -> (ArtistMeta, Vec>) { + fn search_artist_expectations() -> (ArtistMeta, Vec>) { let artist = COLLECTION[3].meta.clone(); - let artist_match_1 = Match::with_score(artist.clone(), 100); - let artist_match_2 = Match::with_score(artist.clone(), 50); + let artist_match_1 = Entity::with_score(artist.clone(), 100); + let artist_match_2 = Entity::with_score(artist.clone(), 50); let matches = vec![artist_match_1.clone(), artist_match_2.clone()]; (artist, matches) @@ -441,23 +441,23 @@ mod tests { mb_ref_opt_unwrap(mbref).mbid().clone() } - fn search_album_expectations_1() -> (AlbumMeta, Vec>) { + fn search_album_expectations_1() -> (AlbumMeta, Vec>) { let album_1 = COLLECTION[1].albums[0].meta.clone(); let album_4 = COLLECTION[1].albums[3].meta.clone(); - let album_match_1_1 = Match::with_score(album_1.clone(), 100); - let album_match_1_2 = Match::with_score(album_4.clone(), 50); + let album_match_1_1 = Entity::with_score(album_1.clone(), 100); + let album_match_1_2 = Entity::with_score(album_4.clone(), 50); let matches_1 = vec![album_match_1_1.clone(), album_match_1_2.clone()]; (album_1, matches_1) } - fn search_album_expectations_4() -> (AlbumMeta, Vec>) { + fn search_album_expectations_4() -> (AlbumMeta, Vec>) { let album_1 = COLLECTION[1].albums[0].meta.clone(); let album_4 = COLLECTION[1].albums[3].meta.clone(); - let album_match_4_1 = Match::with_score(album_4.clone(), 100); - let album_match_4_2 = Match::with_score(album_1.clone(), 30); + let album_match_4_1 = Entity::with_score(album_4.clone(), 100); + let album_match_4_2 = Entity::with_score(album_1.clone(), 30); let matches_4 = vec![album_match_4_1.clone(), album_match_4_2.clone()]; (album_4, matches_4) @@ -539,7 +539,7 @@ mod tests { fn lookup_artist_expectation( musicbrainz: &mut MockIMusicBrainz, mbid: &Mbid, - lookup: &Match, + lookup: &Entity, ) { let result = Ok(lookup.clone()); musicbrainz @@ -554,7 +554,7 @@ mod tests { let mut musicbrainz = musicbrainz(); let mbid = mbid(); let artist = COLLECTION[3].meta.clone(); - let lookup = Match::item(artist.clone()); + let lookup = Entity::new(artist.clone()); lookup_artist_expectation(&mut musicbrainz, &mbid, &lookup); let mut event_sender = event_sender(); @@ -581,7 +581,7 @@ mod tests { fn lookup_release_group_expectation( musicbrainz: &mut MockIMusicBrainz, mbid: &Mbid, - lookup: &Match, + lookup: &Entity, ) { let result = Ok(lookup.clone()); musicbrainz @@ -596,7 +596,7 @@ mod tests { let mut musicbrainz = musicbrainz(); let mbid = mbid(); let album = COLLECTION[1].albums[0].meta.clone(); - let lookup = Match::item(album.clone()); + let lookup = Entity::new(album.clone()); lookup_release_group_expectation(&mut musicbrainz, &mbid, &lookup); let mut event_sender = event_sender(); @@ -627,7 +627,7 @@ mod tests { fn search_artist_expectation( musicbrainz: &mut MockIMusicBrainz, artist: &ArtistMeta, - matches: &[Match], + matches: &[Entity], ) { let result = Ok(matches.to_owned()); musicbrainz @@ -669,7 +669,7 @@ mod tests { seq: &mut Sequence, arid: &Mbid, album: &AlbumMeta, - matches: &[Match], + matches: &[Entity], ) { let result = Ok(matches.to_owned()); musicbrainz diff --git a/src/tui/lib/interface/musicbrainz/api/mod.rs b/src/tui/lib/interface/musicbrainz/api/mod.rs index 2cc618b..cf70913 100644 --- a/src/tui/lib/interface/musicbrainz/api/mod.rs +++ b/src/tui/lib/interface/musicbrainz/api/mod.rs @@ -10,33 +10,33 @@ use musichoard::{ #[cfg_attr(test, automock)] pub trait IMusicBrainz { - fn lookup_artist(&mut self, mbid: &Mbid) -> Result, Error>; - fn lookup_release_group(&mut self, mbid: &Mbid) -> Result, Error>; - fn search_artist(&mut self, artist: &ArtistMeta) -> Result>, Error>; + fn lookup_artist(&mut self, mbid: &Mbid) -> Result, Error>; + fn lookup_release_group(&mut self, mbid: &Mbid) -> Result, Error>; + fn search_artist(&mut self, artist: &ArtistMeta) -> Result>, Error>; fn search_release_group( &mut self, arid: &Mbid, album: &AlbumMeta, - ) -> Result>, Error>; + ) -> Result>, Error>; fn browse_release_group( &mut self, artist: &Mbid, paging: &mut PageSettings, - ) -> Result>>, Error>; + ) -> Result>>, Error>; } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Match { +pub struct Entity { pub score: Option, - pub item: T, + pub entity: T, pub disambiguation: Option, } -impl Match { - pub fn item(item: T) -> Self { - Match { +impl Entity { + pub fn new(entity: T) -> Self { + Entity { score: None, - item, + entity, disambiguation: None, } } diff --git a/src/tui/ui/display.rs b/src/tui/ui/display.rs index cfc78df..6684fff 100644 --- a/src/tui/ui/display.rs +++ b/src/tui/ui/display.rs @@ -141,7 +141,7 @@ impl UiDisplay { match match_option { MatchOption::Some(match_artist) => format!( "{}{}", - display_fn(&match_artist.item, &match_artist.disambiguation), + display_fn(&match_artist.entity, &match_artist.disambiguation), Self::display_option_score(match_artist.score), ), MatchOption::CannotHaveMbid => Self::display_cannot_have_mbid().to_string(), diff --git a/src/tui/ui/match_state.rs b/src/tui/ui/match_state.rs index 741ec26..59f9dae 100644 --- a/src/tui/ui/match_state.rs +++ b/src/tui/ui/match_state.rs @@ -22,7 +22,7 @@ impl<'a, 'b> MatchOverlay<'a, 'b> { fn artists( matching: &ArtistMeta, - matches: &'a Vec>, + matches: &'a [MatchOption], state: &'b mut WidgetState, ) -> Self { let matching = UiDisplay::display_artist_matching(matching); @@ -38,7 +38,7 @@ impl<'a, 'b> MatchOverlay<'a, 'b> { fn albums( matching: &AlbumMeta, - matches: &'a Vec>, + matches: &'a [MatchOption], state: &'b mut WidgetState, ) -> Self { let matching = UiDisplay::display_album_matching(matching); diff --git a/src/tui/ui/mod.rs b/src/tui/ui/mod.rs index 4827a3d..5dc4c8e 100644 --- a/src/tui/ui/mod.rs +++ b/src/tui/ui/mod.rs @@ -204,7 +204,7 @@ mod tests { use crate::tui::{ app::{AppPublic, AppPublicInner, Delta, MatchStatePublic}, - lib::interface::musicbrainz::api::Match, + lib::interface::musicbrainz::api::Entity, testmod::COLLECTION, tests::terminal, }; @@ -331,7 +331,7 @@ mod tests { fn artist_matches() -> MatchStateInfo { let artist = artist_meta(); - let artist_match = Match::with_score(artist.clone(), 80); + let artist_match = Entity::with_score(artist.clone(), 80); let list = vec![artist_match.clone(), artist_match.clone()]; let mut info = MatchStateInfo::artist_search(artist, list); @@ -342,7 +342,7 @@ mod tests { fn artist_lookup() -> MatchStateInfo { let artist = artist_meta(); - let artist_lookup = Match::item(artist.clone()); + let artist_lookup = Entity::new(artist.clone()); let mut info = MatchStateInfo::artist_lookup(artist, artist_lookup); info.push_cannot_have_mbid(); @@ -369,7 +369,7 @@ mod tests { fn album_matches() -> MatchStateInfo { let artist_id = album_artist_id(); let album = album_meta(); - let album_match = Match::with_score(album.clone(), 80); + let album_match = Entity::with_score(album.clone(), 80); let list = vec![album_match.clone(), album_match.clone()]; let mut info = MatchStateInfo::album_search(artist_id, album, list); @@ -381,7 +381,7 @@ mod tests { fn album_lookup() -> MatchStateInfo { let artist_id = album_artist_id(); let album = album_meta(); - let album_lookup = Match::item(album.clone()); + let album_lookup = Entity::new(album.clone()); let mut info = MatchStateInfo::album_lookup(artist_id, album, album_lookup); info.push_cannot_have_mbid();