Generate JSON string with serde_json

Closes #3
pull/1/head
Jonas Frei 2023-08-02 11:15:37 +02:00
parent ff7a7b7c9d
commit 9e3ec12894
2 changed files with 52 additions and 48 deletions

View File

@ -1,6 +1,6 @@
use super::*;
use log::{debug, warn};
use serde_json::{self, Value};
use serde_json::json;
use std::collections::HashMap;
use std::io::prelude::*;
use std::io::BufReader;
@ -179,17 +179,16 @@ impl TypeHandler for Vec<PlaylistEntry> {
}
pub fn get_mpv_property<T: TypeHandler>(instance: &Mpv, property: &str) -> Result<T, Error> {
let ipc_string = format!("{{ \"command\": [\"get_property\",\"{}\"] }}\n", property);
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
let ipc_string = json!({"command": ["get_property", property]});
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
Ok(val) => T::get_value(val),
Err(why) => Err(Error(ErrorCode::JsonParseError(why.to_string()))),
}
}
pub fn get_mpv_property_string(instance: &Mpv, property: &str) -> Result<String, Error> {
let ipc_string = format!("{{ \"command\": [\"get_property\",\"{}\"] }}\n", property);
let val = serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string))
let ipc_string = json!({"command": ["get_property", property]});
let val = serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string))
.map_err(|why| Error(ErrorCode::JsonParseError(why.to_string())))?;
let map = if let Value::Object(map) = val {
@ -220,29 +219,27 @@ pub fn get_mpv_property_string(instance: &Mpv, property: &str) -> Result<String,
}
}
pub fn set_mpv_property<T: TypeHandler>(
instance: &Mpv,
property: &str,
value: T,
) -> Result<(), Error> {
let ipc_string = format!(
"{{ \"command\": [\"set_property\", \"{}\", {}] }}\n",
property,
value.as_string()
);
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
pub fn set_mpv_property(instance: &Mpv, property: &str, value: Value) -> Result<(), Error> {
let ipc_string = json!({
"command": ["set_property", property, value]
});
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
Ok(_) => Ok(()),
Err(why) => Err(Error(ErrorCode::JsonParseError(why.to_string()))),
}
}
pub fn run_mpv_command(instance: &Mpv, command: &str, args: &[&str]) -> Result<(), Error> {
let mut ipc_string = format!(r#"{{ "command": ["{}""#, command);
for arg in args {
ipc_string.push_str(&format!(r#", "{}""#, arg));
let mut ipc_string = json!({
"command": [command]
});
if let Value::Array(args_array) = &mut ipc_string["command"] {
for arg in args {
args_array.push(json!(arg));
}
}
ipc_string.push_str("] }\n");
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
Ok(feedback) => {
if let Value::String(ref error) = feedback["error"] {
if error == "success" {
@ -259,11 +256,10 @@ pub fn run_mpv_command(instance: &Mpv, command: &str, args: &[&str]) -> Result<(
}
pub fn observe_mpv_property(instance: &Mpv, id: &isize, property: &str) -> Result<(), Error> {
let ipc_string = format!(
"{{ \"command\": [\"observe_property\", {}, \"{}\"] }}\n",
id, property
);
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
let ipc_string = json!({
"command": ["observe_property", id, property]
});
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
Ok(feedback) => {
if let Value::String(ref error) = feedback["error"] {
if error == "success" {
@ -280,8 +276,10 @@ pub fn observe_mpv_property(instance: &Mpv, id: &isize, property: &str) -> Resul
}
pub fn unobserve_mpv_property(instance: &Mpv, id: &isize) -> Result<(), Error> {
let ipc_string = format!("{{ \"command\": [\"unobserve_property\", {}] }}\n", id);
match serde_json::from_str::<Value>(&send_command_sync(instance, &ipc_string)) {
let ipc_string = json!({
"command": ["unobserve_property", id]
});
match serde_json::from_str::<Value>(&send_command_sync(instance, ipc_string)) {
Ok(feedback) => {
if let Value::String(ref error) = feedback["error"] {
if error == "success" {
@ -438,12 +436,13 @@ pub fn listen_raw(instance: &mut Mpv) -> String {
response.trim_end().to_string()
}
fn send_command_sync(instance: &Mpv, command: &str) -> String {
let mut stream = &instance.stream;
match stream.write_all(command.as_bytes()) {
fn send_command_sync(instance: &Mpv, command: Value) -> String {
let stream = &instance.stream;
match serde_json::to_writer(stream, &command) {
Err(why) => panic!("Error: Could not write to socket: {}", why),
Ok(_) => {
debug!("Command: {}", command.trim_end());
let mut stream = stream;
stream.write_all(b"\n").unwrap();
let mut response = String::new();
{
let mut reader = BufReader::new(stream);

View File

@ -1,6 +1,7 @@
pub mod ipc;
use ipc::*;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::fmt::{self, Display};
use std::io::{BufReader, Read};
@ -270,25 +271,25 @@ pub trait SetPropertyTypeHandler<T> {
impl SetPropertyTypeHandler<bool> for bool {
fn set_property_generic(instance: &Mpv, property: &str, value: bool) -> Result<(), Error> {
set_mpv_property::<bool>(instance, property, value)
set_mpv_property(instance, property, json!(value))
}
}
impl SetPropertyTypeHandler<String> for String {
fn set_property_generic(instance: &Mpv, property: &str, value: String) -> Result<(), Error> {
set_mpv_property::<String>(instance, property, value)
set_mpv_property(instance, property, json!(value))
}
}
impl SetPropertyTypeHandler<f64> for f64 {
fn set_property_generic(instance: &Mpv, property: &str, value: f64) -> Result<(), Error> {
set_mpv_property::<f64>(instance, property, value)
set_mpv_property(instance, property, json!(value))
}
}
impl SetPropertyTypeHandler<usize> for usize {
fn set_property_generic(instance: &Mpv, property: &str, value: usize) -> Result<(), Error> {
set_mpv_property::<usize>(instance, property, value)
set_mpv_property(instance, property, json!(value))
}
}
@ -430,7 +431,7 @@ impl Mpv {
}
pub fn pause(&self) -> Result<(), Error> {
set_mpv_property(self, "pause", true)
set_mpv_property(self, "pause", json!(true))
}
pub fn prev(&self) -> Result<(), Error> {
@ -569,7 +570,7 @@ impl Mpv {
}
pub fn playlist_play_id(&self, id: usize) -> Result<(), Error> {
set_mpv_property(self, "playlist-pos", id)
set_mpv_property(self, "playlist-pos", json!(id))
}
pub fn playlist_play_next(&self, id: usize) -> Result<(), Error> {
@ -611,7 +612,7 @@ impl Mpv {
Err(msg) => return Err(msg),
},
}
set_mpv_property(self, "loop-file", enabled)
set_mpv_property(self, "loop-file", json!(enabled))
}
pub fn set_loop_playlist(&self, option: Switch) -> Result<(), Error> {
@ -631,7 +632,7 @@ impl Mpv {
Err(msg) => return Err(msg),
},
}
set_mpv_property(self, "loop-playlist", enabled)
set_mpv_property(self, "loop-playlist", json!(enabled))
}
pub fn set_mute(&self, option: Switch) -> Result<(), Error> {
@ -646,7 +647,7 @@ impl Mpv {
Err(msg) => return Err(msg),
},
}
set_mpv_property(self, "mute", enabled)
set_mpv_property(self, "mute", json!(enabled))
}
/// # Description
@ -685,14 +686,16 @@ impl Mpv {
match get_mpv_property::<f64>(self, "speed") {
Ok(speed) => match option {
NumberChangeOptions::Increase => {
set_mpv_property(self, "speed", speed + input_speed)
set_mpv_property(self, "speed", json!(speed + input_speed))
}
NumberChangeOptions::Decrease => {
set_mpv_property(self, "speed", speed - input_speed)
set_mpv_property(self, "speed", json!(speed - input_speed))
}
NumberChangeOptions::Absolute => set_mpv_property(self, "speed", input_speed),
NumberChangeOptions::Absolute => {
set_mpv_property(self, "speed", json!(input_speed))
}
},
Err(msg) => Err(msg),
}
@ -702,14 +705,16 @@ impl Mpv {
match get_mpv_property::<f64>(self, "volume") {
Ok(volume) => match option {
NumberChangeOptions::Increase => {
set_mpv_property(self, "volume", volume + input_volume)
set_mpv_property(self, "volume", json!(volume + input_volume))
}
NumberChangeOptions::Decrease => {
set_mpv_property(self, "volume", volume - input_volume)
set_mpv_property(self, "volume", json!(volume - input_volume))
}
NumberChangeOptions::Absolute => set_mpv_property(self, "volume", input_volume),
NumberChangeOptions::Absolute => {
set_mpv_property(self, "volume", json!(input_volume))
}
},
Err(msg) => Err(msg),
}