use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Clone, Default, Serialize, Deserialize, Hash, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SimpleSubscriptionData {
topic: String,
expression_type: String,
expression: String,
version: u64,
}
impl SimpleSubscriptionData {
pub fn new(topic: String, expression_type: String, expression: String, version: u64) -> Self {
SimpleSubscriptionData {
topic,
expression_type,
expression,
version,
}
}
pub fn topic(&self) -> &str {
&self.topic
}
pub fn expression_type(&self) -> &str {
&self.expression_type
}
pub fn expression(&self) -> &str {
&self.expression
}
pub fn version(&self) -> u64 {
self.version
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serde_preserves_subscription_data() {
let data = SimpleSubscriptionData::new("topic".to_string(), "TAG".to_string(), "*".to_string(), 123);
assert_eq!(
(data.topic(), data.expression_type(), data.expression(), data.version()),
("topic", "TAG", "*", 123)
);
let json = serde_json::to_string(&data).unwrap();
assert_eq!(
json,
r#"{"topic":"topic","expressionType":"TAG","expression":"*","version":123}"#
);
assert_eq!(serde_json::from_str::<SimpleSubscriptionData>(&json).unwrap(), data);
}
}