Create query module

This commit is contained in:
Anas Elgarhy 2023-02-11 02:38:56 +02:00
parent 2758461de2
commit 94ea4eefae
No known key found for this signature in database
GPG Key ID: 0501802A1D496528
1 changed files with 40 additions and 0 deletions

40
src/cmus/query.rs Normal file
View File

@ -0,0 +1,40 @@
use std::str::FromStr;
use crate::cmus::{CmusError, Track};
use crate::cmus::player_settings::PlayerSettings;
/// This struct is used to store the row status response from cmus.
/// So we don't parse it and take the time then we don't need it.
/// We only parse it when we need it.
#[derive(Debug, PartialEq, Default)]
pub struct CmusQueryResponse {
track_row: String,
player_settings_row: String,
}
impl FromStr for CmusQueryResponse {
type Err = String;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
let sep_index = s.find("set ").ok_or("Corrupted cmus response")?;
Ok(Self {
track_row: s[..sep_index].to_string(),
player_settings_row: s[sep_index..].to_string(),
})
}
}
impl CmusQueryResponse {
/// Actually process and parse the track info, from the cmus response.
#[inline(always)]
pub fn track(&self) -> Result<Track, CmusError> {
Track::from_str(&self.track_row)
}
/// Actually process and parse the player settings, from the cmus response.
#[inline(always)]
pub fn player_settings(&self) -> Result<PlayerSettings, CmusError> {
PlayerSettings::from_str(&self.player_settings_row)
}
}