Add UnitResponse for commands without response payload

This commit is contained in:
kageru 2020-06-21 23:19:12 +02:00
parent b31abb7320
commit 74ccb40df5
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 22 additions and 2 deletions

View File

@ -4,7 +4,7 @@ mod error;
use error::{Error, MpdResult};
use itertools::Itertools;
mod structs;
pub use structs::{Position, State, Stats, Status, Track};
pub use structs::{Position, State, Stats, Status, Track, UnitResponse};
/// some unprintable character to separate repeated keys
const SEPARATOR: char = '\x02';
@ -288,7 +288,8 @@ artists: 2841
albums: 2455
songs: 40322
db_playtime: 11620284
db_update: 1588433046";
db_update: 1588433046
OK";
let s: Stats = deserialize_response(input_str.lines()).unwrap();
assert_eq!(
s,
@ -303,4 +304,18 @@ db_update: 1588433046";
}
);
}
#[test]
fn de_unit_response_test() {
let success = "OK";
let r: Result<UnitResponse, _> = deserialize_response(success.lines());
assert_eq!(r, Ok(UnitResponse {}));
let failure = r#"ACK [2@0] {consume} wrong number of arguments for "consume""#;
let r: Result<UnitResponse, _> = deserialize_response(failure.lines());
assert_eq!(
r,
Err(error::Error::from_str(r#"[2@0] {consume} wrong number of arguments for "consume""#))
);
}
}

View File

@ -47,6 +47,11 @@ pub struct Track {
pub composer: Option<String>,
}
/// An empty struct that can be used as the response data for commands that only ever return `OK`
/// or an error message, e.g. `consume` or other boolean toggles.
#[derive(Deserialize, PartialEq, Debug)]
pub struct UnitResponse {}
/// The position of an item in a list with an optional total length.
/// Can be used for e.g. track number,
/// where `Position { 3, 12 }` represents track 3 of a CD with 12 tracks,