Skip to main content

things3_cloud/wire/
area.rs

1use crate::ids::ThingsId;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use std::collections::BTreeMap;
5
6/// Area wire properties.
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
8pub struct AreaProps {
9    /// `tt`: area title.
10    #[serde(rename = "tt", default)]
11    pub title: String,
12
13    /// `tg`: tag IDs applied to this area.
14    #[serde(rename = "tg", default)]
15    pub tag_ids: Vec<ThingsId>,
16
17    /// `ix`: sort index.
18    #[serde(rename = "ix", default)]
19    pub sort_index: i32,
20
21    /// `xx`: conflict override metadata.
22    #[serde(rename = "xx", default)]
23    pub conflict_overrides: Option<Value>,
24}
25
26/// Sparse patch fields for Area `t=1` updates.
27#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
28pub struct AreaPatch {
29    /// `tt`: title.
30    #[serde(rename = "tt", skip_serializing_if = "Option::is_none")]
31    pub title: Option<String>,
32
33    /// `tg`: tag IDs.
34    #[serde(rename = "tg", skip_serializing_if = "Option::is_none")]
35    pub tag_ids: Option<Vec<ThingsId>>,
36
37    /// `md`: modification timestamp.
38    #[serde(rename = "md", skip_serializing_if = "Option::is_none")]
39    pub modification_date: Option<f64>,
40
41    /// `ix`: sort index.
42    #[serde(rename = "ix", skip_serializing_if = "Option::is_none")]
43    pub sort_index: Option<i32>,
44}
45
46impl AreaPatch {
47    pub fn is_empty(&self) -> bool {
48        self.title.is_none()
49            && self.tag_ids.is_none()
50            && self.modification_date.is_none()
51            && self.sort_index.is_none()
52    }
53
54    pub fn into_properties(self) -> BTreeMap<String, Value> {
55        match serde_json::to_value(self) {
56            Ok(Value::Object(map)) => map.into_iter().collect(),
57            _ => BTreeMap::new(),
58        }
59    }
60}