diff --git a/src/rust/amadeus-rs/amadeus/src/amadeus.rs b/src/rust/amadeus-rs/amadeus/src/amadeus.rs index 235b6fff78033409d6d5b2e8d3e66f7db832a1a8..cc4f2e74db4d7b032025e759faaddf5a2aeb1992 100644 --- a/src/rust/amadeus-rs/amadeus/src/amadeus.rs +++ b/src/rust/amadeus-rs/amadeus/src/amadeus.rs @@ -7,7 +7,7 @@ use crate::{ }; use amadeus_macro::either; use eframe::{egui, App}; -use lkt_lib::{Kara, LektorFormatedResponse, LektorQuery, LektorState, Playlist}; +use lkt_lib::{Kara, LektorQuery, LektorResponse, LektorState, Playlist}; use log::debug; use std::{ sync::mpsc::{Receiver, Sender}, @@ -15,7 +15,7 @@ use std::{ }; type CommandDeamonData = ( - (Sender<LektorQuery>, Receiver<LektorFormatedResponse>), + (Sender<LektorQuery>, Receiver<LektorResponse>), utils::deamon::CommandDeamon, ); diff --git a/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs b/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs index a929b26ac7fddd95d69ec0e58fd8a9e1adf35d1b..8720045d894f3bd1c2a75b8178b2d64847efd279 100644 --- a/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs +++ b/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs @@ -87,14 +87,14 @@ macro_rules! implement_deamon_joined { } impl Deamon for CommandDeamon { - type Channels = (Sender<LektorQuery>, Receiver<LektorFormatedResponse>); + type Channels = (Sender<LektorQuery>, Receiver<LektorResponse>); implement_deamon_quit!(); implement_deamon_joined!(); fn spawn(hostname: String, port: i16) -> io::Result<(Self::Channels, Self)> { let mut connexion = LektorConnexion::new(hostname, port)?; - let (responses_send, responses_recv) = channel::<LektorFormatedResponse>(); + let (responses_send, responses_recv) = channel::<LektorResponse>(); let (commands_send, commands_recv) = channel::<LektorQuery>(); let quit = Arc::<AtomicBool>::new(AtomicBool::default()); let joined = Arc::<AtomicBool>::new(AtomicBool::default()); @@ -152,48 +152,33 @@ impl Deamon for StatusDeamon { thread::sleep(time::Duration::from_secs(1)); return_when_flagged!(quit_deamon, joined_deamon); - let status = { - let res = match connexion.send_query(LektorQuery::PlaybackStatus) { - Ok(res) => res, - Err(e) => { - error!("failed to send the playback status command to lektor: {e}"); - continue; - } - }; - match LektorPlaybackStatusResponse::from_formated(res) { - Ok(LektorResponse::PlaybackStatus(res)) => res, - Ok(_) => { - error!("got invalid response from lektor, not a status..."); - continue; - } - Err(e) => { - error!("failed to build response from formated response: {e}"); - continue; - } + let status = match connexion.send_query(LektorQuery::PlaybackStatus) { + Ok(LektorResponse::PlaybackStatus(res)) => res, + Ok(_) => { + error!("got invalid response from lektor, not a status..."); + continue; + } + Err(e) => { + error!("failed to send the playback status command to lektor: {e}"); + continue; } }; - let current = if status.state() != LektorState::Stopped { - let res = match connexion.send_query(LektorQuery::CurrentKara) { - Ok(res) => res, - Err(e) => { - error!("failed to send the current kara command to lektor: {e}",); - continue; - } - }; - match LektorCurrentKaraResponse::from_formated(res) { - Ok(LektorResponse::CurrentKara(res)) => Some(res), - Ok(_) => { - error!("got invalid response from lektor, not a current kara..."); - None - } - Err(err) => { - error!("failed to build response from formated response: {err}"); - None + let current = match status.state() { + LektorState::Stopped => None, + LektorState::Play(_) | LektorState::Pause(_) => { + match connexion.send_query(LektorQuery::CurrentKara) { + Ok(LektorResponse::CurrentKara(res)) => Some(res), + Ok(_) => { + error!("got invalid response from lektor, not a current kara..."); + None + } + Err(e) => { + error!("failed to send the current kara command to lektor: {e}",); + None + } } } - } else { - None }; if let Err(e) = responses_send.send((status, current)) { diff --git a/src/rust/amadeus-rs/lkt-lib/src/connexion.rs b/src/rust/amadeus-rs/lkt-lib/src/connexion.rs index 96d14c2b5ea5785ac418f8897588d1de903f1063..3d8549bf3a2b572564372aa15b2c5b9a1e20eee6 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/connexion.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/connexion.rs @@ -3,6 +3,7 @@ use crate::{ constants, query::LektorQuery, query::LektorQueryLineType, response::LektorFormatedResponse, + LektorResponse, }; use log::error; use std::{ @@ -15,6 +16,7 @@ pub enum LektorQueryError { IO(io::Error), Query(String), Other(String), + ToTyped(String), } impl std::fmt::Display for LektorQueryError { @@ -24,6 +26,7 @@ impl std::fmt::Display for LektorQueryError { Query(err) => write!(f, "lektor query logic error: {err}"), IO(io) => write!(f, "lektor query error io: {io}"), Other(other) => write!(f, "lektor query error: {other}"), + ToTyped(msg) => write!(f, "lektor formated to typed error: {msg}"), } } } @@ -73,15 +76,14 @@ impl LektorConnexion { } } - pub fn send_query( - &mut self, - query: LektorQuery, - ) -> Result<LektorFormatedResponse, LektorQueryError> { + pub fn send_query(&mut self, query: LektorQuery) -> Result<LektorResponse, LektorQueryError> { let mut res: Vec<String> = Vec::new(); query.verify().map_err(LektorQueryError::Query)?; + let builder = query.get_response_type(); self.send_query_inner(query, &mut res) .map_err(LektorQueryError::IO)?; - LektorFormatedResponse::try_from(res).map_err(LektorQueryError::Other) + let formated = LektorFormatedResponse::try_from(res).map_err(LektorQueryError::Other)?; + builder(formated).map_err(LektorQueryError::ToTyped) } fn send_query_inner( diff --git a/src/rust/amadeus-rs/lkt-lib/src/query.rs b/src/rust/amadeus-rs/lkt-lib/src/query.rs index 55eee15ffd0e3b2d72713e3f0fa3bf54273ea807..ef494bb8441ac51745dd94770e9a1f5c328d4969 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/query.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/query.rs @@ -92,7 +92,7 @@ impl LektorQuery { Ping | Close | KillServer | PlayNext | PlayPrevious | ShuffleQueue | InsertKara(_) | AddKara(_) => LektorEmptyResponse::from_formated, - ListAllPlaylists => LektorPlaylistListResponse::from_formated, + ListAllPlaylists => LektorPlaylistSetResponse::from_formated, PlaybackStatus => LektorPlaybackStatusResponse::from_formated, CurrentKara => LektorCurrentKaraResponse::from_formated, diff --git a/src/rust/amadeus-rs/lkt-lib/src/response.rs b/src/rust/amadeus-rs/lkt-lib/src/response.rs index 0bcffefa4644635dd0a111cb684ef638e13dc09e..fe9a9e89f30e376da6aecbc5cc0fb6207b763067 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/response.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/response.rs @@ -83,7 +83,7 @@ impl TryFrom<Vec<String>> for LektorFormatedResponse { pub enum LektorResponse { PlaybackStatus(LektorPlaybackStatusResponse), CurrentKara(LektorCurrentKaraResponse), - PlaylistList(LektorPlaylistListResponse), + PlaylistSet(LektorPlaylistSetResponse), EmptyResponse(LektorEmptyResponse), KaraSet(LektorKaraSetResponse), } @@ -104,7 +104,7 @@ mod private { pub trait Sealed {} impl Sealed for LektorPlaybackStatusResponse {} impl Sealed for LektorCurrentKaraResponse {} - impl Sealed for LektorPlaylistListResponse {} + impl Sealed for LektorPlaylistSetResponse {} impl Sealed for LektorKaraSetResponse {} impl Sealed for LektorEmptyResponse {} } @@ -140,7 +140,7 @@ pub struct LektorPlaybackStatusResponse { } #[derive(Debug)] -pub struct LektorPlaylistListResponse { +pub struct LektorPlaylistSetResponse { playlists: Vec<String>, } @@ -203,7 +203,7 @@ impl LektorKaraSetResponse { } } -impl LektorPlaylistListResponse { +impl LektorPlaylistSetResponse { pub fn iter(&self) -> &[String] { &self.playlists[..] } @@ -217,7 +217,7 @@ impl IntoIterator for LektorKaraSetResponse { } } -impl IntoIterator for LektorPlaylistListResponse { +impl IntoIterator for LektorPlaylistSetResponse { type Item = String; type IntoIter = <std::vec::Vec<std::string::String> as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { @@ -244,9 +244,9 @@ impl FromLektorResponse for LektorEmptyResponse { } } -impl FromLektorResponse for LektorPlaylistListResponse { +impl FromLektorResponse for LektorPlaylistSetResponse { fn from_formated(mut response: LektorFormatedResponse) -> Result<LektorResponse, String> { - Ok(LektorResponse::PlaylistList(Self { + Ok(LektorResponse::PlaylistSet(Self { playlists: response.pop_all("name"), })) } diff --git a/src/rust/amadeus-rs/lkt-rs/src/main.rs b/src/rust/amadeus-rs/lkt-rs/src/main.rs index 56dd5bfbd049dec74d8f0cff50c1dfb0c33d4e6b..7be725a37e9773f35e73660b2fdd2e7787f63b0d 100644 --- a/src/rust/amadeus-rs/lkt-rs/src/main.rs +++ b/src/rust/amadeus-rs/lkt-rs/src/main.rs @@ -4,18 +4,18 @@ fn main() { let mut lektor = LektorConnexion::new("localhost".to_owned(), 6600).unwrap(); if lektor.send_query(LektorQuery::Ping).is_ok() {} - if let Ok(response) = lektor.send_query(LektorQuery::CurrentKara) { - let current_kara = LektorCurrentKaraResponse::from_formated(response); - println!("CURRENT {:?}", current_kara); + if let Ok(LektorResponse::CurrentKara(current)) = lektor.send_query(LektorQuery::CurrentKara) { + println!("CURRENT {current:?}"); } - if let Ok(response) = lektor.send_query(LektorQuery::PlaybackStatus) { - let playback_status = LektorPlaybackStatusResponse::from_formated(response); - println!("PLAYBACK-STATUS {:?}", playback_status); + if let Ok(LektorResponse::PlaybackStatus(status)) = + lektor.send_query(LektorQuery::PlaybackStatus) + { + println!("STATUS {status:?}"); } - if let Ok(response) = lektor.send_query(LektorQuery::ListAllPlaylists) { - let plts_response = LektorPlaylistListResponse::from_formated(response); - println!("ALL-PLTS {:?}", plts_response); + if let Ok(LektorResponse::PlaylistSet(plts)) = lektor.send_query(LektorQuery::ListAllPlaylists) + { + println!("PLTS {plts:?}"); } }