1use crate::protocol::ID;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Clone)]
6pub struct StatusRoot {
7 pub status: Status,
8}
9
10#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Clone)]
11pub struct Status {
12 pub update: Option<String>,
13 pub state: Option<String>,
14 pub man: Option<String>,
15 #[serde(rename = "mod")]
16 pub model: Option<String>,
17 pub ver: Option<String>,
18 pub mac: Option<String>,
19 pub push: Option<bool>,
20 pub ntp: Option<bool>,
21}
22
23#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Clone)]
24pub struct ConfigRoot {
25 pub config: Config,
26}
27
28#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Clone)]
29pub struct Config {
30 pub ip: Option<String>,
31 pub nm: Option<String>,
32 pub gw: Option<String>,
33 pub ports: Vec<Port>,
34}
35
36#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Clone)]
37pub struct Port {
38 pub port: u32,
39 pub ts: u32,
40 pub l: u32,
41 pub ss: u32,
42}
43
44#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Clone)]
45pub struct ControlRoot {
46 pub control: Control,
47}
48
49#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Clone)]
50pub struct Control {
51 pub fx: Option<String>,
52 pub int: Option<u32>,
53 pub spd: Option<u32>,
54 pub dir: Option<u32>,
55 pub colors: Option<Vec<Color>>,
56 pub save: Option<u32>,
57 pub power: Option<u32>,
58}
59
60#[derive(Debug, Serialize, Deserialize, PartialEq, Hash, Clone)]
61pub struct Color {
62 pub r: u32,
63 pub g: u32,
64 pub b: u32,
65}
66
67#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
68pub enum Message {
69 Control(ControlRoot),
70 Status(StatusRoot),
71 Config(ConfigRoot),
72 Parsed((ID, Value)),
73 Unparsed((ID, String)),
74}
75
76impl TryInto<Vec<u8>> for Message {
77 type Error = serde_json::Error;
78
79 fn try_into(self) -> Result<Vec<u8>, Self::Error> {
80 match self {
81 Message::Control(c) => serde_json::to_vec(&c),
82 Message::Status(s) => serde_json::to_vec(&s),
83 Message::Config(c) => serde_json::to_vec(&c),
84 Message::Parsed((_, v)) => serde_json::to_vec(&v),
85 Message::Unparsed((_, s)) => Ok(s.as_bytes().to_vec()),
86 }
87 }
88}
89
90impl Message {
91 pub fn get_id(&self) -> ID {
92 match self {
93 Message::Control(_) => ID::Control,
94 Message::Status(_) => ID::Status,
95 Message::Config(_) => ID::Config,
96 Message::Parsed((i, _)) => *i,
97 Message::Unparsed((i, _)) => *i,
98 }
99 }
100}
101
102impl Into<ID> for Message {
103 fn into(self) -> ID {
104 match self {
105 Message::Control(_) => crate::protocol::ID::Control,
106 Message::Status(_) => crate::protocol::ID::Status,
107 Message::Config(_) => crate::protocol::ID::Config,
108 Message::Parsed((i, _)) => i,
109 Message::Unparsed((i, _)) => i,
110 }
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_id_into() {
120 let msg = Message::Parsed((ID::Config, Value::Null));
121 let id: ID = msg.get_id();
122 assert_eq!(id, ID::Config);
123
124 let vm: Vec<u8> = msg.try_into().unwrap();
125 assert_eq!(vm, b"null");
126 }
127}