From b415ebb5094c21c7aba09c86a6e70c6626bd1d1b Mon Sep 17 00:00:00 2001 From: kageru Date: Sun, 21 Jun 2020 19:12:36 +0200 Subject: [PATCH] Add database stats --- src/lib.rs | 26 +++++++++++++++++++++++++- src/structs.rs | 17 +++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 4867ad6..462fc1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ mod error; use error::{Error, MpdResult}; use itertools::Itertools; mod structs; -pub use structs::{Position, Status, Track}; +pub use structs::{Position, Stats, Status, Track}; /// some unprintable character to separate repeated keys const SEPARATOR: char = '\x02'; @@ -280,4 +280,28 @@ OK"; let queue = read_playlist_info("OK".lines()); assert_eq!(queue, Ok(vec![])); } + + #[test] + fn de_stats_test() { + let input_str = "uptime: 23691 +playtime: 11288 +artists: 2841 +albums: 2455 +songs: 40322 +db_playtime: 11620284 +db_update: 1588433046"; + let s: Stats = deserialize_response(input_str.lines()).unwrap(); + assert_eq!( + s, + Stats { + uptime: Duration::from_secs(23691), + playtime: Duration::from_secs(11288), + artists: 2841, + albums: 2455, + songs: 40322, + db_playtime: Duration::from_secs(11620284), + db_update: 1588433046, + } + ); + } } diff --git a/src/structs.rs b/src/structs.rs index 1eb5306..a5ead09 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -103,6 +103,23 @@ pub struct Status { pub error: Option, } +/// Database statistics as returned by the `stats` command. +#[derive(Deserialize, Clone, Debug, Default, PartialEq)] +#[serde(default)] +pub struct Stats { + pub artists: u32, + pub albums: u32, + pub songs: u32, + #[serde(deserialize_with = "de_time_int")] + pub uptime: Duration, + #[serde(deserialize_with = "de_time_int")] + pub db_playtime: Duration, + // TODO: this is a unix era. use some datetime for it + pub db_update: u32, + #[serde(deserialize_with = "de_time_int")] + pub playtime: Duration, +} + /// Deserialization helpers to handle the quirks of mpd’s output. mod helpers { use super::*;