use serde::{Deserialize, Serialize};
pub const METHOD_EVENTS_HEALTH: &str = "events.health";
pub const EVENTS_HEALTH_OK: &str = "ok";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
pub struct EventsHealthParams {}
impl<'de> Deserialize<'de> for EventsHealthParams {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
serde::de::IgnoredAny::deserialize(deserializer)?;
Ok(Self {})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EventsHealth {
pub status: String,
pub service: String,
pub version: String,
pub uptime_secs: u64,
pub ingested: u64,
pub deduped: u64,
pub evicted: u64,
pub log_dropped: u64,
pub subscribers: u64,
pub head_seq: u64,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn golden_reply() -> serde_json::Value {
json!({
"status": "ok",
"service": "trusty-events",
"version": "0.1.0",
"uptime_secs": 12,
"ingested": 3,
"deduped": 1,
"evicted": 0,
"log_dropped": 0,
"subscribers": 2,
"head_seq": 3,
})
}
#[test]
fn events_health_golden_reply_roundtrip() {
assert_eq!(METHOD_EVENTS_HEALTH, "events.health");
let reply = EventsHealth {
status: EVENTS_HEALTH_OK.into(),
service: super::super::EVENTS_SERVICE.into(),
version: "0.1.0".into(),
uptime_secs: 12,
ingested: 3,
deduped: 1,
evicted: 0,
log_dropped: 0,
subscribers: 2,
head_seq: 3,
};
assert_eq!(
serde_json::to_value(&reply).expect("encode"),
golden_reply()
);
let mut newer = golden_reply();
newer["added_later"] = json!(true);
let back: EventsHealth = serde_json::from_value(newer).expect("unknown field accepted");
assert_eq!(back, reply);
}
#[test]
fn events_health_params_accept_any_shape() {
for params in [json!(null), json!({}), json!({"stray": 1})] {
let decoded: EventsHealthParams =
serde_json::from_value(params.clone()).expect("any params decode");
assert_eq!(decoded, EventsHealthParams {}, "{params}");
}
}
}