mpdclient 0.2.0

Rust interface to MPD using libmpdclient
Documentation
use mpdclient_sys::{mpd_run_change_volume, mpd_run_get_volume, mpd_run_set_volume};

use crate::{Connection, error::Result};

/// Intermediate to bundle mixer functions for MPD.
pub struct Mixer<'a> {
    connection: &'a Connection,
}

impl<'a> Mixer<'a> {
    pub(crate) fn new(connection: &'a Connection) -> Self {
        Self { connection }
    }

    /// Get the current volume
    ///
    /// # Errors
    ///
    /// Returns [`Error::Mpd`](crate::Error::Mpd) if MPD returns an error.
    pub fn get_volume(&self) -> Result<i32> {
        self.connection
            .get_error(|| unsafe { mpd_run_get_volume(self.connection.connection()) })
    }

    /// Set a new volume
    ///
    /// # Errors
    ///
    /// Returns [`Error::Mpd`](crate::Error::Mpd) if MPD returns an error.
    pub fn set_volume(&self, volume: u32) -> Result<()> {
        self.connection
            .get_bool_error(|| unsafe { mpd_run_set_volume(self.connection.connection(), volume) })
    }

    /// Change the volume relative to the current volume
    ///
    /// # Errors
    ///
    /// Returns [`Error::Mpd`](crate::Error::Mpd) if MPD returns an error.
    pub fn change_volume(&self, relative_volume: i32) -> Result<()> {
        self.connection.get_bool_error(|| unsafe {
            mpd_run_change_volume(self.connection.connection(), relative_volume)
        })
    }
}