Implemented data structures for SYNC, QUERY, and EXECUTE

This commit is contained in:
2022-12-13 05:38:21 +01:00
parent 6627174c6f
commit e4369ebf41
8 changed files with 499 additions and 67 deletions

View File

@@ -0,0 +1,83 @@
use std::collections::HashMap;
use serde::Serialize;
use crate::response::State;
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Payload {
#[serde(skip_serializing_if = "Option::is_none")]
pub error_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub debug_string: Option<String>,
devices: HashMap<String, Device>,
}
impl Payload {
pub fn new() -> Self {
Self { error_code: None, debug_string: None, devices: HashMap::new() }
}
pub fn add_device(&mut self, id: &str, device: Device) {
self.devices.insert(id.into(), device);
}
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Status {
Success,
Offline,
Exceptions,
Error,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Device {
online: bool,
status: Status,
#[serde(skip_serializing_if = "Option::is_none")]
pub error_code: Option<String>,
#[serde(flatten)]
pub state: Option<State>,
}
impl Device {
pub fn new(online: bool, status: Status) -> Self {
Self { online, status, error_code: None, state: None }
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use uuid::Uuid;
use super::*;
use crate::response::{Response, ResponsePayload, State};
#[test]
fn serialize() {
let mut query_resp = Payload::new();
let state = State::default().on(true);
let mut device = Device::new(true, Status::Success);
device.state = Some(state);
query_resp.add_device("123", device);
let state = State::default().on(false);
let mut device = Device::new(true, Status::Success);
device.state = Some(state);
query_resp.add_device("456", device);
let resp = Response::new(Uuid::from_str("ff36a3cc-ec34-11e6-b1a0-64510650abcf").unwrap(), ResponsePayload::Query(query_resp));
let json = serde_json::to_string(&resp).unwrap();
println!("{}", json);
// @TODO Add a known correct output to test against
}
}