Skip to main content

cufflink_types/manifest/
subscription.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct SubscriptionDefinition {
6    pub subject: String,
7    pub handler: String,
8    pub handler_type: HandlerType,
9    #[serde(default)]
10    pub config: HashMap<String, String>,
11    /// Explicit JetStream stream to host this subscription's consumer. When
12    /// `None`, the platform uses the tenant stream (`DW_{tenant}`).
13    #[serde(default, skip_serializing_if = "Option::is_none")]
14    pub stream: Option<String>,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(rename_all = "snake_case")]
19pub enum HandlerType {
20    DeleteCascade,
21    UpdateField,
22    Webhook,
23    Wasm,
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn test_subscription_stream_defaults_to_none_in_legacy_json() {
32        let json = r#"{"subject":"x","handler":"h","handler_type":"wasm"}"#;
33        let sub: SubscriptionDefinition = serde_json::from_str(json).unwrap();
34        assert!(sub.stream.is_none());
35    }
36
37    #[test]
38    fn test_subscription_stream_roundtrip_when_set() {
39        let sub = SubscriptionDefinition {
40            subject: "cufflink.tick.5min".into(),
41            handler: "handle_tick_resync".into(),
42            handler_type: HandlerType::Wasm,
43            config: HashMap::new(),
44            stream: Some("CUFFLINK_TICKS".into()),
45        };
46        let json = serde_json::to_string(&sub).unwrap();
47        assert!(json.contains("\"stream\":\"CUFFLINK_TICKS\""));
48        let parsed: SubscriptionDefinition = serde_json::from_str(&json).unwrap();
49        assert_eq!(parsed.stream.as_deref(), Some("CUFFLINK_TICKS"));
50    }
51
52    #[test]
53    fn test_subscription_stream_skipped_when_none() {
54        let sub = SubscriptionDefinition {
55            subject: "x".into(),
56            handler: "h".into(),
57            handler_type: HandlerType::Wasm,
58            config: HashMap::new(),
59            stream: None,
60        };
61        let json = serde_json::to_string(&sub).unwrap();
62        assert!(!json.contains("stream"));
63    }
64}