diidi-travel-common-queue 0.1.16

A collection of common utilities and types for the DiiDi project.
Documentation
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueueConfig {
  pub provider: String,
  #[serde(default)]
  pub namespace: Option<String>,
  #[serde(default)]
  pub default_topic: Option<String>,
  #[serde(default)]
  pub params: serde_json::Value,
}

impl QueueConfig {
  pub fn new(provider: impl Into<String>) -> Self {
    Self { provider: provider.into(), namespace: None, default_topic: None, params: serde_json::Value::Null }
  }

  pub fn with_namespace(mut self, ns: impl Into<String>) -> Self {
    self.namespace = Some(ns.into());
    self
  }

  pub fn with_default_topic(mut self, topic: impl Into<String>) -> Self {
    self.default_topic = Some(topic.into());
    self
  }

  pub fn with_params(mut self, params: serde_json::Value) -> Self {
    self.params = params;
    self
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn parses_minimal_payload() {
    let raw = serde_json::json!({
      "provider": "kafka",
      "params": { "brokers": ["localhost:9092"] }
    });
    let cfg: QueueConfig = serde_json::from_value(raw).unwrap();
    assert_eq!(cfg.provider, "kafka");
    assert_eq!(cfg.params["brokers"][0], "localhost:9092");
    assert!(cfg.default_topic.is_none());
  }

  #[test]
  fn round_trip_namespace_and_topic() {
    let cfg = QueueConfig::new("rabbitmq").with_namespace("svc").with_default_topic("orders");
    let raw = serde_json::to_value(&cfg).unwrap();
    let back: QueueConfig = serde_json::from_value(raw).unwrap();
    assert_eq!(back.namespace.as_deref(), Some("svc"));
    assert_eq!(back.default_topic.as_deref(), Some("orders"));
  }
}