switchbot_api/
command_request.rs1use std::fmt::Display;
2
3#[derive(Clone, Debug, Default, PartialEq, serde::Serialize)]
20#[serde(rename_all = "camelCase")]
21pub struct CommandRequest {
22 pub command: String,
24
25 #[serde(skip_serializing_if = "CommandRequest::can_omit_parameter")]
27 pub parameter: String,
28
29 #[serde(skip_serializing_if = "CommandRequest::can_omit_command_type")]
31 pub command_type: String,
32}
33
34impl CommandRequest {
35 const DEFAULT_PARAMETER: &str = "default";
36 const DEFAULT_COMMAND_TYPE: &str = "command";
37
38 fn can_omit_parameter(str: &str) -> bool {
39 str.is_empty() || str == Self::DEFAULT_PARAMETER
40 }
41
42 fn can_omit_command_type(str: &str) -> bool {
43 str.is_empty() || str == Self::DEFAULT_COMMAND_TYPE
44 }
45}
46
47impl Display for CommandRequest {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 if !Self::can_omit_command_type(&self.command_type) {
57 write!(f, "{}/", self.command_type)?;
58 }
59 write!(f, "{}", self.command)?;
60 if !Self::can_omit_parameter(&self.parameter) {
61 write!(f, ":{}", self.parameter)?;
62 }
63 Ok(())
64 }
65}
66
67impl From<&str> for CommandRequest {
68 fn from(mut text: &str) -> Self {
107 let mut command = CommandRequest::default();
108 if let Some((name, parameter)) = text.split_once(':') {
109 command.parameter = parameter.into();
110 text = name;
111 }
112 if let Some((command_type, name)) = text.split_once('/') {
113 command.command_type = command_type.into();
114 text = name;
115 }
116 command.command = text.into();
117 command
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn serialize_all() {
127 let all = CommandRequest {
128 command: "test_command".into(),
129 parameter: "param".into(),
130 command_type: "type".into(),
131 };
132 assert_eq!(
133 serde_json::to_string(&all).unwrap(),
134 r#"{"command":"test_command","parameter":"param","commandType":"type"}"#
135 );
136 }
137
138 #[test]
139 fn serialize_default() {
140 let param_type_default = CommandRequest {
141 command: "test_command".into(),
142 ..Default::default()
143 };
144 assert_eq!(
145 serde_json::to_string(¶m_type_default).unwrap(),
146 r#"{"command":"test_command"}"#
147 );
148 }
149
150 #[test]
151 fn serialize_default_str() {
152 let param_type_default = CommandRequest {
153 command: "test_command".into(),
154 parameter: CommandRequest::DEFAULT_PARAMETER.into(),
155 command_type: CommandRequest::DEFAULT_COMMAND_TYPE.into(),
156 };
157 assert_eq!(
158 serde_json::to_string(¶m_type_default).unwrap(),
159 r#"{"command":"test_command"}"#
160 );
161 }
162
163 #[test]
164 fn serialize_empty() {
165 let param_type_empty = CommandRequest {
166 command: "test_command".into(),
167 parameter: String::default(),
168 command_type: String::default(),
169 };
170 assert_eq!(
171 serde_json::to_string(¶m_type_empty).unwrap(),
172 r#"{"command":"test_command"}"#
173 );
174 }
175
176 #[test]
177 fn serialize_param() {
178 let with_param = CommandRequest {
179 command: "test_command".into(),
180 parameter: "param".into(),
181 ..Default::default()
182 };
183 assert_eq!(
184 serde_json::to_string(&with_param).unwrap(),
185 r#"{"command":"test_command","parameter":"param"}"#
186 );
187 }
188
189 #[test]
190 fn serialize_type() {
191 let with_type = CommandRequest {
192 command: "test_command".into(),
193 command_type: "type".into(),
194 ..Default::default()
195 };
196 assert_eq!(
197 serde_json::to_string(&with_type).unwrap(),
198 r#"{"command":"test_command","commandType":"type"}"#
199 );
200 }
201}