mpdclient 0.2.0

Rust interface to MPD using libmpdclient
Documentation
use mpdclient_sys::{
    mpd_recv_stats, mpd_run_stats, mpd_stats, mpd_stats_get_db_play_time,
    mpd_stats_get_db_update_time, mpd_stats_get_number_of_albums, mpd_stats_get_number_of_artists,
    mpd_stats_get_number_of_songs, mpd_stats_get_play_time, mpd_stats_get_uptime,
};

use crate::error::Result;

use super::Connection;

/// Intermediate to bundle MPD statistic functions.
pub struct Stats {
    inner: *const mpd_stats,
}

impl Stats {
    pub(super) fn new(connection: &Connection) -> Result<Self> {
        let inner = connection.get_error(|| unsafe { mpd_run_stats(connection.connection()) })?;
        Ok(Self { inner })
    }

    /// ONLY CALL IF STATS ARE PREVIOUSLY REQUESTED
    pub(super) fn receive(connection: &Connection) -> Result<Self> {
        let inner = connection.get_error(|| unsafe { mpd_recv_stats(connection.connection()) })?;
        Ok(Self { inner })
    }

    /// Returns the amount of artists in the database.
    ///
    /// `None` can either mean there are none or the number unknown.
    #[must_use]
    pub fn artists(&self) -> Option<u32> {
        let artists = unsafe { mpd_stats_get_number_of_artists(self.inner) };
        if artists != 0 { Some(artists) } else { None }
    }

    /// Returns the amount of albums in the database.
    ///
    /// `None` can either mean there are none or the number unknown.
    #[must_use]
    pub fn albums(&self) -> Option<u32> {
        let albums = unsafe { mpd_stats_get_number_of_albums(self.inner) };
        if albums != 0 { Some(albums) } else { None }
    }

    /// Returns the amount of songs in the database.
    ///
    /// `None` can either mean there are none or the number unknown.
    #[must_use]
    pub fn songs(&self) -> Option<u32> {
        let songs = unsafe { mpd_stats_get_number_of_songs(self.inner) };
        if songs != 0 { Some(songs) } else { None }
    }

    /// Returns the uptime of the MPD.
    ///
    /// `None` can either mean there are none or the number unknown.
    #[must_use]
    pub fn uptime(&self) -> Option<u64> {
        let uptime = unsafe { mpd_stats_get_uptime(self.inner) };
        if uptime != 0 { Some(uptime) } else { None }
    }

    /// Returns the `UNIX_TIMESTAMP` of the last database update.
    ///
    /// `None` can either mean there are none or the number unknown.
    #[must_use]
    pub fn db_update_time(&self) -> Option<u64> {
        let time = unsafe { mpd_stats_get_db_update_time(self.inner) };
        if time != 0 { Some(time) } else { None }
    }

    /// Returns the total playtime since the MPD was started.
    ///
    /// `None` can either mean there are none or the number unknown.
    #[must_use]
    pub fn play_time(&self) -> Option<u64> {
        let time = unsafe { mpd_stats_get_play_time(self.inner) };
        if time != 0 { Some(time) } else { None }
    }

    /// Returns the total duration of all songs in the database.
    ///
    /// `None` can either mean there are none or the number unknown.
    #[must_use]
    pub fn db_play_time(&self) -> Option<u64> {
        let time = unsafe { mpd_stats_get_db_play_time(self.inner) };
        if time != 0 { Some(time) } else { None }
    }
}