shoal_core/shared/responses.rs
1//! A response from a set of queries
2use rkyv::{Archive, Deserialize, Serialize};
3use uuid::Uuid;
4
5use crate::shared::traits::ShoalTable;
6
7/// The different response kinds from a query
8#[derive(Debug, Archive, Serialize, Deserialize)]
9pub enum ResponseAction<D: ShoalTable> {
10 /// Whether an insert was successful or not
11 Insert(bool),
12 /// The response from a get query
13 Get(Option<Vec<D>>),
14 /// The response from a delete
15 Delete(bool),
16 /// The response from an update
17 Update(bool),
18}
19
20/// A response from a query
21#[derive(Debug, Archive, Serialize, Deserialize)]
22pub struct Response<T: ShoalTable> {
23 /// The id for the query we are responding too
24 pub id: Uuid,
25 /// This response index in the queries vec
26 pub index: usize,
27 /// The response data
28 pub data: ResponseAction<T>,
29 /// Whether this is the last response for a query or not
30 pub end: bool,
31}
32
33impl<T: ShoalTable> Response<T> {
34 /// Mark this response as the last one
35 pub fn end(&mut self) {
36 self.end = true;
37 }
38}
39
40/// The responses from a set of queries
41#[derive(Debug, Archive, Serialize, Deserialize)]
42pub struct Responses<D: ShoalTable> {
43 /// The responses for our queries
44 pub responses: Vec<Response<D>>,
45}
46
47impl<D: ShoalTable> Responses<D> {
48 /// Create an empty responses object of the correct size
49 ///
50 /// # Arguments
51 ///
52 /// * `len` - The number of queries we are responding too
53 pub fn with_capacity(len: usize) -> Self {
54 // build our responses bundle
55 Responses {
56 responses: Vec::with_capacity(len),
57 }
58 }
59
60 /// A new response to this response bundle
61 ///
62 /// # Arguments
63 ///
64 /// * `response` - The response to add
65 pub fn add(&mut self, response: Response<D>) {
66 // add this response
67 self.responses.push(response);
68 }
69}