1use super::*;
6use crate::ast::{Ast, strict};
7
8#[derive(Clone, Debug, PartialEq)]
10pub enum QueriesPatchOp {
11 Put { hash: String, ttl: Option<f64> },
12 Del { hash: String },
13 Clear,
14}
15
16impl JsonCodable for QueriesPatchOp {
17 fn from_json(v: &Value) -> Result<Self> {
18 let r = Reader::new(v, "queries patch op")?;
19 match r.string("op")?.as_str() {
20 "put" => Ok(QueriesPatchOp::Put {
21 hash: r.string("hash")?,
22 ttl: r.optional_number("ttl")?,
23 }),
24 "del" => Ok(QueriesPatchOp::Del {
25 hash: r.string("hash")?,
26 }),
27 "clear" => Ok(QueriesPatchOp::Clear),
28 op => Err(DecodeError::new(format!("Invalid queries patch op \"{op}\"")).at("op")),
29 }
30 }
31
32 fn to_json(&self) -> Value {
33 match self {
34 QueriesPatchOp::Put { hash, ttl } => Builder::new()
35 .set("op", "put")
36 .set("hash", hash.clone())
37 .opt("ttl", *ttl)
38 .build(),
39 QueriesPatchOp::Del { hash } => Builder::new()
40 .set("op", "del")
41 .set("hash", hash.clone())
42 .build(),
43 QueriesPatchOp::Clear => Builder::new().set("op", "clear").build(),
44 }
45 }
46}
47
48pub type QueriesPatch = Vec<QueriesPatchOp>;
49
50pub(crate) fn queries_patch(v: &Value) -> Result<QueriesPatch> {
51 decode::array(v, QueriesPatchOp::from_json)
52}
53
54#[derive(Clone, Debug, PartialEq)]
57pub struct UpQueriesPut {
58 pub hash: String,
59 pub ttl: Option<f64>,
60 pub ast: Option<Ast>,
61 pub name: Option<String>,
62 pub args: Option<Vec<Value>>,
63}
64
65#[derive(Clone, Debug, PartialEq)]
68pub enum UpQueriesPatchOp {
69 Put(Box<UpQueriesPut>),
70 Del { hash: String },
71 Clear,
72}
73
74impl JsonCodable for UpQueriesPatchOp {
75 fn from_json(v: &Value) -> Result<Self> {
76 let r = Reader::new(v, "queries patch op")?;
77 match r.string("op")?.as_str() {
78 "put" => Ok(UpQueriesPatchOp::Put(Box::new(UpQueriesPut {
79 hash: r.string("hash")?,
80 ttl: r.optional_number("ttl")?,
81 ast: r.optional("ast", strict::decode)?,
82 name: r.optional_string("name")?,
83 args: r.optional("args", |v| Ok(decode::array_value(v)?.clone()))?,
84 }))),
85 "del" => Ok(UpQueriesPatchOp::Del {
86 hash: r.string("hash")?,
87 }),
88 "clear" => Ok(UpQueriesPatchOp::Clear),
89 op => Err(DecodeError::new(format!("Invalid queries patch op \"{op}\"")).at("op")),
90 }
91 }
92
93 fn to_json(&self) -> Value {
94 match self {
95 UpQueriesPatchOp::Put(p) => Builder::new()
96 .set("op", "put")
97 .set("hash", p.hash.clone())
98 .opt("ttl", p.ttl)
99 .opt("ast", p.ast.as_ref().map(Ast::to_json))
100 .opt("name", p.name.clone())
101 .opt("args", p.args.clone())
102 .build(),
103 UpQueriesPatchOp::Del { hash } => Builder::new()
104 .set("op", "del")
105 .set("hash", hash.clone())
106 .build(),
107 UpQueriesPatchOp::Clear => Builder::new().set("op", "clear").build(),
108 }
109 }
110}
111
112pub type UpQueriesPatch = Vec<UpQueriesPatchOp>;
113
114pub(crate) fn up_queries_patch(v: &Value) -> Result<UpQueriesPatch> {
115 decode::array(v, UpQueriesPatchOp::from_json)
116}
117
118#[derive(Clone, Debug, PartialEq)]
120pub struct ChangeDesiredQueriesBody {
121 pub desired_queries_patch: UpQueriesPatch,
122 pub traceparent: Option<String>,
124}
125
126impl JsonCodable for ChangeDesiredQueriesBody {
127 fn from_json(v: &Value) -> Result<Self> {
128 let r = Reader::new(v, "changeDesiredQueries body")?;
129 Ok(ChangeDesiredQueriesBody {
130 desired_queries_patch: r.required("desiredQueriesPatch", up_queries_patch)?,
131 traceparent: r.optional_string("traceparent")?,
132 })
133 }
134
135 fn to_json(&self) -> Value {
136 Builder::new()
137 .set(
138 "desiredQueriesPatch",
139 array_json(&self.desired_queries_patch),
140 )
141 .opt("traceparent", self.traceparent.clone())
142 .build()
143 }
144}
145
146#[derive(Clone, Debug, PartialEq)]
148pub struct PullRequestBody {
149 pub client_group_id: String,
150 pub cookie: Option<Version>,
151 pub request_id: String,
152}
153
154impl JsonCodable for PullRequestBody {
155 fn from_json(v: &Value) -> Result<Self> {
156 let r = Reader::new(v, "pull request body")?;
157 Ok(PullRequestBody {
158 client_group_id: r.string("clientGroupID")?,
159 cookie: r.required("cookie", nullable_version)?,
160 request_id: r.string("requestID")?,
161 })
162 }
163
164 fn to_json(&self) -> Value {
165 Builder::new()
166 .set("clientGroupID", self.client_group_id.clone())
167 .set("cookie", nullable_version_json(&self.cookie))
168 .set("requestID", self.request_id.clone())
169 .build()
170 }
171}
172
173#[derive(Clone, Debug, PartialEq)]
176pub struct PullResponseBody {
177 pub cookie: Version,
178 pub request_id: String,
180 pub last_mutation_id_changes: Vec<(String, f64)>,
181}
182
183impl JsonCodable for PullResponseBody {
184 fn from_json(v: &Value) -> Result<Self> {
185 let r = Reader::new(v, "pull response body")?;
186 Ok(PullResponseBody {
187 cookie: r.string("cookie")?,
188 request_id: r.string("requestID")?,
189 last_mutation_id_changes: r.required("lastMutationIDChanges", number_record)?,
190 })
191 }
192
193 fn to_json(&self) -> Value {
194 Builder::new()
195 .set("cookie", self.cookie.clone())
196 .set("requestID", self.request_id.clone())
197 .set(
198 "lastMutationIDChanges",
199 number_record_json(&self.last_mutation_id_changes),
200 )
201 .build()
202 }
203}
204
205#[derive(Clone, Debug, PartialEq, Default)]
207pub struct DeleteClientsBody {
208 pub client_ids: Option<Vec<String>>,
209 pub client_group_ids: Option<Vec<String>>,
210}
211
212impl JsonCodable for DeleteClientsBody {
213 fn from_json(v: &Value) -> Result<Self> {
214 let r = Reader::new(v, "deleteClients body")?;
215 Ok(DeleteClientsBody {
216 client_ids: r.optional("clientIDs", decode::strings)?,
217 client_group_ids: r.optional("clientGroupIDs", decode::strings)?,
218 })
219 }
220
221 fn to_json(&self) -> Value {
222 Builder::new()
223 .opt(
224 "clientIDs",
225 self.client_ids.as_deref().map(decode::strings_json),
226 )
227 .opt(
228 "clientGroupIDs",
229 self.client_group_ids.as_deref().map(decode::strings_json),
230 )
231 .build()
232 }
233}
234
235#[derive(Clone, Debug, PartialEq)]
237pub struct UpdateAuthBody {
238 pub auth: String,
239}
240
241impl JsonCodable for UpdateAuthBody {
242 fn from_json(v: &Value) -> Result<Self> {
243 Ok(UpdateAuthBody {
244 auth: Reader::new(v, "updateAuth body")?.string("auth")?,
245 })
246 }
247
248 fn to_json(&self) -> Value {
249 Builder::new().set("auth", self.auth.clone()).build()
250 }
251}
252
253#[derive(Clone, Debug, PartialEq)]
256pub enum ErroredQuery {
257 App {
260 id: String,
261 name: String,
262 message: Option<String>,
263 details: Option<Value>,
264 },
265 Parse {
267 id: String,
268 name: String,
269 message: String,
270 details: Option<Value>,
271 },
272}
273
274impl JsonCodable for ErroredQuery {
275 fn from_json(v: &Value) -> Result<Self> {
276 let r = Reader::new(v, "errored query")?;
277 let details = r.get("details").cloned();
278 match r.string("error")?.as_str() {
279 "app" => Ok(ErroredQuery::App {
280 id: r.string("id")?,
281 name: r.string("name")?,
282 message: r.optional_string("message")?,
283 details,
284 }),
285 "parse" => Ok(ErroredQuery::Parse {
286 id: r.string("id")?,
287 name: r.string("name")?,
288 message: r.string("message")?,
289 details,
290 }),
291 e => Err(DecodeError::new(format!("Invalid query error \"{e}\"")).at("error")),
292 }
293 }
294
295 fn to_json(&self) -> Value {
296 let (error, id, name, message, details) = match self {
297 ErroredQuery::App {
298 id,
299 name,
300 message,
301 details,
302 } => ("app", id, name, message.clone(), details),
303 ErroredQuery::Parse {
304 id,
305 name,
306 message,
307 details,
308 } => ("parse", id, name, Some(message.clone()), details),
309 };
310 Builder::new()
311 .set("error", error)
312 .set("id", id.clone())
313 .set("name", name.clone())
314 .opt("message", message)
315 .opt("details", details.clone())
316 .build()
317 }
318}