foxglove/websocket/ws_protocol/client/
get_parameters.rs

1use serde::{Deserialize, Serialize};
2
3use crate::websocket::ws_protocol::JsonMessage;
4
5/// Get parameters message.
6///
7/// Spec: <https://github.com/foxglove/ws-protocol/blob/main/docs/spec.md#get-parameters>
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(tag = "op", rename = "getParameters", rename_all = "camelCase")]
10pub struct GetParameters {
11    /// Parameter names.
12    pub parameter_names: Vec<String>,
13    /// Request ID.
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub id: Option<String>,
16}
17
18impl GetParameters {
19    /// Creates a new get parameters message.
20    pub fn new(parameter_names: impl IntoIterator<Item = impl Into<String>>) -> Self {
21        Self {
22            parameter_names: parameter_names.into_iter().map(|s| s.into()).collect(),
23            id: None,
24        }
25    }
26
27    /// Sets the request ID.
28    #[must_use]
29    pub fn with_id(mut self, id: impl Into<String>) -> Self {
30        self.id = Some(id.into());
31        self
32    }
33}
34
35impl JsonMessage for GetParameters {}
36
37#[cfg(test)]
38mod tests {
39    use crate::websocket::ws_protocol::client::ClientMessage;
40
41    use super::*;
42
43    fn message() -> GetParameters {
44        GetParameters::new(["param1", "param2"])
45    }
46
47    fn message_with_id() -> GetParameters {
48        message().with_id("my-id")
49    }
50
51    #[test]
52    fn test_encode() {
53        insta::assert_json_snapshot!(message());
54    }
55
56    #[test]
57    fn test_encode_with_id() {
58        insta::assert_json_snapshot!(message_with_id());
59    }
60
61    fn test_roundtrip_inner(orig: GetParameters) {
62        let buf = orig.to_string();
63        let msg = ClientMessage::parse_json(&buf).unwrap();
64        assert_eq!(msg, ClientMessage::GetParameters(orig));
65    }
66
67    #[test]
68    fn test_roundtrip() {
69        test_roundtrip_inner(message())
70    }
71
72    #[test]
73    fn test_roundtrip_with_id() {
74        test_roundtrip_inner(message_with_id())
75    }
76}