Skip to main content

things3_cloud/wire/
area.rs

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