use serde::{Deserialize, Serialize};
use crate::protocol::JsonMessage;
use crate::protocol::parameter::Parameter;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "op", rename = "setParameters", rename_all = "camelCase")]
pub struct SetParameters {
pub parameters: Vec<Parameter>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
}
impl SetParameters {
pub fn new(parameters: impl IntoIterator<Item = Parameter>) -> Self {
Self {
parameters: parameters.into_iter().collect(),
id: None,
}
}
#[must_use]
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
}
impl JsonMessage for SetParameters {}
#[cfg(test)]
mod tests {
use crate::protocol::parameter::Parameter;
use super::*;
fn message() -> SetParameters {
SetParameters::new([
Parameter::empty("empty"),
Parameter::integer("int", 123),
Parameter::integer_array("int[]", [123, 456]),
Parameter::float64("f64", 1.23),
Parameter::float64_array("f64[]", [1.23, 4.56]),
Parameter::string("str", "hello"),
Parameter::byte_array("byte[]", &[0x10, 0x20, 0x30]),
Parameter::bool("bool", true),
])
}
fn message_with_id() -> SetParameters {
message().with_id("my-id")
}
#[test]
fn test_encode() {
insta::assert_json_snapshot!(message());
}
#[test]
fn test_encode_with_id() {
insta::assert_json_snapshot!(message_with_id());
}
fn test_roundtrip_inner(orig: SetParameters) {
let buf = orig.to_string();
let parsed: SetParameters = serde_json::from_str(&buf).unwrap();
assert_eq!(parsed, orig);
}
#[test]
fn test_roundtrip() {
test_roundtrip_inner(message())
}
#[test]
fn test_roundtrip_with_id() {
test_roundtrip_inner(message_with_id())
}
}