spoolframe-protocol 0.17.5

The spoolframe/client/1 wire protocol.
Documentation
//! Regular features: how a server knows a client handles something.
//!
//! A client names the features it handles in [`Hello::features`]; the
//! server's answer, [`ServerInfo::features`], names those of them it has.
//! A feature is on for the connection only when both named it. Then, and
//! only then, the server answers the feature's requests (a request of a
//! feature that is off is `Unsupported`, as one it does not know) and
//! sends its events. A name either side does not know is ignored, never
//! an error, and both fields are left out when empty, so an older peer
//! sends and reads what it did before.
//!
//! Every opt-in request and event comes with a feature. A new reply field
//! needs none: it is `serde(default)`, and an `Option` where its absence
//! means something else than its default. One is gated all the same, at
//! the owner's word: `spoolframe:rating-sources`'s, which a server leaves
//! out of its replies to a connection without the feature. Nothing is decided from a
//! version number. Unlike the experimental features
//! ([`crate::experimental`]), these are ordinary protocol, with its
//! stability, and a server that has one always names it when asked.
//!
//! The scheme is dated: a feature is what was added by its date, whatever
//! the mix, so a client names one thing for all of it. Later additions get
//! a new dated name; the one a release has shipped never changes what it
//! holds, and a name stays once added.
//!
//! [`Hello::features`]: crate::Hello::features
//! [`ServerInfo::features`]: crate::ServerInfo::features

use std::fmt;

use enumset::{EnumSet, EnumSetType};

/// A regular feature. On the wire it is its name ([`Feature::name`]).
#[derive(Debug, Hash, PartialOrd, Ord, EnumSetType)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[non_exhaustive]
pub enum Feature {
    /// `spoolframe:october-2026`: `ProfileRemoved`, `PinChanged` and
    /// `WatchChanged` on the events stream, `NextEpisode`, `ShowNext`, and
    /// `BeginInvitePasskey` and `RedeemInviteWithPasskey`, an account made
    /// with an invite and an app passkey.
    October2026,
    /// `spoolframe:download-addons`: `DownloadAddonOptions`,
    /// `ReadTrackSubtitle` and `PrepareAudioAddon`, a subtitle as WebVTT
    /// and an audio track as a file of its own, added to a download the
    /// device has without fetching the film again.
    ///
    /// Not dated, unlike the scheme above says: the user chose this name
    /// on purpose (2026-11-09), as a feature of its own that a client may
    /// name without the rest of a month's.
    DownloadAddons,
    /// `spoolframe:diagnostics`: `SendDiagnostics`, an app's playback log
    /// and what it runs on, kept on the server for its admins to read.
    ///
    /// Not dated, as `spoolframe:download-addons` is not: a feature of its
    /// own that an app names without the rest of a month's.
    Diagnostics,
    /// `spoolframe:phone-sign-in`: `OfferDeviceSignIn`, a sign-in a
    /// phone makes for a TV it reached directly
    /// ([`crate::phone_sign_in`]), which the TV redeems with
    /// `AwaitDeviceSignIn`.
    ///
    /// Not dated, as `spoolframe:diagnostics` is not: a feature of its
    /// own that an app names without the rest of a month's.
    PhoneSignIn,
    /// `spoolframe:recently-played-row`: `RecentlyPlayedRow`, Home's one
    /// "Recently played" row of every kind, music by where each play was
    /// started ([`crate::recently_played`]), and
    /// `RecentlyPlayedPreferences` and `SetRecentlyPlayedPreferences`,
    /// how each kind shows in it. Named for what it brings, as
    /// `spoolframe:diagnostics` is.
    RecentlyPlayedRow,
    /// `spoolframe:rating-sources`: whose each rating is
    /// (`MergeFacts.rating_source` and `audience_rating_source`): Rotten
    /// Tomatoes with its verdict, IMDb, TMDB. A reply field, not a
    /// request: a server leaves them out for a connection without it.
    RatingSources,
}

/// A set of features: [`Features::all`] is every one this build knows,
/// and `client & server` those on for a connection. Bit positions are
/// this build's own; only names cross the wire ([`names`]).
pub type Features = EnumSet<Feature>;

impl Feature {
    /// Its name on the wire: the one place the names are written.
    pub const fn name(self) -> &'static str {
        match self {
            Feature::October2026 => "spoolframe:october-2026",
            Feature::DownloadAddons => "spoolframe:download-addons",
            Feature::Diagnostics => "spoolframe:diagnostics",
            Feature::PhoneSignIn => "spoolframe:phone-sign-in",
            Feature::RecentlyPlayedRow => "spoolframe:recently-played-row",
            Feature::RatingSources => "spoolframe:rating-sources",
        }
    }

    /// The feature named `name`, if this build knows it.
    pub fn of(name: &str) -> Option<Feature> {
        Features::all().iter().find(|f| f.name() == name)
    }
}

impl fmt::Display for Feature {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// [`Features`] on the wire, for `#[serde(with = "names")]`: the list
/// of their names. A name this build does not know is skipped as it is
/// read. Never enumset's own serde, which would send its bits.
pub mod names {
    use std::fmt;

    use serde::de::{SeqAccess, Visitor};
    use serde::ser::SerializeSeq;
    use serde::{Deserializer, Serializer};

    use super::{Feature, Features};

    pub fn serialize<S: Serializer>(features: &Features, s: S) -> Result<S::Ok, S::Error> {
        let mut names = s.serialize_seq(Some(features.len()))?;
        for feature in features.iter() {
            names.serialize_element(feature.name())?;
        }
        names.end()
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Features, D::Error> {
        struct Names;

        impl<'de> Visitor<'de> for Names {
            type Value = Features;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("a list of feature names")
            }

            fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Features, A::Error> {
                let mut features = Features::empty();
                // Owned: CBOR text may come in chunks.
                while let Some(name) = seq.next_element::<String>()? {
                    if let Some(feature) = Feature::of(&name) {
                        features |= feature;
                    }
                }
                Ok(features)
            }
        }

        d.deserialize_seq(Names)
    }
}

#[cfg(test)]
mod tests {
    use serde::{Deserialize, Serialize};

    use super::*;

    /// A field as `Hello` and `ServerInfo` carry it.
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
    struct Named(#[serde(with = "names")] Features);

    #[test]
    fn every_feature_is_named_once_and_read_back() {
        let mut names: Vec<&str> = Features::all().iter().map(Feature::name).collect();
        for feature in Features::all() {
            assert_eq!(Feature::of(feature.name()), Some(feature));
            assert!(feature.name().starts_with("spoolframe:"), "{feature}");
        }
        names.sort_unstable();
        names.dedup();
        assert_eq!(names.len(), Features::all().len());
        assert_eq!(Feature::of("spoolframe:teleport"), None);
    }

    /// The download add-ons' name is fixed: an app names it as it is.
    #[test]
    fn download_addons_is_named_as_the_apps_name_it() {
        assert_eq!(
            Feature::of("spoolframe:download-addons"),
            Some(Feature::DownloadAddons)
        );
    }

    /// The diagnostics' name is fixed: an app names it as it is.
    #[test]
    fn diagnostics_is_named_as_the_apps_name_it() {
        assert_eq!(
            Feature::of("spoolframe:diagnostics"),
            Some(Feature::Diagnostics)
        );
    }

    /// The phone sign-in's name is fixed: an app names it as it is.
    #[test]
    fn phone_sign_in_is_named_as_the_apps_name_it() {
        assert_eq!(
            Feature::of("spoolframe:phone-sign-in"),
            Some(Feature::PhoneSignIn)
        );
    }

    /// What is on for a connection: those both sides named.
    #[test]
    fn on_is_what_both_named() {
        let both = Features::only(Feature::October2026);
        assert_eq!(both & both, both);
        assert!((both & Features::empty()).is_empty());
    }

    /// Only names cross the wire, in JSON and in CBOR.
    #[test]
    fn a_set_goes_as_the_list_of_its_names() {
        let set = Named(Feature::October2026.into());
        assert_eq!(
            serde_json::to_value(&set).unwrap(),
            serde_json::json!(["spoolframe:october-2026"])
        );
        let cbor = crate::codec::encode(&set).unwrap();
        let value: ciborium::Value = ciborium::from_reader(cbor.as_slice()).unwrap();
        assert_eq!(
            value,
            ciborium::Value::Array(vec!["spoolframe:october-2026".into()])
        );
        assert_eq!(crate::codec::decode::<Named>(&cbor).unwrap(), set);
    }

    /// A name this build does not know is skipped; the rest are read.
    #[test]
    fn an_unknown_name_is_skipped() {
        let names = vec!["spoolframe:teleport", "spoolframe:october-2026"];
        let read: Named = serde_json::from_value(serde_json::json!(names)).unwrap();
        assert_eq!(read, Named(Feature::October2026.into()));
        let cbor = crate::codec::encode(&names).unwrap();
        assert_eq!(
            crate::codec::decode::<Named>(&cbor).unwrap(),
            Named(Feature::October2026.into())
        );
    }
}