Just format the code :P

This commit is contained in:
Anas Elgarhy 2023-02-12 05:35:14 +02:00
parent 355ef0db93
commit cb5c02d0b9
No known key found for this signature in database
GPG key ID: 0501802A1D496528
4 changed files with 67 additions and 42 deletions

View file

@ -1,13 +1,13 @@
mod events; mod events;
mod query;
mod player_settings; mod player_settings;
mod query;
use crate::cmus::query::CmusQueryResponse;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::Display; use std::fmt::Display;
use std::num::ParseIntError; use std::num::ParseIntError;
use std::str::FromStr; use std::str::FromStr;
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
use crate::cmus::query::CmusQueryResponse;
#[derive(Debug, PartialEq, Default)] #[derive(Debug, PartialEq, Default)]
pub struct TrackMetadata { pub struct TrackMetadata {
@ -176,7 +176,9 @@ impl Track {
/// And collect the output, and parse it into a `CmusQueryResponse`. /// And collect the output, and parse it into a `CmusQueryResponse`.
/// If the cmus is not running, or the socket is not available, this function will return an error. /// If the cmus is not running, or the socket is not available, this function will return an error.
#[inline] #[inline]
pub fn ping_cmus(query_command: &mut std::process::Command) -> Result<CmusQueryResponse, CmusError> { pub fn ping_cmus(
query_command: &mut std::process::Command,
) -> Result<CmusQueryResponse, CmusError> {
// Just run the command, and collect the output. // Just run the command, and collect the output.
let output = query_command let output = query_command
.output() .output()

View file

@ -1,6 +1,6 @@
use crate::cmus::CmusError;
use std::num::ParseIntError; use std::num::ParseIntError;
use std::str::FromStr; use std::str::FromStr;
use crate::cmus::CmusError;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct PlayerSettings { pub struct PlayerSettings {
@ -41,7 +41,7 @@ impl FromStr for AAAMode {
"all" => Ok(Self::All), "all" => Ok(Self::All),
"album" => Ok(Self::Album), "album" => Ok(Self::Album),
"artist" => Ok(Self::Artist), "artist" => Ok(Self::Artist),
_ => Err(CmusError::UnknownAAAMode(s.to_string())) _ => Err(CmusError::UnknownAAAMode(s.to_string())),
} }
} }
} }
@ -54,7 +54,7 @@ impl FromStr for Shuffle {
"off" => Ok(Self::Off), "off" => Ok(Self::Off),
"tracks" => Ok(Self::Tracks), "tracks" => Ok(Self::Tracks),
"albums" => Ok(Self::Albums), "albums" => Ok(Self::Albums),
_ => Err(CmusError::UnknownShuffleMode(s.to_string())) _ => Err(CmusError::UnknownShuffleMode(s.to_string())),
} }
} }
} }
@ -72,15 +72,25 @@ impl FromStr for PlayerSettings {
for line in s.lines() { for line in s.lines() {
if line.starts_with("set ") { if line.starts_with("set ") {
let line = &line[4..]; let line = &line[4..];
let (key, value) = line.split_once(" ").ok_or(CmusError::UnknownError("Corrupted cmus response".to_string()))?; let (key, value) = line.split_once(" ").ok_or(CmusError::UnknownError(
"Corrupted cmus response".to_string(),
))?;
match key { match key {
"repeat" => repeat = value == "true", "repeat" => repeat = value == "true",
"repeat_current" => repeat_current = value == "true", "repeat_current" => repeat_current = value == "true",
"shuffle" => shuffle = Shuffle::from_str(value)?, "shuffle" => shuffle = Shuffle::from_str(value)?,
"aaa_mode" => aaa_mode = AAAMode::from_str(value)?, "aaa_mode" => aaa_mode = AAAMode::from_str(value)?,
"vol_left" => volume.left = value.parse().map_err(|e: ParseIntError| CmusError::UnknownError(e.to_string()))?, "vol_left" => {
"vol_right" => volume.right = value.parse().map_err(|e: ParseIntError| CmusError::UnknownError(e.to_string()))?, volume.left = value
.parse()
.map_err(|e: ParseIntError| CmusError::UnknownError(e.to_string()))?
}
"vol_right" => {
volume.right = value
.parse()
.map_err(|e: ParseIntError| CmusError::UnknownError(e.to_string()))?
}
_ => {} _ => {}
} }
} }
@ -110,7 +120,10 @@ mod tests {
assert_eq!(all, Ok(AAAMode::All)); assert_eq!(all, Ok(AAAMode::All));
assert_eq!(album, Ok(AAAMode::Album)); assert_eq!(album, Ok(AAAMode::Album));
assert_eq!(artist, Ok(AAAMode::Artist)); assert_eq!(artist, Ok(AAAMode::Artist));
assert_eq!(unknown, Err(CmusError::UnknownAAAMode("unknown".to_string()))); assert_eq!(
unknown,
Err(CmusError::UnknownAAAMode("unknown".to_string()))
);
} }
#[test] #[test]
@ -123,7 +136,10 @@ mod tests {
assert_eq!(off, Ok(Shuffle::Off)); assert_eq!(off, Ok(Shuffle::Off));
assert_eq!(tracks, Ok(Shuffle::Tracks)); assert_eq!(tracks, Ok(Shuffle::Tracks));
assert_eq!(albums, Ok(Shuffle::Albums)); assert_eq!(albums, Ok(Shuffle::Albums));
assert_eq!(unknown, Err(CmusError::UnknownShuffleMode("unknown".to_string()))); assert_eq!(
unknown,
Err(CmusError::UnknownShuffleMode("unknown".to_string()))
);
} }
#[test] #[test]
@ -133,15 +149,18 @@ mod tests {
let settings = PlayerSettings::from_str(setting_sample); let settings = PlayerSettings::from_str(setting_sample);
assert_eq!(settings, Ok(PlayerSettings { assert_eq!(
repeat: false, settings,
repeat_current: false, Ok(PlayerSettings {
shuffle: Shuffle::Tracks, repeat: false,
aaa_mode: AAAMode::Artist, repeat_current: false,
volume: Volume { shuffle: Shuffle::Tracks,
left: 46, aaa_mode: AAAMode::Artist,
right: 46, volume: Volume {
} left: 46,
})); right: 46,
}
})
);
} }
} }

View file

@ -1,6 +1,6 @@
use std::str::FromStr;
use crate::cmus::{CmusError, Track};
use crate::cmus::player_settings::PlayerSettings; use crate::cmus::player_settings::PlayerSettings;
use crate::cmus::{CmusError, Track};
use std::str::FromStr;
/// This struct is used to store the row status response from cmus. /// 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. /// So we don't parse it and take the time then we don't need it.
@ -41,10 +41,10 @@ impl CmusQueryResponse {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use test_context::{test_context, TestContext}; use super::*;
use crate::cmus::player_settings::{AAAMode, Shuffle}; use crate::cmus::player_settings::{AAAMode, Shuffle};
use crate::cmus::TrackStatus; use crate::cmus::TrackStatus;
use super::*; use test_context::{test_context, TestContext};
#[test] #[test]
fn test_parse_query_from_str() { fn test_parse_query_from_str() {
@ -54,8 +54,14 @@ mod tests {
assert!(query.is_ok()); assert!(query.is_ok());
let query = query.unwrap(); let query = query.unwrap();
assert_eq!(query.track_row, include_str!("../../tests/samples/row/cmus-remote-output-track-row.txt")); assert_eq!(
assert_eq!(query.player_settings_row, include_str!("../../tests/samples/row/cmus-remote-output-player-row.txt")); query.track_row,
include_str!("../../tests/samples/row/cmus-remote-output-track-row.txt")
);
assert_eq!(
query.player_settings_row,
include_str!("../../tests/samples/row/cmus-remote-output-player-row.txt")
);
} }
struct Context { struct Context {
@ -67,9 +73,7 @@ mod tests {
let row = include_str!("../../tests/samples/row/cmus-remote-output-row.txt"); let row = include_str!("../../tests/samples/row/cmus-remote-output-row.txt");
let query = CmusQueryResponse::from_str(row).unwrap(); let query = CmusQueryResponse::from_str(row).unwrap();
Self { Self { query }
query,
}
} }
} }
@ -81,7 +85,10 @@ mod tests {
assert!(track.is_ok()); assert!(track.is_ok());
let track = track.unwrap(); let track = track.unwrap();
assert_eq!(track.path, "/mnt/Data/Music/FLAC/Taylor Swift/Taylor Swift - Speak Now/12 - Haunted.mp3"); assert_eq!(
track.path,
"/mnt/Data/Music/FLAC/Taylor Swift/Taylor Swift - Speak Now/12 - Haunted.mp3"
);
assert_eq!(track.status, TrackStatus::Playing); assert_eq!(track.status, TrackStatus::Playing);
assert_eq!(track.position, 34); assert_eq!(track.position, 34);
assert_eq!(track.duration, 242); assert_eq!(track.duration, 242);

View file

@ -19,7 +19,6 @@ fn main() {
&args.cmus_socket_password, &args.cmus_socket_password,
); );
let sleep = || { let sleep = || {
std::thread::sleep(std::time::Duration::from_millis(args.interval)); std::thread::sleep(std::time::Duration::from_millis(args.interval));
}; };
@ -43,18 +42,16 @@ fn main() {
} }
}; };
/* // Compare the track info with the previous one, and if they are the same, just sleep for a while and try again. /* // Compare the track info with the previous one, and if they are the same, just sleep for a while and try again.
if track == previous_track { if track == previous_track {
sleep(); sleep();
continue; continue;
} }
// If the track info is different from the previous one, get the changes events. // If the track info is different from the previous one, get the changes events.
let changes = track.get_changes(&previous_track); let changes = track.get_changes(&previous_track);
*/ */
sleep(); sleep();
} }
} }