Separate interface from implementation

This commit is contained in:
Wojciech Kozlowski 2024-09-21 19:10:00 +02:00
parent 41199f4750
commit 39bd5e7935
5 changed files with 71 additions and 63 deletions

View File

@ -10,7 +10,7 @@ use crate::tui::{
machine::{match_state::MatchState, App, AppInner, AppMachine},
AppPublicState, AppState, IAppEventFetch, IAppInteractFetch,
},
lib::external::musicbrainz::daemon::{
lib::interface::musicbrainz::daemon::{
Error as DaemonError, IMbJobSender, MbApiResult, MbParams, ResultSender,
},
};

View File

@ -13,7 +13,7 @@ use crate::tui::{
selection::Selection, AppMode, AppPublic, AppPublicInner, AppPublicState, AppState, IApp,
IAppAccess, IAppBase, IAppState,
},
lib::{external::musicbrainz::daemon::IMbJobSender, IMusicHoard},
lib::{interface::musicbrainz::daemon::IMbJobSender, IMusicHoard},
};
use browse_state::BrowseState;

View File

@ -1,25 +1,17 @@
use std::{collections::VecDeque, fmt, sync::mpsc, thread, time};
use musichoard::collection::{album::AlbumMeta, artist::ArtistMeta, musicbrainz::Mbid};
use std::{collections::VecDeque, sync::mpsc, thread, time};
use crate::tui::{
app::MatchStateInfo,
event::IFetchCompleteEventSender,
lib::interface::musicbrainz::api::{Error as ApiError, IMusicBrainz},
lib::interface::musicbrainz::{
api::{Error as ApiError, IMusicBrainz},
daemon::{Error, IMbJobSender, MbParams, ResultSender, SearchParams},
},
};
pub enum Error {
enum JobError {
JobQueueEmpty,
EventChannelDisconnected,
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 {
@ -29,11 +21,6 @@ pub struct MusicBrainzDaemon {
event_sender: Box<dyn IFetchCompleteEventSender>,
}
enum JobError {
JobQueueEmpty,
EventChannelDisconnected,
}
struct JobQueue {
foreground_queue: VecDeque<JobInstance>,
background_queue: VecDeque<JobInstance>,
@ -55,8 +42,6 @@ enum JobPriority {
Background,
}
pub type MbApiResult = Result<MatchStateInfo, ApiError>;
pub type ResultSender = mpsc::Sender<MbApiResult>;
struct JobInstance {
result_sender: ResultSender,
requests: VecDeque<MbParams>,
@ -81,37 +66,6 @@ impl JobInstance {
}
}
pub enum MbParams {
Search(SearchParams),
}
impl MbParams {
pub fn search_artist(artist: ArtistMeta) -> Self {
MbParams::Search(SearchParams::Artist(SearchArtistParams { artist }))
}
pub fn search_release_group(arid: Mbid, album: AlbumMeta) -> Self {
MbParams::Search(SearchParams::ReleaseGroup(SearchReleaseGroupParams {
arid,
album,
}))
}
}
pub enum SearchParams {
Artist(SearchArtistParams),
ReleaseGroup(SearchReleaseGroupParams),
}
pub struct SearchArtistParams {
artist: ArtistMeta,
}
pub struct SearchReleaseGroupParams {
arid: Mbid,
album: AlbumMeta,
}
pub struct JobChannel {
sender: mpsc::Sender<Job>,
receiver: mpsc::Receiver<Job>,
@ -144,14 +98,6 @@ impl JobChannel {
}
}
pub trait IMbJobSender {
fn submit_background_job(
&self,
result_sender: ResultSender,
requests: VecDeque<MbParams>,
) -> Result<(), Error>;
}
impl IMbJobSender for JobSender {
fn submit_background_job(
&self,

View File

@ -0,0 +1,61 @@
use std::{collections::VecDeque, fmt, sync::mpsc};
use musichoard::collection::{album::AlbumMeta, artist::ArtistMeta, musicbrainz::Mbid};
use crate::tui::{app::MatchStateInfo, lib::interface::musicbrainz::api::Error as MbApiError};
pub enum Error {
EventChannelDisconnected,
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 type MbApiResult = Result<MatchStateInfo, MbApiError>;
pub type ResultSender = mpsc::Sender<MbApiResult>;
pub trait IMbJobSender {
fn submit_background_job(
&self,
result_sender: ResultSender,
requests: VecDeque<MbParams>,
) -> Result<(), Error>;
}
pub enum MbParams {
Search(SearchParams),
}
pub enum SearchParams {
Artist(SearchArtistParams),
ReleaseGroup(SearchReleaseGroupParams),
}
pub struct SearchArtistParams {
pub artist: ArtistMeta,
}
pub struct SearchReleaseGroupParams {
pub arid: Mbid,
pub album: AlbumMeta,
}
impl MbParams {
pub fn search_artist(artist: ArtistMeta) -> Self {
MbParams::Search(SearchParams::Artist(SearchArtistParams { artist }))
}
pub fn search_release_group(arid: Mbid, album: AlbumMeta) -> Self {
MbParams::Search(SearchParams::ReleaseGroup(SearchReleaseGroupParams {
arid,
album,
}))
}
}

View File

@ -1 +1,2 @@
pub mod api;
pub mod daemon;