Errors and types

This commit is contained in:
Wojciech Kozlowski 2024-09-21 16:09:39 +02:00
parent f4745b5d68
commit a67e6b937f
2 changed files with 29 additions and 18 deletions

View File

@ -8,11 +8,10 @@ use musichoard::collection::{artist::Artist, musicbrainz::IMusicBrainzRef};
use crate::tui::{ use crate::tui::{
app::{ app::{
machine::{match_state::MatchState, App, AppInner, AppMachine}, machine::{match_state::MatchState, App, AppInner, AppMachine},
AppPublicState, AppState, IAppEventFetch, IAppInteractFetch, MatchStateInfo, AppPublicState, AppState, IAppEventFetch, IAppInteractFetch,
}, },
lib::{ lib::external::musicbrainz::daemon::{
external::musicbrainz::daemon::{MbParams, ResultSender}, Error as DaemonError, IMbJobSender, MbApiResult, MbParams, ResultSender,
interface::musicbrainz::api::Error as MbError,
}, },
}; };
@ -20,16 +19,13 @@ pub struct FetchState {
fetch_rx: FetchReceiver, fetch_rx: FetchReceiver,
} }
pub type FetchReceiver = mpsc::Receiver<MbApiResult>;
impl FetchState { impl FetchState {
pub fn new(fetch_rx: FetchReceiver) -> Self { pub fn new(fetch_rx: FetchReceiver) -> Self {
FetchState { fetch_rx } FetchState { fetch_rx }
} }
} }
pub type FetchError = MbError;
pub type FetchResult = Result<MatchStateInfo, FetchError>;
pub type FetchReceiver = mpsc::Receiver<FetchResult>;
impl AppMachine<FetchState> { impl AppMachine<FetchState> {
fn fetch_state(inner: AppInner, state: FetchState) -> Self { fn fetch_state(inner: AppInner, state: FetchState) -> Self {
AppMachine::new(inner, state) AppMachine::new(inner, state)
@ -44,8 +40,10 @@ impl AppMachine<FetchState> {
} }
}; };
let (fetch_tx, fetch_rx) = mpsc::channel::<FetchResult>(); let (fetch_tx, fetch_rx) = mpsc::channel::<MbApiResult>();
Self::submit_fetch_job(&inner, artist, fetch_tx); if let Err(err) = Self::submit_fetch_job(&inner.musicbrainz, fetch_tx, artist) {
return AppMachine::error_state(inner, err.to_string()).into();
}
let fetch = FetchState::new(fetch_rx); let fetch = FetchState::new(fetch_rx);
AppMachine::app_fetch(inner, fetch, true) AppMachine::app_fetch(inner, fetch, true)
@ -79,19 +77,22 @@ impl AppMachine<FetchState> {
} }
} }
fn submit_fetch_job(inner: &AppInner, artist: &Artist, tx: ResultSender) { fn submit_fetch_job(
let queue = match artist.meta.musicbrainz { musicbrainz: &Box<dyn IMbJobSender>,
result_sender: ResultSender,
artist: &Artist,
) -> Result<(), DaemonError> {
let requests = match artist.meta.musicbrainz {
Some(ref arid) => { Some(ref arid) => {
let arid = arid.mbid(); let arid = arid.mbid();
artist let albums = artist.albums.iter();
.albums albums
.iter()
.map(|album| MbParams::search_release_group(arid.clone(), album.meta.clone())) .map(|album| MbParams::search_release_group(arid.clone(), album.meta.clone()))
.collect() .collect()
} }
None => VecDeque::from([MbParams::search_artist(artist.meta.clone())]), None => VecDeque::from([MbParams::search_artist(artist.meta.clone())]),
}; };
inner.musicbrainz.submit_background_job(tx, queue); musicbrainz.submit_background_job(result_sender, requests)
} }
} }

View File

@ -1,4 +1,4 @@
use std::{collections::VecDeque, sync::mpsc, thread, time}; use std::{collections::VecDeque, fmt, sync::mpsc, thread, time};
use musichoard::collection::{album::AlbumMeta, artist::ArtistMeta, musicbrainz::Mbid}; use musichoard::collection::{album::AlbumMeta, artist::ArtistMeta, musicbrainz::Mbid};
@ -13,6 +13,15 @@ pub enum Error {
JobChannelDisconnected, JobChannelDisconnected,
} }
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::EventChannelDisconnected => write!(f, "the event channel is disconnected"),
Error::JobChannelDisconnected => write!(f, "the job channel is disconnected"),
}
}
}
pub struct MusicBrainzDaemon { pub struct MusicBrainzDaemon {
musicbrainz: Box<dyn IMusicBrainz>, musicbrainz: Box<dyn IMusicBrainz>,
job_receiver: mpsc::Receiver<Job>, job_receiver: mpsc::Receiver<Job>,
@ -46,7 +55,8 @@ enum JobPriority {
Background, Background,
} }
pub type ResultSender = mpsc::Sender<Result<MatchStateInfo, ApiError>>; pub type MbApiResult = Result<MatchStateInfo, ApiError>;
pub type ResultSender = mpsc::Sender<MbApiResult>;
struct JobInstance { struct JobInstance {
result_sender: ResultSender, result_sender: ResultSender,
requests: VecDeque<MbParams>, requests: VecDeque<MbParams>,