subc_control/
connection_describe.rs1use serde::{Deserialize, Serialize};
7use subc_protocol::peer::OperatorApp;
8
9#[non_exhaustive]
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
12pub struct ConnectionDescribeRequest {
13 op: ConnectionDescribeOp,
14}
15
16impl ConnectionDescribeRequest {
17 pub fn new() -> Self {
18 Self {
19 op: ConnectionDescribeOp::Describe,
20 }
21 }
22}
23
24impl Default for ConnectionDescribeRequest {
25 fn default() -> Self {
26 Self::new()
27 }
28}
29
30#[non_exhaustive]
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
33pub struct ConnectionDescribeReply {
34 op: ConnectionDescribeOp,
35 pub local: bool,
37 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub operator_app: Option<OperatorApp>,
41}
42
43impl ConnectionDescribeReply {
44 pub fn new(local: bool, operator_app: Option<OperatorApp>) -> Self {
45 Self {
46 op: ConnectionDescribeOp::Describe,
47 local,
48 operator_app,
49 }
50 }
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
55enum ConnectionDescribeOp {
56 #[serde(rename = "connection.describe")]
57 Describe,
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63 use serde_json::json;
64
65 #[test]
66 fn connection_describe_bodies_require_the_op() {
67 for op in [None, Some("app.open")] {
68 let mut request = json!({});
69 let mut reply = json!({"local": true});
70 if let Some(op) = op {
71 request["op"] = json!(op);
72 reply["op"] = json!(op);
73 }
74 assert!(serde_json::from_value::<ConnectionDescribeRequest>(request).is_err());
75 assert!(serde_json::from_value::<ConnectionDescribeReply>(reply).is_err());
76 }
77 }
78
79 #[test]
80 fn connection_describe_wire_shapes() {
81 assert_eq!(
82 serde_json::to_value(ConnectionDescribeRequest::new()).unwrap(),
83 json!({"op": "connection.describe"})
84 );
85 assert_eq!(
86 serde_json::to_value(ConnectionDescribeReply::new(false, None)).unwrap(),
87 json!({"op": "connection.describe", "local": false})
88 );
89 let reply = ConnectionDescribeReply::new(true, Some(OperatorApp::new("fixture", 42)));
90 let wire = json!({"op": "connection.describe", "local": true, "operator_app": {"app": "fixture", "pid": 42}});
91 assert_eq!(serde_json::to_value(&reply).unwrap(), wire);
92 assert_eq!(
93 serde_json::from_value::<ConnectionDescribeReply>(wire).unwrap(),
94 reply
95 );
96 }
97}