Remove duplication re: miss options
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 2m22s
Cargo CI / Lint (pull_request) Successful in 1m9s

This commit is contained in:
Wojciech Kozlowski 2024-09-28 09:08:56 +02:00
parent a26ae891ff
commit c6424ec036
4 changed files with 94 additions and 137 deletions

View File

@ -246,7 +246,7 @@ mod tests {
use crate::tui::{ use crate::tui::{
app::{ app::{
machine::tests::{inner, music_hoard}, machine::tests::{inner, music_hoard},
Delta, IApp, IAppAccess, IAppInteractBrowse, MatchStateInfo, MissOption, SearchOption, Delta, IApp, IAppAccess, IAppInteractBrowse, MatchOption, MatchStateInfo,
}, },
lib::interface::musicbrainz::{ lib::interface::musicbrainz::{
self, self,
@ -503,8 +503,8 @@ mod tests {
let match_state = public.state.unwrap_match(); let match_state = public.state.unwrap_match();
let match_options = vec![ let match_options = vec![
artist_match.into(), artist_match.into(),
SearchOption::None(MissOption::CannotHaveMbid), MatchOption::CannotHaveMbid,
SearchOption::None(MissOption::ManualInputMbid), MatchOption::ManualInputMbid,
]; ];
let expected = MatchStateInfo::artist_search(artist, match_options); let expected = MatchStateInfo::artist_search(artist, match_options);
assert_eq!(match_state.info, Some(expected).as_ref()); assert_eq!(match_state.info, Some(expected).as_ref());

View File

@ -10,42 +10,39 @@ use crate::tui::{
app::{ app::{
machine::{fetch_state::FetchState, input::Input, App, AppInner, AppMachine}, machine::{fetch_state::FetchState, input::Input, App, AppInner, AppMachine},
AlbumMatches, AppPublicState, AppState, ArtistMatches, IAppInteractMatch, ListOption, AlbumMatches, AppPublicState, AppState, ArtistMatches, IAppInteractMatch, ListOption,
LookupOption, MatchStateInfo, MatchStatePublic, MissOption, SearchOption, WidgetState, MatchOption, MatchStateInfo, MatchStatePublic, WidgetState,
},
lib::{
interface::musicbrainz::api::{Lookup, Match},
IMusicHoard,
}, },
lib::IMusicHoard,
}; };
impl LookupOption<ArtistMeta> { macro_rules! item_option_artist_set {
($holder:ident) => {
impl MatchOption<$holder<ArtistMeta>> {
fn set( fn set(
self, self,
music_hoard: &mut dyn IMusicHoard, music_hoard: &mut dyn IMusicHoard,
meta: &ArtistMeta, meta: &ArtistMeta,
) -> Result<(), musichoard::Error> { ) -> Result<(), musichoard::Error> {
let mbref = match self { let mbref = match self {
LookupOption::Match(lookup) => lookup.item.musicbrainz, MatchOption::Some(m) => m.item.musicbrainz,
LookupOption::None(MissOption::CannotHaveMbid) => MbRefOption::CannotHaveMbid, MatchOption::CannotHaveMbid => MbRefOption::CannotHaveMbid,
_ => panic!(), MatchOption::ManualInputMbid => panic!(),
}; };
music_hoard.set_artist_musicbrainz(&meta.id, mbref) music_hoard.set_artist_musicbrainz(&meta.id, mbref)
} }
}
impl SearchOption<ArtistMeta> {
fn set(
self,
music_hoard: &mut dyn IMusicHoard,
meta: &ArtistMeta,
) -> Result<(), musichoard::Error> {
let mbref = match self {
SearchOption::Match(search) => search.item.musicbrainz,
SearchOption::None(MissOption::CannotHaveMbid) => MbRefOption::CannotHaveMbid,
_ => panic!(),
};
music_hoard.set_artist_musicbrainz(&meta.id, mbref)
} }
};
} }
impl LookupOption<AlbumMeta> { item_option_artist_set!(Lookup);
item_option_artist_set!(Match);
macro_rules! item_option_album_set {
($holder:ident) => {
impl MatchOption<$holder<AlbumMeta>> {
fn set( fn set(
self, self,
music_hoard: &mut dyn IMusicHoard, music_hoard: &mut dyn IMusicHoard,
@ -53,47 +50,25 @@ impl LookupOption<AlbumMeta> {
meta: &AlbumMeta, meta: &AlbumMeta,
) -> Result<(), musichoard::Error> { ) -> Result<(), musichoard::Error> {
let (mbref, primary_type, secondary_types) = match self { let (mbref, primary_type, secondary_types) = match self {
LookupOption::Match(lookup) => ( MatchOption::Some(m) => (
lookup.item.musicbrainz, m.item.musicbrainz,
lookup.item.primary_type, m.item.primary_type,
lookup.item.secondary_types, m.item.secondary_types,
), ),
LookupOption::None(MissOption::CannotHaveMbid) => { MatchOption::CannotHaveMbid => (MbRefOption::CannotHaveMbid, None, Vec::new()),
(MbRefOption::CannotHaveMbid, None, Vec::new()) MatchOption::ManualInputMbid => panic!(),
}
_ => panic!(),
}; };
music_hoard.set_album_musicbrainz(artist, &meta.id, mbref)?; music_hoard.set_album_musicbrainz(artist, &meta.id, mbref)?;
music_hoard.set_album_primary_type(artist, &meta.id, primary_type)?; music_hoard.set_album_primary_type(artist, &meta.id, primary_type)?;
music_hoard.set_album_secondary_types(artist, &meta.id, secondary_types)?; music_hoard.set_album_secondary_types(artist, &meta.id, secondary_types)?;
Ok(()) Ok(())
} }
}
};
} }
impl SearchOption<AlbumMeta> { item_option_album_set!(Lookup);
fn set( item_option_album_set!(Match);
self,
music_hoard: &mut dyn IMusicHoard,
artist: &ArtistId,
meta: &AlbumMeta,
) -> Result<(), musichoard::Error> {
let (mbref, primary_type, secondary_types) = match self {
SearchOption::Match(lookup) => (
lookup.item.musicbrainz,
lookup.item.primary_type,
lookup.item.secondary_types,
),
SearchOption::None(MissOption::CannotHaveMbid) => {
(MbRefOption::CannotHaveMbid, None, Vec::new())
}
_ => panic!(),
};
music_hoard.set_album_musicbrainz(artist, &meta.id, mbref)?;
music_hoard.set_album_primary_type(artist, &meta.id, primary_type)?;
music_hoard.set_album_secondary_types(artist, &meta.id, secondary_types)?;
Ok(())
}
}
impl<T: PartialEq> ListOption<T> { impl<T: PartialEq> ListOption<T> {
fn len(&self) -> usize { fn len(&self) -> usize {
@ -105,26 +80,22 @@ impl<T: PartialEq> ListOption<T> {
fn push_cannot_have_mbid(&mut self) { fn push_cannot_have_mbid(&mut self) {
match self { match self {
ListOption::Lookup(list) => list.push(LookupOption::None(MissOption::CannotHaveMbid)), ListOption::Lookup(list) => list.push(MatchOption::CannotHaveMbid),
ListOption::Search(list) => list.push(SearchOption::None(MissOption::CannotHaveMbid)), ListOption::Search(list) => list.push(MatchOption::CannotHaveMbid),
} }
} }
fn push_manual_input_mbid(&mut self) { fn push_manual_input_mbid(&mut self) {
match self { match self {
ListOption::Lookup(list) => list.push(LookupOption::None(MissOption::ManualInputMbid)), ListOption::Lookup(list) => list.push(MatchOption::ManualInputMbid),
ListOption::Search(list) => list.push(SearchOption::None(MissOption::ManualInputMbid)), ListOption::Search(list) => list.push(MatchOption::ManualInputMbid),
} }
} }
fn is_manual_input_mbid(&self, index: usize) -> bool { fn is_manual_input_mbid(&self, index: usize) -> bool {
match self { match self {
ListOption::Lookup(list) => { ListOption::Lookup(list) => list.get(index) == Some(&MatchOption::ManualInputMbid),
list.get(index) == Some(&LookupOption::None(MissOption::ManualInputMbid)) ListOption::Search(list) => list.get(index) == Some(&MatchOption::ManualInputMbid),
}
ListOption::Search(list) => {
list.get(index) == Some(&SearchOption::None(MissOption::ManualInputMbid))
}
} }
} }
} }

View File

@ -181,38 +181,21 @@ pub struct AppPublicInner<'app> {
pub type InputPublic<'app> = &'app tui_input::Input; pub type InputPublic<'app> = &'app tui_input::Input;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum MissOption { pub enum MatchOption<T> {
Some(T),
CannotHaveMbid, CannotHaveMbid,
ManualInputMbid, ManualInputMbid,
} }
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SearchOption<T> {
Match(Match<T>),
None(MissOption),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LookupOption<T> {
Match(Lookup<T>),
None(MissOption),
}
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum ListOption<T> { pub enum ListOption<T> {
Search(Vec<SearchOption<T>>), Search(Vec<MatchOption<Match<T>>>),
Lookup(Vec<LookupOption<T>>), Lookup(Vec<MatchOption<Lookup<T>>>),
} }
impl<T> From<Match<T>> for SearchOption<T> { impl<T> From<T> for MatchOption<T> {
fn from(value: Match<T>) -> Self { fn from(value: T) -> Self {
SearchOption::Match(value) MatchOption::Some(value)
}
}
impl<T> From<Lookup<T>> for LookupOption<T> {
fn from(value: Lookup<T>) -> Self {
LookupOption::Match(value)
} }
} }
@ -236,7 +219,7 @@ pub enum MatchStateInfo {
} }
impl MatchStateInfo { impl MatchStateInfo {
pub fn artist_search<M: Into<SearchOption<ArtistMeta>>>( pub fn artist_search<M: Into<MatchOption<Match<ArtistMeta>>>>(
matching: ArtistMeta, matching: ArtistMeta,
list: Vec<M>, list: Vec<M>,
) -> Self { ) -> Self {
@ -244,7 +227,7 @@ impl MatchStateInfo {
MatchStateInfo::Artist(ArtistMatches { matching, list }) MatchStateInfo::Artist(ArtistMatches { matching, list })
} }
pub fn album_search<M: Into<SearchOption<AlbumMeta>>>( pub fn album_search<M: Into<MatchOption<Match<AlbumMeta>>>>(
artist: ArtistId, artist: ArtistId,
matching: AlbumMeta, matching: AlbumMeta,
list: Vec<M>, list: Vec<M>,
@ -257,12 +240,15 @@ impl MatchStateInfo {
}) })
} }
pub fn artist_lookup<M: Into<LookupOption<ArtistMeta>>>(matching: ArtistMeta, item: M) -> Self { pub fn artist_lookup<M: Into<MatchOption<Lookup<ArtistMeta>>>>(
matching: ArtistMeta,
item: M,
) -> Self {
let list = ListOption::Lookup(vec![item.into()]); let list = ListOption::Lookup(vec![item.into()]);
MatchStateInfo::Artist(ArtistMatches { matching, list }) MatchStateInfo::Artist(ArtistMatches { matching, list })
} }
pub fn album_lookup<M: Into<LookupOption<AlbumMeta>>>( pub fn album_lookup<M: Into<MatchOption<Lookup<AlbumMeta>>>>(
artist: ArtistId, artist: ArtistId,
matching: AlbumMeta, matching: AlbumMeta,
item: M, item: M,

View File

@ -5,7 +5,10 @@ use musichoard::collection::{
track::{TrackFormat, TrackQuality}, track::{TrackFormat, TrackQuality},
}; };
use crate::tui::app::{LookupOption, MatchStateInfo, MissOption, SearchOption}; use crate::tui::{
app::{MatchOption, MatchStateInfo},
lib::interface::musicbrainz::api::{Lookup, Match},
};
pub struct UiDisplay; pub struct UiDisplay;
@ -133,23 +136,25 @@ impl UiDisplay {
} }
} }
pub fn display_search_option_artist(match_option: &SearchOption<ArtistMeta>) -> String { pub fn display_search_option_artist(match_option: &MatchOption<Match<ArtistMeta>>) -> String {
match match_option { match match_option {
SearchOption::Match(match_artist) => format!( MatchOption::Some(match_artist) => format!(
"{} ({}%)", "{} ({}%)",
Self::display_option_artist(&match_artist.item, &match_artist.disambiguation), Self::display_option_artist(&match_artist.item, &match_artist.disambiguation),
match_artist.score, match_artist.score,
), ),
SearchOption::None(miss) => Self::display_miss_option(miss).to_string(), MatchOption::CannotHaveMbid => Self::display_cannot_have_mbid().to_string(),
MatchOption::ManualInputMbid => Self::display_manual_input_mbid().to_string(),
} }
} }
pub fn display_lookup_option_artist(lookup_option: &LookupOption<ArtistMeta>) -> String { pub fn display_lookup_option_artist(lookup_option: &MatchOption<Lookup<ArtistMeta>>) -> String {
match lookup_option { match lookup_option {
LookupOption::Match(match_artist) => { MatchOption::Some(match_artist) => {
Self::display_option_artist(&match_artist.item, &match_artist.disambiguation) Self::display_option_artist(&match_artist.item, &match_artist.disambiguation)
} }
LookupOption::None(miss) => Self::display_miss_option(miss).to_string(), MatchOption::CannotHaveMbid => Self::display_cannot_have_mbid().to_string(),
MatchOption::ManualInputMbid => Self::display_manual_input_mbid().to_string(),
} }
} }
@ -165,21 +170,23 @@ impl UiDisplay {
) )
} }
pub fn display_search_option_album(match_option: &SearchOption<AlbumMeta>) -> String { pub fn display_search_option_album(match_option: &MatchOption<Match<AlbumMeta>>) -> String {
match match_option { match match_option {
SearchOption::Match(match_album) => format!( MatchOption::Some(match_album) => format!(
"{} ({}%)", "{} ({}%)",
Self::display_option_album(&match_album.item), Self::display_option_album(&match_album.item),
match_album.score, match_album.score,
), ),
SearchOption::None(miss) => Self::display_miss_option(miss).to_string(), MatchOption::CannotHaveMbid => Self::display_cannot_have_mbid().to_string(),
MatchOption::ManualInputMbid => Self::display_manual_input_mbid().to_string(),
} }
} }
pub fn display_lookup_option_album(lookup_option: &LookupOption<AlbumMeta>) -> String { pub fn display_lookup_option_album(lookup_option: &MatchOption<Lookup<AlbumMeta>>) -> String {
match lookup_option { match lookup_option {
LookupOption::Match(match_album) => Self::display_option_album(&match_album.item), MatchOption::Some(match_album) => Self::display_option_album(&match_album.item),
LookupOption::None(miss) => Self::display_miss_option(miss).to_string(), MatchOption::CannotHaveMbid => Self::display_cannot_have_mbid().to_string(),
MatchOption::ManualInputMbid => Self::display_manual_input_mbid().to_string(),
} }
} }
@ -192,13 +199,6 @@ impl UiDisplay {
) )
} }
fn display_miss_option(miss_option: &MissOption) -> &'static str {
match miss_option {
MissOption::CannotHaveMbid => Self::display_cannot_have_mbid(),
MissOption::ManualInputMbid => Self::display_manual_input_mbid(),
}
}
fn display_cannot_have_mbid() -> &'static str { fn display_cannot_have_mbid() -> &'static str {
"-- Cannot have a MusicBrainz Identifier --" "-- Cannot have a MusicBrainz Identifier --"
} }