Skip to main content

live_stream/
builder.rs

1//! Builder for [`SubscriptionDescriptor`].
2
3use live_data::{
4    FilterExpr, FormatPreference, Sampling, SubscriptionDescriptor, TransportPreference,
5    TransportTag,
6};
7
8/// Builder for a [`SubscriptionDescriptor`].
9///
10/// Every method returns `Self` by value so chains compose without
11/// intermediate state. The default request matches "give me
12/// everything on this feed".
13#[derive(Debug, Clone)]
14pub struct SubscriptionBuilder {
15    inner: SubscriptionDescriptor,
16}
17
18impl SubscriptionBuilder {
19    pub fn new(feed: impl Into<String>) -> Self {
20        Self { inner: SubscriptionDescriptor::new(feed) }
21    }
22
23    /// Restrict to a named subset of columns. Pass an empty iterator
24    /// to clear a previously-set projection.
25    pub fn columns<I, S>(mut self, cols: I) -> Self
26    where
27        I: IntoIterator<Item = S>,
28        S: Into<String>,
29    {
30        self.inner.columns = Some(cols.into_iter().map(Into::into).collect());
31        self
32    }
33
34    pub fn filter(mut self, f: FilterExpr) -> Self {
35        self.inner.filter = f;
36        self
37    }
38
39    pub fn sampling(mut self, s: Sampling) -> Self {
40        self.inner.sampling = Some(s);
41        self
42    }
43
44    /// Replace the entire transport preference list.
45    pub fn transport_pref(mut self, p: TransportPreference) -> Self {
46        self.inner.transport_pref = p;
47        self
48    }
49
50    /// Append a single transport tag to the preference list. Builds an
51    /// ordered fallback chain when called multiple times.
52    pub fn prefer_transport(mut self, t: TransportTag) -> Self {
53        self.inner.transport_pref.0.push(t);
54        self
55    }
56
57    pub fn format(mut self, f: FormatPreference) -> Self {
58        self.inner.format_pref = Some(f);
59        self
60    }
61
62    pub fn build(self) -> SubscriptionDescriptor {
63        self.inner
64    }
65}
66
67impl From<SubscriptionBuilder> for SubscriptionDescriptor {
68    fn from(b: SubscriptionBuilder) -> Self {
69        b.build()
70    }
71}