From 3290c5673836c673b1b6a96e2ff81523ff6f0899 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Fri, 9 Sep 2022 15:17:19 +0200 Subject: [PATCH] LKT-LIB-RS: Add comments about modules + small refacto of deamon code Signed-off-by: Kubat <mael.martin31@gmail.com> --- .../amadeus-rs/amadeus/src/utils/deamon.rs | 70 +++++++++++-------- src/rust/amadeus-rs/lkt-lib/src/connexion.rs | 3 + src/rust/amadeus-rs/lkt-lib/src/constants.rs | 3 + src/rust/amadeus-rs/lkt-lib/src/lib.rs | 3 + src/rust/amadeus-rs/lkt-lib/src/query.rs | 33 +++++---- src/rust/amadeus-rs/lkt-lib/src/response.rs | 30 ++++---- src/rust/amadeus-rs/lkt-lib/src/types.rs | 2 + src/rust/amadeus-rs/lkt-lib/src/uri.rs | 2 + src/rust/amadeus-rs/lkt-rs/src/main.rs | 4 +- 9 files changed, 89 insertions(+), 61 deletions(-) diff --git a/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs b/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs index 1cf061e2..fa774c68 100644 --- a/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs +++ b/src/rust/amadeus-rs/amadeus/src/utils/deamon.rs @@ -107,18 +107,23 @@ impl Deamon for CommandDeamon { return_when_flagged!(quit_deamon, joined_deamon); match commands_recv.recv() { Ok(command) => { - let maybe_res = connexion.send_query(command); - if maybe_res.is_err() { - error!("Failed to send query to lektor: {}", maybe_res.unwrap_err()); - continue; - } - let res = maybe_res.unwrap(); - let response = lkt_lib::response::LektorFormatedResponse::from(res); - if let Err(e) = responses_send.send(response) { - error!("Failed to send response to amadeus: {e}") + let res = match connexion.send_query(command) { + Ok(ok) => ok, + Err(e) => { + error!("failed to send query to lektor: {e}"); + continue; + } + }; + match lkt_lib::response::LektorFormatedResponse::try_from(res) { + Ok(response) => { + if let Err(e) = responses_send.send(response) { + error!("failed to send response to amadeus: {e}") + } + } + Err(e) => error!("failed to format the response: {e}"), } } - Err(e) => error!("Failed to get command from amadeus: {e}"), + Err(e) => error!("failed to get command from amadeus: {e}"), }; }); @@ -153,31 +158,36 @@ impl Deamon for StatusDeamon { return_when_flagged!(quit_deamon, joined_deamon); let status = { - let maybe_res = connexion.send_query(lkt_lib::query::LektorQuery::PlaybackStatus); - if maybe_res.is_err() { - error!( - "Failed to send the playback status command to lektor: {}", - maybe_res.unwrap_err() - ); - continue; + let res = match connexion.send_query(lkt_lib::query::LektorQuery::PlaybackStatus) { + Ok(res) => res, + Err(e) => { + error!("failed to send the playback status command to lektor: {e}"); + continue; + } + }; + match lkt_lib::response::LektorFormatedResponse::try_from(res) { + Err(e) => panic!("{e}"), + Ok(mut response) => lkt_lib::response::PlaybackStatus::consume(&mut response), } - let res = maybe_res.unwrap(); - let mut response = lkt_lib::response::LektorFormatedResponse::from(res); - lkt_lib::response::PlaybackStatus::consume(&mut response) }; let current = if status.state != lkt_lib::types::LektorState::Stopped { - let maybe_res = connexion.send_query(lkt_lib::query::LektorQuery::CurrentKara); - if maybe_res.is_err() { - error!( - "Failed to send the current kara command to lektor: {}", - maybe_res.unwrap_err() - ); - continue; + let res = match connexion.send_query(lkt_lib::query::LektorQuery::CurrentKara) { + Ok(res) => res, + Err(e) => { + error!("failed to send the current kara command to lektor: {e}",); + continue; + } + }; + match lkt_lib::response::LektorFormatedResponse::try_from(res) { + Ok(mut response) => { + Some(lkt_lib::response::CurrentKara::consume(&mut response)) + } + Err(e) => { + error!("failed to format response from lektor: {e}"); + None + } } - let res = maybe_res.unwrap(); - let mut response = lkt_lib::response::LektorFormatedResponse::from(res); - Some(lkt_lib::response::CurrentKara::consume(&mut response)) } else { None }; diff --git a/src/rust/amadeus-rs/lkt-lib/src/connexion.rs b/src/rust/amadeus-rs/lkt-lib/src/connexion.rs index 70a3bcf0..552a992a 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/connexion.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/connexion.rs @@ -1,3 +1,6 @@ +//! Create a connexion to a lektord instande then send commands and recieve data +//! from the server. + use crate::{constants, query::LektorQuery, query::LektorQueryLineType}; use log::error; use std::{ diff --git a/src/rust/amadeus-rs/lkt-lib/src/constants.rs b/src/rust/amadeus-rs/lkt-lib/src/constants.rs index 960c2792..505f0af8 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/constants.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/constants.rs @@ -1,3 +1,6 @@ +//! Contains standard constants from lektord, must be updated manually if they +//! change in lektord... + #![allow(dead_code)] /// MPD commands are at most 32 character long. diff --git a/src/rust/amadeus-rs/lkt-lib/src/lib.rs b/src/rust/amadeus-rs/lkt-lib/src/lib.rs index 3975593c..100741fd 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/lib.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/lib.rs @@ -1,3 +1,6 @@ +//! Contains structurs and mechanisms to build, send and recieve commands +//! to/from lektord. + pub mod connexion; mod constants; pub mod query; diff --git a/src/rust/amadeus-rs/lkt-lib/src/query.rs b/src/rust/amadeus-rs/lkt-lib/src/query.rs index aba24167..886f19df 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/query.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/query.rs @@ -1,3 +1,5 @@ +//! Contains files to create and build queries to send latter to lektord. + use crate::uri::LektorUri; use amadeus_macro::lkt_command_from_str; use std::string::ToString; @@ -78,27 +80,28 @@ impl LektorQuery { impl ToString for LektorQuery { fn to_string(&self) -> String { + use LektorQuery::*; match self { - Self::Ping => lkt_command_from_str!("ping"), - Self::Close => lkt_command_from_str!("close"), - Self::KillServer => lkt_command_from_str!("kill"), - Self::ConnectAsUser(password) => format!("password {}\n", password), + Ping => lkt_command_from_str!("ping"), + Close => lkt_command_from_str!("close"), + KillServer => lkt_command_from_str!("kill"), + ConnectAsUser(password) => format!("password {}\n", password), - Self::CurrentKara => lkt_command_from_str!("currentsong"), - Self::PlaybackStatus => lkt_command_from_str!("status"), + CurrentKara => lkt_command_from_str!("currentsong"), + PlaybackStatus => lkt_command_from_str!("status"), - Self::PlayNext => lkt_command_from_str!("next"), - Self::PlayPrevious => lkt_command_from_str!("previous"), - Self::ShuffleQueue => lkt_command_from_str!("shuffle"), + PlayNext => lkt_command_from_str!("next"), + PlayPrevious => lkt_command_from_str!("previous"), + ShuffleQueue => lkt_command_from_str!("shuffle"), - Self::ListAllPlaylists => lkt_command_from_str!("listplaylists"), - Self::ListPlaylist(plt_name) => format!("listplaylist {}\n", plt_name), + ListAllPlaylists => lkt_command_from_str!("listplaylists"), + ListPlaylist(plt_name) => format!("listplaylist {}\n", plt_name), - Self::FindAddKara(uri) => format!("findadd {}\n", uri.to_string()), - Self::InsertKara(uri) => format!("__insert {}\n", uri.to_string()), - Self::AddKara(uri) => format!("add {}\n", uri.to_string()), + FindAddKara(uri) => format!("findadd {}\n", uri.to_string()), + InsertKara(uri) => format!("__insert {}\n", uri.to_string()), + AddKara(uri) => format!("add {}\n", uri.to_string()), - Self::Continuation(cont, query) => format!("{} {}", cont, query.to_owned().to_string()), + Continuation(cont, query) => format!("{} {}", cont, query.to_owned().to_string()), } } } diff --git a/src/rust/amadeus-rs/lkt-lib/src/response.rs b/src/rust/amadeus-rs/lkt-lib/src/response.rs index 24333136..bbedf6d4 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/response.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/response.rs @@ -1,3 +1,5 @@ +//! Contains types for typed response. + use crate::types::LektorState; use std::collections::HashMap; @@ -15,21 +17,21 @@ impl LektorFormatedResponse { } } -impl From<Vec<String>> for LektorFormatedResponse { - fn from(vec: Vec<String>) -> Self { - Self { - content: vec - .into_iter() - .map(|line| { - let key_pair: Vec<&str> = line.splitn(2, ':').collect(); - if let [key, value] = key_pair[..] { - (key.trim().to_lowercase(), value.trim().to_owned()) - } else { - panic!("Invalid line for formated response: {}", line); - } - }) - .collect(), +impl TryFrom<Vec<String>> for LektorFormatedResponse { + type Error = String; + + fn try_from(vec: Vec<String>) -> Result<Self, Self::Error> { + let mut content = HashMap::with_capacity(vec.len()); + for line in vec { + let key_pair: Vec<&str> = line.splitn(2, ':').collect(); + match key_pair[..] { + [key, value] => { + content.insert(key.trim().to_lowercase(), value.trim().to_string()); + } + _ => return Err(format!("Invalid line for formated response: {}", line)), + } } + Ok(Self { content }) } } diff --git a/src/rust/amadeus-rs/lkt-lib/src/types.rs b/src/rust/amadeus-rs/lkt-lib/src/types.rs index fa3faaff..421fbd7d 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/types.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/types.rs @@ -1,3 +1,5 @@ +//! Common types for lektord. + use std::hash::{Hash, Hasher}; /// Lektor Types are the Kara and the Playlist types. You should not implement diff --git a/src/rust/amadeus-rs/lkt-lib/src/uri.rs b/src/rust/amadeus-rs/lkt-lib/src/uri.rs index 95892a1c..7c1e8308 100644 --- a/src/rust/amadeus-rs/lkt-lib/src/uri.rs +++ b/src/rust/amadeus-rs/lkt-lib/src/uri.rs @@ -1,3 +1,5 @@ +//! Build lektord queries. + #[derive(Debug, Clone)] pub enum LektorUri { Id(i32), diff --git a/src/rust/amadeus-rs/lkt-rs/src/main.rs b/src/rust/amadeus-rs/lkt-rs/src/main.rs index fc6376ca..3e939515 100644 --- a/src/rust/amadeus-rs/lkt-rs/src/main.rs +++ b/src/rust/amadeus-rs/lkt-rs/src/main.rs @@ -5,13 +5,13 @@ fn main() { if lektor.send_query(LektorQuery::Ping).is_ok() {} if let Ok(res) = lektor.send_query(LektorQuery::CurrentKara) { - let mut response = response::LektorFormatedResponse::from(res); + let mut response = response::LektorFormatedResponse::try_from(res).unwrap(); let current_kara = response::CurrentKara::consume(&mut response); println!("CURRENT {:?}", current_kara); } if let Ok(res) = lektor.send_query(LektorQuery::PlaybackStatus) { - let mut response = response::LektorFormatedResponse::from(res); + let mut response = response::LektorFormatedResponse::try_from(res).unwrap(); let playback_status = response::PlaybackStatus::consume(&mut response); println!("PLAYBACK-STATUS {:?}", playback_status); } -- GitLab