live_data/
subscription.rs1use serde::{Deserialize, Serialize};
8
9use crate::endpoint::Endpoint;
10use crate::transport::{FormatPreference, TransportPreference};
11
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub struct SubscriptionDescriptor {
19 pub feed: String,
21 #[serde(default)]
23 pub columns: Option<Vec<String>>,
24 #[serde(default = "FilterExpr::empty")]
27 pub filter: FilterExpr,
28 #[serde(default)]
30 pub sampling: Option<Sampling>,
31 #[serde(default)]
33 pub transport_pref: TransportPreference,
34 #[serde(default)]
36 pub format_pref: Option<FormatPreference>,
37}
38
39impl SubscriptionDescriptor {
40 pub fn new(feed: impl Into<String>) -> Self {
41 Self {
42 feed: feed.into(),
43 columns: None,
44 filter: FilterExpr::empty(),
45 sampling: None,
46 transport_pref: TransportPreference::any(),
47 format_pref: None,
48 }
49 }
50
51 pub fn columns<I, S>(mut self, cols: I) -> Self
52 where
53 I: IntoIterator<Item = S>,
54 S: Into<String>,
55 {
56 self.columns = Some(cols.into_iter().map(Into::into).collect());
57 self
58 }
59
60 pub fn transport_pref(mut self, pref: TransportPreference) -> Self {
61 self.transport_pref = pref;
62 self
63 }
64
65 pub fn format(mut self, fmt: FormatPreference) -> Self {
66 self.format_pref = Some(fmt);
67 self
68 }
69
70 pub fn sampling(mut self, sampling: Sampling) -> Self {
71 self.sampling = Some(sampling);
72 self
73 }
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
78pub struct Sampling {
79 pub stride: u32,
81}
82
83impl Sampling {
84 pub fn every(stride: u32) -> Self {
85 Self { stride: stride.max(1) }
86 }
87}
88
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
95#[serde(tag = "kind", rename_all = "snake_case")]
96#[non_exhaustive]
97pub enum FilterExpr {
98 Empty,
100}
101
102impl FilterExpr {
103 pub fn empty() -> Self {
104 Self::Empty
105 }
106
107 pub fn is_empty(&self) -> bool {
108 matches!(self, Self::Empty)
109 }
110}
111
112impl Default for FilterExpr {
113 fn default() -> Self {
114 Self::Empty
115 }
116}
117
118#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
120#[serde(tag = "outcome", rename_all = "snake_case")]
121#[non_exhaustive]
122pub enum SubscribeResponse {
123 Ack(SubscribeAck),
124 Err(SubscribeError),
125}
126
127#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
130pub struct SubscribeAck {
131 pub endpoint: Endpoint,
134 pub format: FormatPreference,
136 #[serde(default)]
139 pub negotiated_columns: Option<Vec<String>>,
140}
141
142#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
144pub struct SubscribeError {
145 pub code: SubscribeErrorCode,
146 pub message: String,
147}
148
149#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
150#[serde(rename_all = "snake_case")]
151#[non_exhaustive]
152pub enum SubscribeErrorCode {
153 UnknownFeed,
155 UnsupportedTransport,
157 UnsupportedFormat,
159 UnsupportedCapability,
161 Internal,
163}