use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct ResponseCode {
pub code: u32,
pub message: &'static str,
}
impl ResponseCode {
pub fn is_ok(&self) -> bool {
self.code == OK.code
}
pub fn is_error(&self) -> bool {
!self.is_ok()
}
}
pub const OK: ResponseCode = ResponseCode {
code: 1000,
message: "Ok",
};
pub const ERROR: ResponseCode = ResponseCode {
code: 1001,
message: "Error",
};
pub const ERROR_INVALID_ARGUMENT: ResponseCode = ResponseCode {
code: 1101,
message: "Invalid argument value",
};
pub const ERROR2: ResponseCode = ResponseCode {
code: 1102,
message: "Error",
};
pub const ERROR_VALUE_WRONG_MISSING_KEY: ResponseCode = ResponseCode {
code: 1103,
message: "Error - value too long? Or missing required object key?",
};
pub const ERROR_MALFORMED_JSON_INPUT: ResponseCode = ResponseCode {
code: 1104,
message: "Error - malformed JSON on input?",
};
pub const ERROR_INVALID_ARGUMENT_KEY: ResponseCode = ResponseCode {
code: 1105,
message: "Invalid argument key",
};
pub const OK2: ResponseCode = ResponseCode {
code: 1107,
message: "OK?",
};
pub const OK3: ResponseCode = ResponseCode {
code: 1108,
message: "OK?",
};
pub const FIRMWARE_UPGRADE_ERROR: ResponseCode = ResponseCode {
code: 1205,
message: "Error with firmware upgrade - SHA1SUM does not match",
};
pub trait ResponseCodeTrait {
fn response_code(&self) -> ResponseCode;
fn map_response_code(code: u32) -> ResponseCode {
match code {
1000 => OK,
1001 => ERROR,
1101 => ERROR_INVALID_ARGUMENT,
1102 => ERROR2,
1103 => ERROR_VALUE_WRONG_MISSING_KEY,
1104 => ERROR_MALFORMED_JSON_INPUT,
1105 => ERROR_INVALID_ARGUMENT_KEY,
1107 => OK2,
1108 => OK3,
1205 => FIRMWARE_UPGRADE_ERROR,
_ => ERROR,
}
}
}