live-stream 0.1.0

Consumer SDK for browsing and subscribing to live data feeds. Publishers use the live-feed crate.
Documentation
//! Builder for [`SubscriptionDescriptor`].

use live_data::{
    FilterExpr, FormatPreference, Sampling, SubscriptionDescriptor, TransportPreference,
    TransportTag,
};

/// Builder for a [`SubscriptionDescriptor`].
///
/// Every method returns `Self` by value so chains compose without
/// intermediate state. The default request matches "give me
/// everything on this feed".
#[derive(Debug, Clone)]
pub struct SubscriptionBuilder {
    inner: SubscriptionDescriptor,
}

impl SubscriptionBuilder {
    pub fn new(feed: impl Into<String>) -> Self {
        Self { inner: SubscriptionDescriptor::new(feed) }
    }

    /// Restrict to a named subset of columns. Pass an empty iterator
    /// to clear a previously-set projection.
    pub fn columns<I, S>(mut self, cols: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.inner.columns = Some(cols.into_iter().map(Into::into).collect());
        self
    }

    pub fn filter(mut self, f: FilterExpr) -> Self {
        self.inner.filter = f;
        self
    }

    pub fn sampling(mut self, s: Sampling) -> Self {
        self.inner.sampling = Some(s);
        self
    }

    /// Replace the entire transport preference list.
    pub fn transport_pref(mut self, p: TransportPreference) -> Self {
        self.inner.transport_pref = p;
        self
    }

    /// Append a single transport tag to the preference list. Builds an
    /// ordered fallback chain when called multiple times.
    pub fn prefer_transport(mut self, t: TransportTag) -> Self {
        self.inner.transport_pref.0.push(t);
        self
    }

    pub fn format(mut self, f: FormatPreference) -> Self {
        self.inner.format_pref = Some(f);
        self
    }

    pub fn build(self) -> SubscriptionDescriptor {
        self.inner
    }
}

impl From<SubscriptionBuilder> for SubscriptionDescriptor {
    fn from(b: SubscriptionBuilder) -> Self {
        b.build()
    }
}