liveactivity 0.12.1

Framework-neutral Live Activities: one interface over ActivityKit, Android Live Updates and other backends
Documentation
use serde::{Deserialize, Serialize};

/// The result of every call of the contract.
pub type Result<T> = std::result::Result<T, Error>;

/// Every code of the contract, on every platform and backend.
pub const ERROR_CODES: &[&str] = &[
    "UNSUPPORTED_PLATFORM",
    "UNSUPPORTED_FEATURE",
    "PERMISSION_DENIED",
    "NOT_FOREGROUND",
    "CONFIGURATION_REQUIRED",
    "INVALID_INPUT",
    "PAYLOAD_TOO_LARGE",
    "NOT_FOUND",
    "STALE_REVISION",
    "CONFLICT",
    "SYSTEM_LIMIT",
    "NATIVE_ERROR",
];

/// A failure of the contract: one of [`ERROR_CODES`] and a message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
#[error("{code}: {message}")]
pub struct Error {
    /// One of [`ERROR_CODES`].
    pub code: String,
    /// What went wrong, for a developer.
    pub message: String,
}

impl Error {
    /// `INVALID_INPUT`: the request does not match the contract.
    pub fn invalid() -> Self {
        Self::new(
            "INVALID_INPUT",
            "Request does not match the live activity contract",
        )
    }

    /// `PAYLOAD_TOO_LARGE`: the layout, styles and values exceed the limit.
    pub fn payload_too_large() -> Self {
        Self::new(
            "PAYLOAD_TOO_LARGE",
            "Layout, styles and values exceed the live activity payload limit",
        )
    }

    /// `UNSUPPORTED_PLATFORM`: the platform has no Live Activities.
    pub fn unsupported() -> Self {
        Self::new(
            "UNSUPPORTED_PLATFORM",
            "This release supports native activities on iOS and Android only",
        )
    }

    /// A backend takes one event sink: a second connection would share, and so split, the platform's events.
    pub(crate) fn already_connected() -> Self {
        Self::new(
            "CONFLICT",
            "The backend is already connected to an event sink",
        )
    }

    /// The backend does not provide what the request asks for.
    pub fn unsupported_feature(message: &str) -> Self {
        Self::new("UNSUPPORTED_FEATURE", message)
    }

    /// `NATIVE_ERROR`: the platform or its bridge failed.
    pub fn native() -> Self {
        Self::new(
            "NATIVE_ERROR",
            "The native activity backend could not complete the request",
        )
    }

    /// An error of `code`, which is one of [`ERROR_CODES`].
    pub fn new(code: &str, message: &str) -> Self {
        debug_assert!(ERROR_CODES.contains(&code), "no error code: {code}");
        Self {
            code: code.into(),
            message: message.into(),
        }
    }
}

/// An error of activitykit keeps its code. The message is its text without the code: constant text, with the feature
/// and the iOS version of `UNSUPPORTED_FEATURE` and the scope of `SYSTEM_LIMIT`.
impl From<activitykit::Error> for Error {
    fn from(error: activitykit::Error) -> Self {
        let code = error.code();
        let text = error.to_string();
        let message = text
            .strip_prefix(code)
            .and_then(|rest| rest.strip_prefix(": "))
            .unwrap_or_else(|| error.message());
        Self::new(code, message)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use activitykit::{Error as Kit, LimitScope};
    #[test]
    fn activitykit_errors_keep_their_codes_and_their_details() {
        let cases = [
            (Kit::Unsupported, "UNSUPPORTED_PLATFORM"),
            (
                Kit::InvalidInput("Request does not match the live activity contract".into()),
                "INVALID_INPUT",
            ),
            (Kit::PayloadTooLarge("p".into()), "PAYLOAD_TOO_LARGE"),
            (Kit::NotFound("n".into()), "NOT_FOUND"),
            (Kit::Conflict("c".into()), "CONFLICT"),
            (Kit::StaleRevision("s".into()), "STALE_REVISION"),
            (Kit::PermissionDenied("d".into()), "PERMISSION_DENIED"),
            (Kit::NotForeground("f".into()), "NOT_FOREGROUND"),
            (
                Kit::ConfigurationRequired("r".into()),
                "CONFIGURATION_REQUIRED",
            ),
            (Kit::Native("x".into()), "NATIVE_ERROR"),
        ];
        for (error, code) in cases {
            let converted = Error::from(error.clone());
            assert_eq!(converted.code, code);
            assert_eq!(converted.message, error.message());
            assert_eq!(converted.to_string(), error.to_string());
        }
        let invalid = Kit::InvalidInput("Request does not match the live activity contract".into());
        assert_eq!(Error::from(invalid), Error::invalid());
        let feature = Error::from(Kit::unsupported_feature("transient", (18, 0)));
        assert_eq!(
            (feature.code.as_str(), feature.message.as_str()),
            ("UNSUPPORTED_FEATURE", "transient needs iOS 18.0")
        );
        let limit = Error::from(Kit::system_limit(LimitScope::App));
        assert_eq!(
            limit.to_string(),
            "SYSTEM_LIMIT: the app's limit of Live Activities is reached"
        );
        let device = Error::from(Kit::system_limit(LimitScope::Device));
        assert_eq!(
            device.message,
            "the device's limit of Live Activities is reached"
        );
    }
}