glow_control_lib/util/
traits.rs

1use serde::{Deserialize, Serialize};
2
3/// The response code for a command.
4///
5/// The HTTP Status in a response may
6/// only tell if a command could be error free received, but not if it was in any way valid
7/// and could be processed.
8#[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    /// This is a code that means "Ok".
16    ///
17    /// Use this function instead of comparing the code to "1000",
18    /// it can be expanded if it becomes clearer (reverse-engineered) what other codes mean "Ok".
19    ///
20    /// That could be context-dependent.
21    pub fn is_ok(&self) -> bool {
22        self.code == OK.code
23    }
24
25    /// This is a code which means "Error".
26    pub fn is_error(&self) -> bool {
27        !self.is_ok()
28    }
29}
30
31// Errors codes from https://xled-docs.readthedocs.io/en/latest/rest_api.html#http-responses.
32
33/// The OK response code.
34pub const OK: ResponseCode = ResponseCode {
35    code: 1000,
36    message: "Ok",
37};
38/// An error response code.
39pub const ERROR: ResponseCode = ResponseCode {
40    code: 1001,
41    message: "Error",
42};
43/// An error response code.
44pub const ERROR_INVALID_ARGUMENT: ResponseCode = ResponseCode {
45    code: 1101,
46    message: "Invalid argument value",
47};
48/// An error response code.
49pub const ERROR2: ResponseCode = ResponseCode {
50    code: 1102,
51    message: "Error",
52};
53/// An error response code.
54pub const ERROR_VALUE_WRONG_MISSING_KEY: ResponseCode = ResponseCode {
55    code: 1103,
56    message: "Error - value too long? Or missing required object key?",
57};
58/// An error response code.
59pub const ERROR_MALFORMED_JSON_INPUT: ResponseCode = ResponseCode {
60    code: 1104,
61    message: "Error - malformed JSON on input?",
62};
63/// An error response code.
64pub const ERROR_INVALID_ARGUMENT_KEY: ResponseCode = ResponseCode {
65    code: 1105,
66    message: "Invalid argument key",
67};
68/// An OK response code?
69pub const OK2: ResponseCode = ResponseCode {
70    code: 1107,
71    message: "OK?",
72};
73/// An OK response code?
74pub const OK3: ResponseCode = ResponseCode {
75    code: 1108,
76    message: "OK?",
77};
78/// An error response code.
79pub const FIRMWARE_UPGRADE_ERROR: ResponseCode = ResponseCode {
80    code: 1205,
81    message: "Error with firmware upgrade - SHA1SUM does not match",
82};
83
84/// Trait for response codes.
85pub trait ResponseCodeTrait {
86    /// Get the response code.
87    /// # Returns
88    /// The response code.
89    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}