glow_control_lib/util/
traits.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
9pub struct ResponseCode {
10 pub code: u32,
11 pub message: &'static str,
12}
13
14impl ResponseCode {
15 pub fn is_ok(&self) -> bool {
22 self.code == OK.code
23 }
24
25 pub fn is_error(&self) -> bool {
27 !self.is_ok()
28 }
29}
30
31pub const OK: ResponseCode = ResponseCode {
35 code: 1000,
36 message: "Ok",
37};
38pub const ERROR: ResponseCode = ResponseCode {
40 code: 1001,
41 message: "Error",
42};
43pub const ERROR_INVALID_ARGUMENT: ResponseCode = ResponseCode {
45 code: 1101,
46 message: "Invalid argument value",
47};
48pub const ERROR2: ResponseCode = ResponseCode {
50 code: 1102,
51 message: "Error",
52};
53pub const ERROR_VALUE_WRONG_MISSING_KEY: ResponseCode = ResponseCode {
55 code: 1103,
56 message: "Error - value too long? Or missing required object key?",
57};
58pub const ERROR_MALFORMED_JSON_INPUT: ResponseCode = ResponseCode {
60 code: 1104,
61 message: "Error - malformed JSON on input?",
62};
63pub const ERROR_INVALID_ARGUMENT_KEY: ResponseCode = ResponseCode {
65 code: 1105,
66 message: "Invalid argument key",
67};
68pub const OK2: ResponseCode = ResponseCode {
70 code: 1107,
71 message: "OK?",
72};
73pub const OK3: ResponseCode = ResponseCode {
75 code: 1108,
76 message: "OK?",
77};
78pub const FIRMWARE_UPGRADE_ERROR: ResponseCode = ResponseCode {
80 code: 1205,
81 message: "Error with firmware upgrade - SHA1SUM does not match",
82};
83
84pub trait ResponseCodeTrait {
86 fn response_code(&self) -> ResponseCode;
90
91 fn map_response_code(code: u32) -> ResponseCode {
92 match code {
93 1000 => OK,
94 1001 => ERROR,
95 1101 => ERROR_INVALID_ARGUMENT,
96 1102 => ERROR2,
97 1103 => ERROR_VALUE_WRONG_MISSING_KEY,
98 1104 => ERROR_MALFORMED_JSON_INPUT,
99 1105 => ERROR_INVALID_ARGUMENT_KEY,
100 1107 => OK2,
101 1108 => OK3,
102 1205 => FIRMWARE_UPGRADE_ERROR,
103 _ => ERROR,
104 }
105 }
106}