1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use crate::resource;
use derive_setters::Setters;
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};

/// A resourcelink to group resources in the bridge.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
pub struct Resourcelink {
    /// Identifier of the resourcelink.
    #[serde(skip)]
    pub id: String,
    /// Name of the resourcelink.
    pub name: String,
    /// Description of the resourcelink.
    pub description: String,
    /// Owner of the resourcelink.
    pub owner: String,
    /// Kind of the resourcelink.
    #[serde(rename = "type")]
    pub kind: Kind,
    /// Class identifier of the resourcelink.
    #[serde(rename = "classid")]
    pub class_id: u16,
    /// Whether the resource is automatically deleted when not referenced anymore.
    pub recycle: bool,
    /// References to resources which are used by this resourcelink.
    pub links: Vec<Link>,
}

impl Resourcelink {
    pub(crate) fn with_id(self, id: String) -> Self {
        Self { id, ..self }
    }
}

impl resource::Resource for Resourcelink {}

/// Kind of a resourcelink.
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub enum Kind {
    Link,
}

/// A reference to a resource.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Link {
    /// Kind of the resource.
    pub kind: LinkKind,
    /// Identifier of the resource.
    pub id: String,
}

impl<'de> Deserialize<'de> for Link {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value: String = Deserialize::deserialize(deserializer)?;
        let mut values: Vec<&str> = value.split('/').collect();
        let id_str = values
            .pop()
            .ok_or_else(|| D::Error::custom("expected link in the format /<kind>/<id>"))?;
        let kind_str = values
            .pop()
            .ok_or_else(|| D::Error::custom("expected link in the format /<kind>/<id>"))?;
        Ok(Self {
            kind: LinkKind::from_str(kind_str)
                .ok_or_else(|| D::Error::custom(format!("invalid link type '{}'", kind_str)))?,
            id: id_str.to_owned(),
        })
    }
}

impl Serialize for Link {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&format!("/{}/{}", self.kind.as_str(), self.id))
    }
}

/// Kind of a link.
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum LinkKind {
    Group,
    Light,
    Resourcelink,
    Rule,
    Scene,
    Schedule,
    Sensor,
}

impl LinkKind {
    fn from_str(value: &str) -> Option<Self> {
        match value {
            "groups" => Some(Self::Group),
            "lights" => Some(Self::Light),
            "resourcelinks" => Some(Self::Resourcelink),
            "rules" => Some(Self::Rule),
            "scenes" => Some(Self::Scene),
            "schedules" => Some(Self::Schedule),
            "sensors" => Some(Self::Sensor),
            _ => None,
        }
    }

    fn as_str(&self) -> &str {
        match self {
            Self::Group => "groups",
            Self::Light => "lights",
            Self::Resourcelink => "resourcelinks",
            Self::Rule => "rules",
            Self::Scene => "scenes",
            Self::Schedule => "schedules",
            Self::Sensor => "sensors",
        }
    }
}

/// Struct for creating a resourcelink.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Setters)]
#[setters(strip_option, prefix = "with_")]
pub struct Creator {
    /// Sets the name of the resourcelink.
    #[setters(skip)]
    pub name: String,
    /// Sets the description of the resourcelink.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Sets the owner of the resourcelink.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub owner: Option<String>,
    /// Sets the kind of the resourcelink.
    #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
    pub kind: Option<Kind>,
    /// Sets the class id of the resourcelink.
    #[serde(rename = "classid")]
    #[setters(skip)]
    pub class_id: u16,
    /// Sets the whether to recycle the resourcelink.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recycle: Option<bool>,
    /// Sets the links of the resourcelink.
    #[setters(skip)]
    pub links: Vec<Link>,
}

impl Creator {
    /// Creates a new [`Creator`].
    pub fn new(name: String, class_id: u16, links: Vec<Link>) -> Self {
        Self {
            name,
            description: None,
            owner: None,
            kind: None,
            class_id,
            recycle: None,
            links,
        }
    }
}

impl resource::Creator for Creator {
    fn url_suffix() -> String {
        "resourcelinks".to_owned()
    }
}

/// Modifier for a resourcelink.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Setters)]
#[setters(strip_option, prefix = "with_")]
pub struct Modifier {
    /// Sets the name of the resourcelink.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Sets the description of the resourcelink.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Sets the class id of the resourcelink.
    #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
    pub kind: Option<Kind>,
    /// Sets the kind of the resourcelink.
    #[serde(skip_serializing_if = "Option::is_none", rename = "classid")]
    pub class_id: Option<u16>,
    /// Sets the links of the resourcelink.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub links: Option<Vec<Link>>,
}

impl Modifier {
    /// Creates a new [`Modifier`].
    pub fn new() -> Self {
        Self::default()
    }
}

impl resource::Modifier for Modifier {
    type Id = String;
    fn url_suffix(id: Self::Id) -> String {
        format!("resourcelinks/{}", id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn serialize_creator() {
        let links = vec![
            Link {
                kind: LinkKind::Sensor,
                id: "1".into(),
            },
            Link {
                kind: LinkKind::Schedule,
                id: "2".into(),
            },
        ];

        let creator = Creator::new("test".into(), 1, links.clone());
        let creator_json = serde_json::to_value(creator).unwrap();
        let expected_json = json!({
            "name": "test",
            "classid": 1,
            "links": ["/sensors/1", "/schedules/2"],
        });
        assert_eq!(creator_json, expected_json);

        let creator = Creator {
            name: "test".into(),
            description: Some("description test".into()),
            owner: Some("owner test".into()),
            kind: Some(Kind::Link),
            class_id: 1,
            recycle: Some(true),
            links,
        };
        let creator_json = serde_json::to_value(creator).unwrap();
        let expected_json = json!({
            "name": "test",
            "description": "description test",
            "owner": "owner test",
            "type": "Link",
            "classid": 1,
            "recycle": true,
            "links": ["/sensors/1", "/schedules/2"]
        });
        assert_eq!(creator_json, expected_json);
    }

    #[test]
    fn serialize_modifier() {
        let modifier = Modifier::new();
        let modifier_json = serde_json::to_value(modifier).unwrap();
        let expected_json = json!({});
        assert_eq!(modifier_json, expected_json);

        let modifier = Modifier {
            name: Some("test".into()),
            description: Some("description test".into()),
            kind: Some(Kind::Link),
            class_id: Some(1),
            links: Some(vec![
                Link {
                    kind: LinkKind::Group,
                    id: "1".into(),
                },
                Link {
                    kind: LinkKind::Scene,
                    id: "2".into(),
                },
            ]),
        };
        let modifier_json = serde_json::to_value(modifier).unwrap();
        let expected_json = json!({
            "name": "test",
            "description": "description test",
            "type": "Link",
            "classid": 1,
            "links": ["/groups/1", "/scenes/2"]
        });
        assert_eq!(modifier_json, expected_json);
    }
}