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]
name = "mpvipc"
version = "1.1.7"
version = "1.1.8"
authors = ["Jonas Frei <freijon@pm.me>"]
description = "A small library which provides bindings to control existing mpv instances through sockets."
license = "GPL-3.0"

View File

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

View File

@ -9,19 +9,6 @@ use std::os::unix::net::UnixStream;
#[derive(Debug)]
pub enum Event {
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,
EndFile,
FileLoaded,
@ -36,7 +23,7 @@ pub enum Event {
MetadataUpdate,
Seek,
PlaybackRestart,
PropertyChange(isize, Property),
PropertyChange { id: isize, property: Property },
ChapterChange,
Unimplemented,
}
@ -48,10 +35,33 @@ pub enum Property {
PlaybackTime(Option<f64>),
Duration(Option<f64>),
Metadata(Option<HashMap<String, MpvDataType>>),
Unknown {
name: String,
data: MpvDataType,
Unknown { 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)]
@ -75,7 +85,6 @@ pub enum NumberChangeOptions {
pub enum PlaylistAddOptions {
Replace,
Append,
AppendPlay,
}
pub enum PlaylistAddTypeOptions {
@ -330,13 +339,13 @@ impl Mpv {
///
/// # Example
/// ```
/// # use mpvipc::{Mpv, Error};
/// # fn main() -> Result<(), Error> {
/// use mpvipc::{Mpv, Error};
/// fn main() -> Result<(), Error> {
/// let mpv = Mpv::connect("/tmp/mpvsocket")?;
/// let paused: bool = mpv.get_property("pause")?;
/// let title: String = mpv.get_property("media-title")?;
/// # Ok(())
/// # }
/// Ok(())
/// }
/// ```
pub fn get_property<T: GetPropertyTypeHandler>(&self, property: &str) -> Result<T, Error> {
T::get_property_generic(self, property)
@ -354,19 +363,19 @@ impl Mpv {
/// # Example
///
/// ```
/// # use mpvipc::{Mpv, Error};
/// # fn main() -> Result<(), Error> {
/// use mpvipc::{Mpv, Error};
/// fn main() -> Result<(), Error> {
/// let mpv = Mpv::connect("/tmp/mpvsocket")?;
/// let title = mpv.get_property_string("media-title")?;
/// # Ok(())
/// # }
/// Ok(())
/// }
/// ```
pub fn get_property_string(&self, property: &str) -> Result<String, Error> {
get_mpv_property_string(self, property)
}
pub fn kill(&self) -> Result<(), Error> {
run_mpv_command(self, "quit", &[])
self.run_command(MpvCommand::Quit)
}
/// # Description
@ -391,7 +400,7 @@ impl Mpv {
}
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> {
@ -403,11 +412,14 @@ impl Mpv {
}
pub fn prev(&self) -> Result<(), Error> {
run_mpv_command(self, "playlist-prev", &[])
self.run_command(MpvCommand::PlaylistPrev)
}
pub fn restart(&self) -> Result<(), Error> {
run_mpv_command(self, "seek", &["0", "absolute"])
self.run_command(MpvCommand::Seek {
seconds: 0f64,
option: SeekOptions::Absolute,
})
}
/// # Description
@ -421,20 +433,71 @@ impl Mpv {
///
/// # Example
/// ```
/// # use mpvipc::{Mpv, Error};
/// # fn main() -> Result<(), Error> {
/// use mpvipc::{Mpv, Error};
/// fn main() -> Result<(), Error> {
/// let mpv = Mpv::connect("/tmp/mpvsocket")?;
///
/// //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
/// mpv.run_command("seek", &["0", "absolute"])?;
/// # Ok(())
/// # }
/// mpv.run_command(MpvCommand::Seek {
/// seconds: 0f64,
/// option: SeekOptions::Absolute,
/// })?;
/// Ok(())
/// }
/// ```
pub fn run_command(&self, command: &str, args: &[&str]) -> Result<(), Error> {
run_mpv_command(self, command, args)
pub fn run_command(&self, command: MpvCommand) -> Result<(), Error> {
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(
@ -444,33 +507,24 @@ impl Mpv {
option: PlaylistAddOptions,
) -> Result<(), Error> {
match file_type {
PlaylistAddTypeOptions::File => match option {
PlaylistAddOptions::Replace => {
run_mpv_command(self, "loadfile", &[file, "replace"])
}
PlaylistAddOptions::Append => run_mpv_command(self, "loadfile", &[file, "append"]),
PlaylistAddOptions::AppendPlay => {
run_mpv_command(self, "loadfile", &[file, "append-play"])
}
},
PlaylistAddTypeOptions::File => self.run_command(MpvCommand::LoadFile {
file: file.to_string(),
option,
}),
PlaylistAddTypeOptions::Playlist => match option {
PlaylistAddOptions::Replace => {
run_mpv_command(self, "loadlist", &[file, "replace"])
}
PlaylistAddOptions::Append | PlaylistAddOptions::AppendPlay => {
run_mpv_command(self, "loadlist", &[file, "append"])
}
},
PlaylistAddTypeOptions::Playlist => self.run_command(MpvCommand::LoadList {
file: file.to_string(),
option,
}),
}
}
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> {
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> {
@ -479,38 +533,24 @@ impl Mpv {
pub fn playlist_play_next(&self, id: usize) -> Result<(), Error> {
match get_mpv_property::<usize>(self, "playlist-pos") {
Ok(current_id) => run_mpv_command(
self,
"playlist-move",
&[&id.to_string(), &(current_id + 1).to_string()],
),
Ok(current_id) => self.run_command(MpvCommand::PlaylistMove {
from: id,
to: current_id + 1,
}),
Err(msg) => Err(msg),
}
}
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> {
run_mpv_command(self, "playlist-shuffle", &[])
self.run_command(MpvCommand::PlaylistShuffle)
}
pub fn seek(&self, seconds: f64, option: SeekOptions) -> Result<(), Error> {
match 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"])
}
}
self.run_command(MpvCommand::Seek { seconds, option })
}
pub fn set_loop_file(&self, option: Switch) -> Result<(), Error> {
@ -585,12 +625,12 @@ impl Mpv {
///
/// # Example
/// ```
/// # use mpvipc::{Mpv, Error};
/// # fn main() -> Result<(), Error> {
/// use mpvipc::{Mpv, Error};
/// fn main() -> Result<(), Error> {
/// let mpv = Mpv::connect("/tmp/mpvsocket")?;
/// mpv.set_property("pause", true)?;
/// # Ok(())
/// # }
/// Ok(())
/// }
/// ```
pub fn set_property<T: SetPropertyTypeHandler<T>>(
&self,
@ -635,7 +675,7 @@ impl Mpv {
}
pub fn stop(&self) -> Result<(), Error> {
run_mpv_command(self, "stop", &[])
self.run_command(MpvCommand::Stop)
}
pub fn toggle(&self) -> Result<(), Error> {