Added list of supported commands as Enum. Cleaned up comments. Pushed version to 1.1.8

This commit is contained in:
Jonas Frei 2019-06-24 20:11:58 +02:00
parent 456fa2ea4d
commit cb5ad88544
4 changed files with 153 additions and 116 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "mpvipc" name = "mpvipc"
version = "1.1.7" version = "1.1.8"
authors = ["Jonas Frei <freijon@pm.me>"] authors = ["Jonas Frei <freijon@pm.me>"]
description = "A small library which provides bindings to control existing mpv instances through sockets." description = "A small library which provides bindings to control existing mpv instances through sockets."
license = "GPL-3.0" license = "GPL-3.0"

View File

@ -26,7 +26,7 @@ fn main() -> Result<(), Error> {
loop { loop {
let event = mpv.event_listen()?; let event = mpv.event_listen()?;
match event { match event {
Event::PropertyChange(_, property) => match property { Event::PropertyChange { id: _, property } => match property {
Property::Path(Some(value)) => println!("\nPlaying: {}", value), Property::Path(Some(value)) => println!("\nPlaying: {}", value),
Property::Path(None) => (), Property::Path(None) => (),
Property::Pause(value) => pause = value, Property::Pause(value) => pause = value,
@ -50,10 +50,7 @@ fn main() -> Result<(), Error> {
} }
} }
Property::Metadata(None) => (), Property::Metadata(None) => (),
Property::Unknown { Property::Unknown { name: _, data: _ } => (),
name: _,
data: _,
} => (),
}, },
Event::Shutdown => return Ok(()), Event::Shutdown => return Ok(()),
Event::Unimplemented => panic!("Unimplemented event"), Event::Unimplemented => panic!("Unimplemented event"),

View File

@ -312,7 +312,7 @@ fn try_convert_property(name: &str, id: isize, data: MpvDataType) -> Event {
} }
} }
}; };
Event::PropertyChange(id, property) Event::PropertyChange { id, property }
} }
pub fn listen(instance: &mut Mpv) -> Result<Event, Error> { pub fn listen(instance: &mut Mpv) -> Result<Event, Error> {

View File

@ -9,19 +9,6 @@ use std::os::unix::net::UnixStream;
#[derive(Debug)] #[derive(Debug)]
pub enum Event { pub enum Event {
Shutdown, Shutdown,
// LogMessage {
// prefix: &'static str,
// level: &'static str,
// text: &'static str,
// log_level: LogLevel,
// },
// GetPropertyReply {
// name: &'static str,
// result: Result<Format<'a>>,
// reply_userdata: u32,
// },
// SetPropertyReply(Result<()>, u32),
// CommandReply(Result<()>, u32),
StartFile, StartFile,
EndFile, EndFile,
FileLoaded, FileLoaded,
@ -36,7 +23,7 @@ pub enum Event {
MetadataUpdate, MetadataUpdate,
Seek, Seek,
PlaybackRestart, PlaybackRestart,
PropertyChange(isize, Property), PropertyChange { id: isize, property: Property },
ChapterChange, ChapterChange,
Unimplemented, Unimplemented,
} }
@ -48,10 +35,33 @@ pub enum Property {
PlaybackTime(Option<f64>), PlaybackTime(Option<f64>),
Duration(Option<f64>), Duration(Option<f64>),
Metadata(Option<HashMap<String, MpvDataType>>), Metadata(Option<HashMap<String, MpvDataType>>),
Unknown { Unknown { name: String, data: MpvDataType },
name: String, }
data: MpvDataType,
pub enum MpvCommand {
LoadFile {
file: String,
option: PlaylistAddOptions,
}, },
LoadList {
file: String,
option: PlaylistAddOptions,
},
PlaylistClear,
PlaylistMove {
from: usize,
to: usize,
},
PlaylistNext,
PlaylistPrev,
PlaylistRemove(usize),
PlaylistShuffle,
Quit,
Seek {
seconds: f64,
option: SeekOptions,
},
Stop,
} }
#[derive(Debug)] #[derive(Debug)]
@ -75,7 +85,6 @@ pub enum NumberChangeOptions {
pub enum PlaylistAddOptions { pub enum PlaylistAddOptions {
Replace, Replace,
Append, Append,
AppendPlay,
} }
pub enum PlaylistAddTypeOptions { pub enum PlaylistAddTypeOptions {
@ -312,11 +321,11 @@ impl Mpv {
} }
} }
/// #Description /// # Description
/// ///
/// Retrieves the property value from mpv. /// Retrieves the property value from mpv.
/// ///
/// ##Supported types /// ## Supported types
/// - String /// - String
/// - bool /// - bool
/// - HashMap<String, String> (e.g. for the 'metadata' property) /// - HashMap<String, String> (e.g. for the 'metadata' property)
@ -324,56 +333,56 @@ impl Mpv {
/// - usize /// - usize
/// - f64 /// - f64
/// ///
/// ##Input arguments /// ## Input arguments
/// ///
/// - **property** defines the mpv property that should be retrieved /// - **property** defines the mpv property that should be retrieved
/// ///
/// #Example /// # Example
/// ``` /// ```
/// # use mpvipc::{Mpv, Error}; /// use mpvipc::{Mpv, Error};
/// # fn main() -> Result<(), Error> { /// fn main() -> Result<(), Error> {
/// let mpv = Mpv::connect("/tmp/mpvsocket")?; /// let mpv = Mpv::connect("/tmp/mpvsocket")?;
/// let paused: bool = mpv.get_property("pause")?; /// let paused: bool = mpv.get_property("pause")?;
/// let title: String = mpv.get_property("media-title")?; /// let title: String = mpv.get_property("media-title")?;
/// # Ok(()) /// Ok(())
/// # } /// }
/// ``` /// ```
pub fn get_property<T: GetPropertyTypeHandler>(&self, property: &str) -> Result<T, Error> { pub fn get_property<T: GetPropertyTypeHandler>(&self, property: &str) -> Result<T, Error> {
T::get_property_generic(self, property) T::get_property_generic(self, property)
} }
/// #Description /// # Description
/// ///
/// Retrieves the property value from mpv. /// Retrieves the property value from mpv.
/// The result is always of type String, regardless of the type of the value of the mpv property /// The result is always of type String, regardless of the type of the value of the mpv property
/// ///
/// ##Input arguments /// ## Input arguments
/// ///
/// - **property** defines the mpv property that should be retrieved /// - **property** defines the mpv property that should be retrieved
/// ///
/// #Example /// # Example
/// ///
/// ``` /// ```
/// # use mpvipc::{Mpv, Error}; /// use mpvipc::{Mpv, Error};
/// # fn main() -> Result<(), Error> { /// fn main() -> Result<(), Error> {
/// let mpv = Mpv::connect("/tmp/mpvsocket")?; /// let mpv = Mpv::connect("/tmp/mpvsocket")?;
/// let title = mpv.get_property_string("media-title")?; /// let title = mpv.get_property_string("media-title")?;
/// # Ok(()) /// Ok(())
/// # } /// }
/// ``` /// ```
pub fn get_property_string(&self, property: &str) -> Result<String, Error> { pub fn get_property_string(&self, property: &str) -> Result<String, Error> {
get_mpv_property_string(self, property) get_mpv_property_string(self, property)
} }
pub fn kill(&self) -> Result<(), Error> { pub fn kill(&self) -> Result<(), Error> {
run_mpv_command(self, "quit", &[]) self.run_command(MpvCommand::Quit)
} }
/// #Description /// # Description
/// ///
/// Waits until an mpv event occurs and returns the Event. /// Waits until an mpv event occurs and returns the Event.
/// ///
/// #Example /// # Example
/// ///
/// ```ignore /// ```ignore
/// let mut mpv = Mpv::connect("/tmp/mpvsocket")?; /// let mut mpv = Mpv::connect("/tmp/mpvsocket")?;
@ -391,7 +400,7 @@ impl Mpv {
} }
pub fn next(&self) -> Result<(), Error> { pub fn next(&self) -> Result<(), Error> {
run_mpv_command(self, "playlist-next", &[]) self.run_command(MpvCommand::PlaylistNext)
} }
pub fn observe_property(&self, id: &isize, property: &str) -> Result<(), Error> { pub fn observe_property(&self, id: &isize, property: &str) -> Result<(), Error> {
@ -403,38 +412,92 @@ impl Mpv {
} }
pub fn prev(&self) -> Result<(), Error> { pub fn prev(&self) -> Result<(), Error> {
run_mpv_command(self, "playlist-prev", &[]) self.run_command(MpvCommand::PlaylistPrev)
} }
pub fn restart(&self) -> Result<(), Error> { pub fn restart(&self) -> Result<(), Error> {
run_mpv_command(self, "seek", &["0", "absolute"]) self.run_command(MpvCommand::Seek {
seconds: 0f64,
option: SeekOptions::Absolute,
})
} }
/// #Description /// # Description
/// ///
/// Runs mpv commands. The arguments are passed as a String-Vector reference: /// Runs mpv commands. The arguments are passed as a String-Vector reference:
/// ///
/// ##Input arguments /// ## Input arguments
/// ///
/// - **command** defines the mpv command that should be executed /// - **command** defines the mpv command that should be executed
/// - **args** a slice of &str's which define the arguments /// - **args** a slice of &str's which define the arguments
/// ///
/// #Example /// # Example
/// ``` /// ```
/// # use mpvipc::{Mpv, Error}; /// use mpvipc::{Mpv, Error};
/// # fn main() -> Result<(), Error> { /// fn main() -> Result<(), Error> {
/// let mpv = Mpv::connect("/tmp/mpvsocket")?; /// let mpv = Mpv::connect("/tmp/mpvsocket")?;
/// ///
/// //Run command 'playlist-shuffle' which takes no arguments /// //Run command 'playlist-shuffle' which takes no arguments
/// mpv.run_command("playlist-shuffle", &[])?; /// mpv.run_command(MpvCommand::PlaylistShuffle)?;
/// ///
/// //Run command 'seek' which in this case takes two arguments /// //Run command 'seek' which in this case takes two arguments
/// mpv.run_command("seek", &["0", "absolute"])?; /// mpv.run_command(MpvCommand::Seek {
/// # Ok(()) /// seconds: 0f64,
/// # } /// option: SeekOptions::Absolute,
/// })?;
/// Ok(())
/// }
/// ``` /// ```
pub fn run_command(&self, command: &str, args: &[&str]) -> Result<(), Error> { pub fn run_command(&self, command: MpvCommand) -> Result<(), Error> {
run_mpv_command(self, command, args) match command {
MpvCommand::LoadFile { file, option } => run_mpv_command(
self,
"loadfile",
&[
file.as_ref(),
match option {
PlaylistAddOptions::Append => "append",
PlaylistAddOptions::Replace => "replace",
},
],
),
MpvCommand::LoadList { file, option } => run_mpv_command(
self,
"loadlist",
&[
file.as_ref(),
match option {
PlaylistAddOptions::Append => "append",
PlaylistAddOptions::Replace => "replace",
},
],
),
MpvCommand::PlaylistClear => run_mpv_command(self, "playlist-clear", &[]),
MpvCommand::PlaylistMove { from, to } => {
run_mpv_command(self, "playlist-move", &[&from.to_string(), &to.to_string()])
}
MpvCommand::PlaylistNext => run_mpv_command(self, "playlist-next", &[]),
MpvCommand::PlaylistPrev => run_mpv_command(self, "playlist-prev", &[]),
MpvCommand::PlaylistRemove(id) => {
run_mpv_command(self, "playlist-remove", &[&id.to_string()])
}
MpvCommand::PlaylistShuffle => run_mpv_command(self, "playlist-shuffle", &[]),
MpvCommand::Quit => run_mpv_command(self, "quit", &[]),
MpvCommand::Seek { seconds, option } => run_mpv_command(
self,
"seek",
&[
&seconds.to_string(),
match option {
SeekOptions::Absolute => "absolute",
SeekOptions::Relative => "relative",
SeekOptions::AbsolutePercent => "absolute-percent",
SeekOptions::RelativePercent => "relative-percent",
},
],
),
MpvCommand::Stop => run_mpv_command(self, "stop", &[]),
}
} }
pub fn playlist_add( pub fn playlist_add(
@ -444,33 +507,24 @@ impl Mpv {
option: PlaylistAddOptions, option: PlaylistAddOptions,
) -> Result<(), Error> { ) -> Result<(), Error> {
match file_type { match file_type {
PlaylistAddTypeOptions::File => match option { PlaylistAddTypeOptions::File => self.run_command(MpvCommand::LoadFile {
PlaylistAddOptions::Replace => { file: file.to_string(),
run_mpv_command(self, "loadfile", &[file, "replace"]) option,
} }),
PlaylistAddOptions::Append => run_mpv_command(self, "loadfile", &[file, "append"]),
PlaylistAddOptions::AppendPlay => {
run_mpv_command(self, "loadfile", &[file, "append-play"])
}
},
PlaylistAddTypeOptions::Playlist => match option { PlaylistAddTypeOptions::Playlist => self.run_command(MpvCommand::LoadList {
PlaylistAddOptions::Replace => { file: file.to_string(),
run_mpv_command(self, "loadlist", &[file, "replace"]) option,
} }),
PlaylistAddOptions::Append | PlaylistAddOptions::AppendPlay => {
run_mpv_command(self, "loadlist", &[file, "append"])
}
},
} }
} }
pub fn playlist_clear(&self) -> Result<(), Error> { pub fn playlist_clear(&self) -> Result<(), Error> {
run_mpv_command(self, "playlist-clear", &[]) self.run_command(MpvCommand::PlaylistClear)
} }
pub fn playlist_move_id(&self, from: usize, to: usize) -> Result<(), Error> { pub fn playlist_move_id(&self, from: usize, to: usize) -> Result<(), Error> {
run_mpv_command(self, "playlist-move", &[&from.to_string(), &to.to_string()]) self.run_command(MpvCommand::PlaylistMove { from, to })
} }
pub fn playlist_play_id(&self, id: usize) -> Result<(), Error> { pub fn playlist_play_id(&self, id: usize) -> Result<(), Error> {
@ -479,38 +533,24 @@ impl Mpv {
pub fn playlist_play_next(&self, id: usize) -> Result<(), Error> { pub fn playlist_play_next(&self, id: usize) -> Result<(), Error> {
match get_mpv_property::<usize>(self, "playlist-pos") { match get_mpv_property::<usize>(self, "playlist-pos") {
Ok(current_id) => run_mpv_command( Ok(current_id) => self.run_command(MpvCommand::PlaylistMove {
self, from: id,
"playlist-move", to: current_id + 1,
&[&id.to_string(), &(current_id + 1).to_string()], }),
),
Err(msg) => Err(msg), Err(msg) => Err(msg),
} }
} }
pub fn playlist_remove_id(&self, id: usize) -> Result<(), Error> { pub fn playlist_remove_id(&self, id: usize) -> Result<(), Error> {
run_mpv_command(self, "playlist-remove", &[&id.to_string()]) self.run_command(MpvCommand::PlaylistRemove(id))
} }
pub fn playlist_shuffle(&self) -> Result<(), Error> { pub fn playlist_shuffle(&self) -> Result<(), Error> {
run_mpv_command(self, "playlist-shuffle", &[]) self.run_command(MpvCommand::PlaylistShuffle)
} }
pub fn seek(&self, seconds: f64, option: SeekOptions) -> Result<(), Error> { pub fn seek(&self, seconds: f64, option: SeekOptions) -> Result<(), Error> {
match option { self.run_command(MpvCommand::Seek { seconds, option })
SeekOptions::Absolute => {
run_mpv_command(self, "seek", &[&seconds.to_string(), "absolute"])
}
SeekOptions::AbsolutePercent => {
run_mpv_command(self, "seek", &[&seconds.to_string(), "absolute-percent"])
}
SeekOptions::Relative => {
run_mpv_command(self, "seek", &[&seconds.to_string(), "relative"])
}
SeekOptions::RelativePercent => {
run_mpv_command(self, "seek", &[&seconds.to_string(), "relative-percent"])
}
}
} }
pub fn set_loop_file(&self, option: Switch) -> Result<(), Error> { pub fn set_loop_file(&self, option: Switch) -> Result<(), Error> {
@ -568,29 +608,29 @@ impl Mpv {
set_mpv_property(self, "mute", enabled) set_mpv_property(self, "mute", enabled)
} }
/// #Description /// # Description
/// ///
/// Sets the mpv property _<property>_ to _<value>_. /// Sets the mpv property _<property>_ to _<value>_.
/// ///
/// ##Supported types /// ## Supported types
/// - String /// - String
/// - bool /// - bool
/// - f64 /// - f64
/// - usize /// - usize
/// ///
/// ##Input arguments /// ## Input arguments
/// ///
/// - **property** defines the mpv property that should be retrieved /// - **property** defines the mpv property that should be retrieved
/// - **value** defines the value of the given mpv property _<property>_ /// - **value** defines the value of the given mpv property _<property>_
/// ///
/// #Example /// # Example
/// ``` /// ```
/// # use mpvipc::{Mpv, Error}; /// use mpvipc::{Mpv, Error};
/// # fn main() -> Result<(), Error> { /// fn main() -> Result<(), Error> {
/// let mpv = Mpv::connect("/tmp/mpvsocket")?; /// let mpv = Mpv::connect("/tmp/mpvsocket")?;
/// mpv.set_property("pause", true)?; /// mpv.set_property("pause", true)?;
/// # Ok(()) /// Ok(())
/// # } /// }
/// ``` /// ```
pub fn set_property<T: SetPropertyTypeHandler<T>>( pub fn set_property<T: SetPropertyTypeHandler<T>>(
&self, &self,
@ -635,7 +675,7 @@ impl Mpv {
} }
pub fn stop(&self) -> Result<(), Error> { pub fn stop(&self) -> Result<(), Error> {
run_mpv_command(self, "stop", &[]) self.run_command(MpvCommand::Stop)
} }
pub fn toggle(&self) -> Result<(), Error> { pub fn toggle(&self) -> Result<(), Error> {