zero_client/protocol/
down.rs1use super::*;
5
6#[derive(Clone, Debug, PartialEq)]
9pub enum Downstream {
10 Connected(ConnectedBody),
11 Error(ErrorBody),
12 Pong,
13 PokeStart(PokeStartBody),
14 PokePart(PokePartBody),
15 PokeEnd(PokeEndBody),
16 Pull(PullResponseBody),
17 DeleteClients(DeleteClientsBody),
18 PushResponse(PushResponseBody),
19 Inspect(InspectDownBody),
20 TransformError(Vec<ErroredQuery>),
22}
23
24impl Downstream {
25 pub fn tag(&self) -> &'static str {
26 match self {
27 Downstream::Connected(_) => "connected",
28 Downstream::Error(_) => "error",
29 Downstream::Pong => "pong",
30 Downstream::PokeStart(_) => "pokeStart",
31 Downstream::PokePart(_) => "pokePart",
32 Downstream::PokeEnd(_) => "pokeEnd",
33 Downstream::Pull(_) => "pull",
34 Downstream::DeleteClients(_) => "deleteClients",
35 Downstream::PushResponse(_) => "pushResponse",
36 Downstream::Inspect(_) => "inspect",
37 Downstream::TransformError(_) => "transformError",
38 }
39 }
40
41 pub fn from_text(text: &str) -> Result<Self> {
43 Self::from_json(&parse_message(text)?)
44 }
45
46 pub fn to_text(&self) -> String {
48 json::stringify(&self.to_json())
49 }
50}
51
52impl JsonCodable for Downstream {
53 fn from_json(v: &Value) -> Result<Self> {
54 let Value::Array(a) = v else {
55 return Err(DecodeError::new("Expected array"));
56 };
57 let [Value::String(tag), body] = a.as_slice() else {
58 return Err(DecodeError::new("Expected [tag, body] tuple"));
59 };
60 let at = |e: DecodeError| e.at("1");
61 Ok(match tag.as_str() {
62 "connected" => Downstream::Connected(ConnectedBody::from_json(body).map_err(at)?),
63 "error" => Downstream::Error(ErrorBody::from_json(body).map_err(at)?),
64 "pong" => {
65 decode::object(body).map_err(at)?;
66 Downstream::Pong
67 }
68 "pokeStart" => Downstream::PokeStart(PokeStartBody::from_json(body).map_err(at)?),
69 "pokePart" => Downstream::PokePart(PokePartBody::from_json(body).map_err(at)?),
70 "pokeEnd" => Downstream::PokeEnd(PokeEndBody::from_json(body).map_err(at)?),
71 "pull" => Downstream::Pull(PullResponseBody::from_json(body).map_err(at)?),
72 "deleteClients" => {
73 Downstream::DeleteClients(DeleteClientsBody::from_json(body).map_err(at)?)
74 }
75 "pushResponse" => {
76 Downstream::PushResponse(PushResponseBody::from_json(body).map_err(at)?)
77 }
78 "inspect" => Downstream::Inspect(InspectDownBody::from_json(body).map_err(at)?),
79 "transformError" => Downstream::TransformError(
80 decode::array(body, ErroredQuery::from_json).map_err(at)?,
81 ),
82 _ => {
83 return Err(DecodeError::new(format!("Unknown message type \"{tag}\"")).at("0"));
84 }
85 })
86 }
87
88 fn to_json(&self) -> Value {
89 let body = match self {
90 Downstream::Connected(b) => b.to_json(),
91 Downstream::Error(b) => b.to_json(),
92 Downstream::Pong => Value::Object(Object::new()),
93 Downstream::PokeStart(b) => b.to_json(),
94 Downstream::PokePart(b) => b.to_json(),
95 Downstream::PokeEnd(b) => b.to_json(),
96 Downstream::Pull(b) => b.to_json(),
97 Downstream::DeleteClients(b) => b.to_json(),
98 Downstream::PushResponse(b) => b.to_json(),
99 Downstream::Inspect(b) => b.to_json(),
100 Downstream::TransformError(b) => array_json(b),
101 };
102 message(self.tag(), body)
103 }
104}