Skip to main content

core_api/node/
settings.rs

1use serde::{Deserialize, Serialize};
2
3pub const VERSION: &str = crate::node::V1;
4pub const STATUS: &str = "settings/status";
5pub const UPDATE: &str = "settings/update";
6pub const REVERT: &str = "settings/revert";
7pub const RETRY: &str = "settings/retry";
8pub const CHANGED_TARGET: &str = "/node/settings/changed";
9
10#[derive(Debug, Clone, Default, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase", deny_unknown_fields)]
12pub struct StatusRequest {}
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15#[serde(rename_all = "camelCase", deny_unknown_fields)]
16pub struct UpdateRequest<T> {
17    pub idempotency_key: String,
18    pub section: String,
19    pub expected_revision: u64,
20    pub value: T,
21}
22
23impl<T> UpdateRequest<T> {
24    pub fn new(idempotency_key: String, section: String, expected_revision: u64, value: T) -> Self {
25        Self {
26            idempotency_key,
27            section,
28            expected_revision,
29            value,
30        }
31    }
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(rename_all = "camelCase", deny_unknown_fields)]
36pub struct SectionCommandRequest {
37    pub idempotency_key: String,
38    pub section: String,
39    pub expected_revision: u64,
40}
41
42impl SectionCommandRequest {
43    pub fn new(idempotency_key: String, section: String, expected_revision: u64) -> Self {
44        Self {
45            idempotency_key,
46            section,
47            expected_revision,
48        }
49    }
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
53#[serde(rename_all = "camelCase")]
54pub struct ChangedEvent {
55    pub version: String,
56    pub node_id: String,
57    pub node_type: String,
58    pub event_seq: u64,
59    pub section: String,
60    pub revision: u64,
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn changed_event_accepts_additive_fields_without_internal_identity() {
69        let event = serde_json::from_value::<ChangedEvent>(serde_json::json!({
70            "version": "v1",
71            "nodeId": "camera-1",
72            "nodeType": "camera",
73            "eventSeq": 7,
74            "section": "recognition",
75            "revision": 3,
76            "futureField": true
77        }))
78        .unwrap();
79        assert_eq!(event.node_id, "camera-1");
80    }
81}