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

View File

@ -10,42 +10,39 @@ use crate::tui::{
app::{
machine::{fetch_state::FetchState, input::Input, App, AppInner, AppMachine},
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(
self,
music_hoard: &mut dyn IMusicHoard,
meta: &ArtistMeta,
) -> Result<(), musichoard::Error> {
let mbref = match self {
LookupOption::Match(lookup) => lookup.item.musicbrainz,
LookupOption::None(MissOption::CannotHaveMbid) => MbRefOption::CannotHaveMbid,
_ => panic!(),
MatchOption::Some(m) => m.item.musicbrainz,
MatchOption::CannotHaveMbid => MbRefOption::CannotHaveMbid,
MatchOption::ManualInputMbid => panic!(),
};
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(
self,
music_hoard: &mut dyn IMusicHoard,
@ -53,15 +50,13 @@ impl LookupOption<AlbumMeta> {
meta: &AlbumMeta,
) -> Result<(), musichoard::Error> {
let (mbref, primary_type, secondary_types) = match self {
LookupOption::Match(lookup) => (
lookup.item.musicbrainz,
lookup.item.primary_type,
lookup.item.secondary_types,
MatchOption::Some(m) => (
m.item.musicbrainz,
m.item.primary_type,
m.item.secondary_types,
),
LookupOption::None(MissOption::CannotHaveMbid) => {
(MbRefOption::CannotHaveMbid, None, Vec::new())
}
_ => panic!(),
MatchOption::CannotHaveMbid => (MbRefOption::CannotHaveMbid, None, Vec::new()),
MatchOption::ManualInputMbid => panic!(),
};
music_hoard.set_album_musicbrainz(artist, &meta.id, mbref)?;
music_hoard.set_album_primary_type(artist, &meta.id, primary_type)?;
@ -69,31 +64,11 @@ impl LookupOption<AlbumMeta> {
Ok(())
}
}
};
}
impl SearchOption<AlbumMeta> {
fn set(
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(())
}
}
item_option_album_set!(Lookup);
item_option_album_set!(Match);
impl<T: PartialEq> ListOption<T> {
fn len(&self) -> usize {
@ -105,26 +80,22 @@ impl<T: PartialEq> ListOption<T> {
fn push_cannot_have_mbid(&mut self) {
match self {
ListOption::Lookup(list) => list.push(LookupOption::None(MissOption::CannotHaveMbid)),
ListOption::Search(list) => list.push(SearchOption::None(MissOption::CannotHaveMbid)),
ListOption::Lookup(list) => list.push(MatchOption::CannotHaveMbid),
ListOption::Search(list) => list.push(MatchOption::CannotHaveMbid),
}
}
fn push_manual_input_mbid(&mut self) {
match self {
ListOption::Lookup(list) => list.push(LookupOption::None(MissOption::ManualInputMbid)),
ListOption::Search(list) => list.push(SearchOption::None(MissOption::ManualInputMbid)),
ListOption::Lookup(list) => list.push(MatchOption::ManualInputMbid),
ListOption::Search(list) => list.push(MatchOption::ManualInputMbid),
}
}
fn is_manual_input_mbid(&self, index: usize) -> bool {
match self {
ListOption::Lookup(list) => {
list.get(index) == Some(&LookupOption::None(MissOption::ManualInputMbid))
}
ListOption::Search(list) => {
list.get(index) == Some(&SearchOption::None(MissOption::ManualInputMbid))
}
ListOption::Lookup(list) => list.get(index) == Some(&MatchOption::ManualInputMbid),
ListOption::Search(list) => list.get(index) == Some(&MatchOption::ManualInputMbid),
}
}
}

View File

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

View File

@ -5,7 +5,10 @@ use musichoard::collection::{
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;
@ -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 {
SearchOption::Match(match_artist) => format!(
MatchOption::Some(match_artist) => format!(
"{} ({}%)",
Self::display_option_artist(&match_artist.item, &match_artist.disambiguation),
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 {
LookupOption::Match(match_artist) => {
MatchOption::Some(match_artist) => {
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 {
SearchOption::Match(match_album) => format!(
MatchOption::Some(match_album) => format!(
"{} ({}%)",
Self::display_option_album(&match_album.item),
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 {
LookupOption::Match(match_album) => Self::display_option_album(&match_album.item),
LookupOption::None(miss) => Self::display_miss_option(miss).to_string(),
MatchOption::Some(match_album) => Self::display_option_album(&match_album.item),
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 {
"-- Cannot have a MusicBrainz Identifier --"
}