diff --git a/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs b/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs index 21317f21c7ef8abb5763d81ed0325b85eeacca83..252aaf3b227a6009792605a6b9ac1da40d229a58 100644 --- a/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs +++ b/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs @@ -160,7 +160,7 @@ impl Deamon for StatusDeamon { continue; } }; - match LektorPlaybackStatusResponse::consume(res) { + match LektorPlaybackStatusResponse::from_formated(res) { Err(e) => { error!("failed to build response from formated response: {e}"); continue; @@ -169,7 +169,7 @@ impl Deamon for StatusDeamon { } }; - let current = if status.state != LektorState::Stopped { + let current = if status.state() != LektorState::Stopped { let res = match connexion.send_query(LektorQuery::CurrentKara) { Ok(res) => res, Err(e) => { @@ -177,7 +177,7 @@ impl Deamon for StatusDeamon { continue; } }; - match LektorCurrentKaraResponse::consume(res) { + match LektorCurrentKaraResponse::from_formated(res) { Ok(res) => Some(res), Err(err) => { error!("failed to build response from formated response: {err}"); diff --git a/src/rust/amadeus-rs/lkt-lib/src/response.rs b/src/rust/amadeus-rs/lkt-lib/src/response.rs index d80e662f86cc8e10743db422235e5c08a08f19cc..1765bc64a4604eca29f5f809753069edc42c28f0 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/response.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/response.rs @@ -85,7 +85,7 @@ pub enum LektordResponse { pub trait FromLektorResponse: Sized + std::fmt::Debug + private::Sealed { /// Consume a formated response to produce the correctly typed response. May /// got an error as a string that describes the problem. - fn consume(response: LektorFormatedResponse) -> Result<Self, String>; + fn from_formated(response: LektorFormatedResponse) -> Result<Self, String>; } mod private { @@ -97,46 +97,104 @@ mod private { impl Sealed for LektorEmptyResponse {} } +macro_rules! getter { + ($name: ident: ref $type: ty) => { + pub fn $name(&self) -> &$type { + &self.$name + } + }; + + ($name: ident: $type: ty) => { + pub fn $name(&self) -> $type { + self.$name + } + }; +} + #[derive(Debug)] pub struct LektorPlaybackStatusResponse { - pub elapsed: usize, - pub songid: Option<usize>, - pub song: Option<usize>, - pub volume: usize, - pub state: LektorState, - pub duration: usize, - pub updating_db: usize, - pub playlistlength: usize, - pub random: bool, - pub consume: bool, - pub single: bool, - pub repeat: bool, + elapsed: usize, + songid: Option<usize>, + song: Option<usize>, + volume: usize, + state: LektorState, + duration: usize, + updating_db: usize, + playlistlength: usize, + random: bool, + consume: bool, + single: bool, + repeat: bool, } #[derive(Debug)] pub struct LektorPlaylistListResponse { - pub playlists: Vec<String>, + playlists: Vec<String>, } #[derive(Debug)] pub struct LektorCurrentKaraInnerResponse { - pub title: String, - pub author: String, - pub source: String, - pub song_type: String, - pub song_number: Option<usize>, - pub category: String, - pub language: String, + title: String, + author: String, + source: String, + song_type: String, + song_number: Option<usize>, + category: String, + language: String, } #[derive(Debug)] pub struct LektorCurrentKaraResponse { - pub content: Option<LektorCurrentKaraInnerResponse>, + content: Option<LektorCurrentKaraInnerResponse>, } #[derive(Debug)] pub struct LektorEmptyResponse; +impl LektorCurrentKaraResponse { + getter!(content: ref Option<LektorCurrentKaraInnerResponse>); +} + +impl LektorCurrentKaraInnerResponse { + getter!(title: ref String); + getter!(author: ref String); + getter!(source: ref String); + getter!(song_type: ref String); + getter!(song_number: Option<usize>); + getter!(category: ref String); + getter!(language: ref String); +} + +impl LektorPlaybackStatusResponse { + getter!(elapsed: usize); + getter!(songid: Option<usize>); + getter!(song: Option<usize>); + getter!(volume: usize); + getter!(state: LektorState); + getter!(duration: usize); + getter!(updating_db: usize); + getter!(playlistlength: usize); + getter!(random: bool); + getter!(consume: bool); + getter!(single: bool); + getter!(repeat: bool); +} + +impl LektorPlaylistListResponse { + pub fn iter(&self) -> &[String] { + &self.playlists[..] + } +} + +impl IntoIterator for LektorPlaylistListResponse { + type Item = String; + type IntoIter = <std::vec::Vec<std::string::String> as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.playlists.into_iter() + } +} + impl LektorCurrentKaraInnerResponse { /// If the response is partial we might want to return none (if we are not /// playing anything for example...) @@ -151,13 +209,13 @@ impl LektorCurrentKaraInnerResponse { } impl FromLektorResponse for LektorEmptyResponse { - fn consume(_: LektorFormatedResponse) -> Result<Self, String> { + fn from_formated(_: LektorFormatedResponse) -> Result<Self, String> { Ok(Self {}) } } impl FromLektorResponse for LektorPlaylistListResponse { - fn consume(mut response: LektorFormatedResponse) -> Result<Self, String> { + fn from_formated(mut response: LektorFormatedResponse) -> Result<Self, String> { Ok(Self { playlists: response.pop_all("name"), }) @@ -165,7 +223,7 @@ impl FromLektorResponse for LektorPlaylistListResponse { } impl FromLektorResponse for LektorPlaybackStatusResponse { - fn consume(mut response: LektorFormatedResponse) -> Result<Self, String> { + fn from_formated(mut response: LektorFormatedResponse) -> Result<Self, String> { let mut ret = Self { elapsed: response.pop("elapsed")?.parse::<usize>().unwrap_or(0), songid: match response.pop("songid")?.parse::<isize>() { @@ -201,7 +259,7 @@ impl FromLektorResponse for LektorPlaybackStatusResponse { } impl FromLektorResponse for LektorCurrentKaraResponse { - fn consume(mut response: LektorFormatedResponse) -> Result<Self, String> { + fn from_formated(mut response: LektorFormatedResponse) -> Result<Self, String> { let song_type_number = response.pop("type")?; let (song_type, song_number) = match song_type_number.find(char::is_numeric) { Some(index) => ( diff --git a/src/rust/amadeus-rs/lkt-lib/src/types.rs b/src/rust/amadeus-rs/lkt-lib/src/types.rs index 421fbd7d01a0cd134dce21b598b5a8816dabbb91..1c3b7223ad2fbda3850662b383ee94a91374fbf4 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/types.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/types.rs @@ -56,7 +56,7 @@ impl LektorType<'_> for Playlist { } } -#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)] +#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq, Copy)] pub enum LektorState { Stopped, Play(usize), diff --git a/src/rust/amadeus-rs/lkt-rs/src/main.rs b/src/rust/amadeus-rs/lkt-rs/src/main.rs index f7d6f81f7fd9f086c930e47ba414f1351b9e83cd..56dd5bfbd049dec74d8f0cff50c1dfb0c33d4e6b 100644 --- a/src/rust/amadeus-rs/lkt-rs/src/main.rs +++ b/src/rust/amadeus-rs/lkt-rs/src/main.rs @@ -5,17 +5,17 @@ fn main() { if lektor.send_query(LektorQuery::Ping).is_ok() {} if let Ok(response) = lektor.send_query(LektorQuery::CurrentKara) { - let current_kara = LektorCurrentKaraResponse::consume(response); + let current_kara = LektorCurrentKaraResponse::from_formated(response); println!("CURRENT {:?}", current_kara); } if let Ok(response) = lektor.send_query(LektorQuery::PlaybackStatus) { - let playback_status = LektorPlaybackStatusResponse::consume(response); + let playback_status = LektorPlaybackStatusResponse::from_formated(response); println!("PLAYBACK-STATUS {:?}", playback_status); } if let Ok(response) = lektor.send_query(LektorQuery::ListAllPlaylists) { - let plts_response = LektorPlaylistListResponse::consume(response); + let plts_response = LektorPlaylistListResponse::from_formated(response); println!("ALL-PLTS {:?}", plts_response); } }