use crate::std_shim::{String, Vec};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Serialization {
#[default]
Json,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "method")]
pub enum MqttAuthentication {
#[serde(rename = "ANONYMOUS")]
Anonymous,
#[serde(rename = "OAUTH2_BEARER_TOKEN")]
Oauth2BearerToken {
username: String,
},
#[serde(rename = "CERTIFICATE", rename_all = "camelCase")]
Certificate {
ca_cert: String,
client_cert: String,
client_key: String,
},
}
impl MqttAuthentication {
pub const CLIENT_ID_PLACEHOLDER: &'static str = "{clientID}";
pub fn resolve_username<'a>(&'a self, client_id: Option<&'a str>) -> Option<&'a str> {
match self {
MqttAuthentication::Oauth2BearerToken { username } => {
if username == Self::CLIENT_ID_PLACEHOLDER {
client_id
} else {
Some(username)
}
}
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MqttNotifierBinding {
#[serde(rename = "URIS")]
pub uris: Vec<String>,
pub serialization: Serialization,
pub authentication: MqttAuthentication,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NotifiersResponse {
#[serde(rename = "WEBHOOK")]
pub webhook: bool,
#[serde(rename = "MQTT", default, skip_serializing_if = "Option::is_none")]
pub mqtt: Option<MqttNotifierBinding>,
}
impl Default for NotifiersResponse {
fn default() -> Self {
Self {
webhook: true,
mqtt: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "UPPERCASE")]
pub struct NotifierTopics {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub create: Option<String>,
pub update: String,
pub delete: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub all: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TopicsResponse {
pub topics: NotifierTopics,
}
impl TopicsResponse {
pub fn under(prefix: &str) -> Self {
Self {
topics: NotifierTopics {
create: Some(crate::std_shim::format!("{prefix}/create")),
update: crate::std_shim::format!("{prefix}/update"),
delete: crate::std_shim::format!("{prefix}/delete"),
all: Some(crate::std_shim::format!("{prefix}/+")),
},
}
}
pub fn under_without_create(prefix: &str) -> Self {
let mut r = Self::under(prefix);
r.topics.create = None;
r
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn notifiers_response_uses_uppercase_binding_keys() {
let r = NotifiersResponse {
webhook: true,
mqtt: Some(MqttNotifierBinding {
uris: crate::std_shim::vec!["mqtts://broker.example.com".into()],
serialization: Serialization::Json,
authentication: MqttAuthentication::Anonymous,
}),
};
let v: serde_json::Value = serde_json::to_value(&r).unwrap();
assert_eq!(v["WEBHOOK"], true);
assert_eq!(v["MQTT"]["URIS"][0], "mqtts://broker.example.com");
assert_eq!(v["MQTT"]["authentication"]["method"], "ANONYMOUS");
assert_eq!(v["MQTT"]["serialization"], "JSON");
}
#[test]
fn client_id_placeholder_resolves() {
let a = MqttAuthentication::Oauth2BearerToken {
username: "{clientID}".into(),
};
assert_eq!(a.resolve_username(Some("ven-7")), Some("ven-7"));
assert_eq!(
a.resolve_username(None),
None,
"a placeholder with no clientID resolves to nothing"
);
let b = MqttAuthentication::Oauth2BearerToken {
username: "oauth2".into(),
};
assert_eq!(b.resolve_username(Some("ven-7")), Some("oauth2"));
assert_eq!(
b.resolve_username(None),
Some("oauth2"),
"a fixed username needs no clientID"
);
}
#[test]
fn topic_sets_match_the_documented_layout() {
let t = TopicsResponse::under("events/programs/44");
assert_eq!(
t.topics.create.as_deref(),
Some("events/programs/44/create")
);
assert_eq!(t.topics.all.as_deref(), Some("events/programs/44/+"));
let scoped = TopicsResponse::under_without_create("programs/44");
assert!(scoped.topics.create.is_none());
assert_eq!(scoped.topics.update, "programs/44/update");
}
#[test]
fn topics_serialise_with_uppercase_operation_keys() {
let v = serde_json::to_value(TopicsResponse::under("programs")).unwrap();
assert_eq!(v["topics"]["CREATE"], "programs/create");
assert_eq!(v["topics"]["ALL"], "programs/+");
}
}