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
//! `Command` module
//!
use serde::{de::DeserializeOwned, Deserialize, Serialize};

/// `Command` definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Command {
    /// Unique command's id
    device_command_id: u64,
    /// Unique command's short name
    command_short_name: String,
    /// Command's parameters
    parameters: serde_json::Value,
}

impl Command {
    /// Returns unique device command's id
    #[must_use]
    pub const fn device_command_id(&self) -> u64 {
        self.device_command_id
    }

    /// Returns unique command's short name
    #[must_use]
    pub const fn command_short_name(&self) -> &String {
        &self.command_short_name
    }

    /// Command's parameters
    ///
    /// # Errors
    ///
    /// This conversion can fail if the structure of the Value does not match the
    /// structure expected by `T`, for example if `T` is a struct type but the Value
    /// contains something other than a JSON map. It can also fail if the structure
    /// is correct but `T`'s implementation of `Deserialize` decides that something
    /// is wrong with the data, for example required struct fields are missing from
    /// the JSON map or some number is too big to fit in the expected primitive
    /// type.
    pub fn parameters<T>(&self) -> Result<T, serde_json::Error>
    where
        T: DeserializeOwned,
    {
        serde_json::from_value::<T>(self.parameters.clone())
    }
}