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
#[macro_use]
extern crate serde_derive;

#[cfg(test)]
#[macro_use]
extern crate serde_json;

#[cfg(not(test))]
extern crate serde_json;

extern crate failure;
extern crate ommui_file_loading;
extern crate ommui_string_patterns;
extern crate serde;
extern crate uuid;
extern crate xio_base_datatypes;

use ommui_string_patterns::{freetextstring, idstring};
use std::collections::BTreeMap;
use uuid::Uuid;
use xio_base_datatypes as base;

mod hexnumber;

pub mod filesystem;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DeviceDescription {
    pub ftdi: FtdiDescription,
    pub xio: Option<XioDescription>,
    pub system: SystemDescription,
    pub summary: SummaryDescription,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct FtdiDescription {
    pub id: String,
    pub vendor: String,
    pub product: String,
    pub uuid: Uuid,
    pub recognized: bool,
    pub programmed: bool,
    pub board: String,
    pub device: String,
    pub symlink: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct XioDescription {
    pub id: String,
    pub vendor: String,
    pub product: String,
    pub uuid: Uuid,
    pub recognized: bool,
    pub programmed: bool,
    pub board: String,
    pub device: String,
    pub symlink: String,
    pub version: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SystemDescription {
    pub version: String,
    pub firmware: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SummaryDescription {
    pub ready: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct HardwareBoardDescription {
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    pub description: GenericDescription,

    // TODO: verify that the key is an idstring
    pub capabilities: BTreeMap<String, Capability>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GenericDescription {
    #[serde(with = "freetextstring")]
    pub vendor: String,

    #[serde(with = "freetextstring")]
    pub model: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Capability {
    pub builtin: bool,

    #[serde(with = "idstring")]
    pub module: String,

    #[serde(with = "hexnumber")]
    pub id: u16,
}

#[cfg(test)]
mod capability_tests {
    extern crate serde_json;
    use super::Capability;

    #[test]
    fn deserialize() {
        #[cfg_attr(rustfmt, rustfmt_skip)]
        let json = json!({ "builtin": true, "module": "xyz", "id": "0x1234"});
        let cap: Capability =
            serde_json::from_str(&json.to_string()).unwrap();
        assert_eq!(
            cap,
            Capability {
                builtin: true,
                module: "xyz".to_string(),
                id: 0x1234,
            }
        );
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Module {
    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    pub description: GenericDescription,

    pub channels: Vec<Channel>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Channel {
    #[serde(with = "idstring")]
    pub id: String,

    #[serde(rename = "_caption", with = "freetextstring")]
    pub caption: String,

    #[serde(rename = "type")]
    pub channel_type: base::DataType,

    pub permission: base::ChannelPermission,
}