webserver-base 0.5.2

A Rust library which contains shared logic for all of my webserver projects.
Documentation
//! The Safari Smart App Banner (`<meta name="apple-itunes-app">`).

use serde::{Deserialize, Serialize};

/// A site's iOS app, advertised by Safari as a banner or App Clip card.
///
/// Declared once on [`BaseTemplateData`](super::BaseTemplateData) and emitted on
/// every page. Safari ignores the tag on other browsers and platforms.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SmartAppBanner {
    id: String,
    clip_bundle_id: Option<String>,
    #[serde(default)]
    clip_card: bool,
    argument: Option<String>,
}

impl SmartAppBanner {
    /// A banner for the App Store app with this numeric id.
    #[must_use]
    pub fn new(app_id: impl Into<String>) -> Self {
        Self {
            id: app_id.into(),
            clip_bundle_id: None,
            clip_card: false,
            argument: None,
        }
    }

    /// Adds the App Clip's bundle id, so Safari shows the App Clip card.
    #[must_use]
    pub fn with_app_clip_bundle_id(mut self, bundle_id: impl Into<String>) -> Self {
        self.clip_bundle_id = Some(bundle_id.into());
        self
    }

    /// Adds `app-clip-display=card`, which iOS 15+ requires to show the App Clip card.
    ///
    /// Has no effect without [`with_app_clip_bundle_id`](Self::with_app_clip_bundle_id).
    #[must_use]
    pub const fn with_app_clip_card(mut self) -> Self {
        self.clip_card = true;
        self
    }

    /// Fixes `app-argument` on every page.
    ///
    /// Without this, each page passes its own canonical URL, so the app opens
    /// where the reader was; a fixed value is rarely what you want.
    #[must_use]
    pub fn with_app_argument(mut self, app_argument: impl Into<String>) -> Self {
        self.argument = Some(app_argument.into());
        self
    }

    /// The `content` of the meta tag for the page at `canonical_url`.
    #[must_use]
    pub fn content(&self, canonical_url: &str) -> String {
        let mut parts: Vec<String> = vec![format!("app-id={}", self.id)];
        if let Some(bundle_id) = &self.clip_bundle_id {
            parts.push(format!("app-clip-bundle-id={bundle_id}"));
            if self.clip_card {
                parts.push(String::from("app-clip-display=card"));
            }
        }
        // A comma would end the `app-argument` value early in the comma-separated content.
        let argument: String = self
            .argument
            .as_deref()
            .unwrap_or(canonical_url)
            .replace(',', "%2C");
        parts.push(format!("app-argument={argument}"));
        parts.join(", ")
    }
}

#[cfg(test)]
mod tests {
    use super::SmartAppBanner;

    #[test]
    fn the_app_argument_defaults_to_the_page_url() {
        let expected: String =
            String::from("app-id=123, app-argument=https://www.example.com/play");
        let actual: String = SmartAppBanner::new("123").content("https://www.example.com/play");
        assert_eq!(expected, actual);
    }

    #[test]
    fn the_app_clip_bundle_id_sits_between_the_id_and_the_argument() {
        let expected: String = String::from(
            "app-id=123, app-clip-bundle-id=com.example.app.Clip, app-argument=https://www.example.com/",
        );
        let actual: String = SmartAppBanner::new("123")
            .with_app_clip_bundle_id("com.example.app.Clip")
            .content("https://www.example.com/");
        assert_eq!(expected, actual);
    }

    #[test]
    fn a_fixed_app_argument_beats_the_page_url() {
        let expected: String = String::from("app-id=123, app-argument=example://home");
        let actual: String = SmartAppBanner::new("123")
            .with_app_argument("example://home")
            .content("https://www.example.com/play");
        assert_eq!(expected, actual);
    }

    #[test]
    fn the_app_clip_card_follows_the_bundle_id() {
        let expected: String = String::from(
            "app-id=123, app-clip-bundle-id=com.example.app.Clip, app-clip-display=card, app-argument=https://www.example.com/",
        );
        let actual: String = SmartAppBanner::new("123")
            .with_app_clip_bundle_id("com.example.app.Clip")
            .with_app_clip_card()
            .content("https://www.example.com/");
        assert_eq!(expected, actual);
    }

    #[test]
    fn the_app_clip_card_needs_a_bundle_id() {
        let expected: String = String::from("app-id=123, app-argument=https://www.example.com/");
        let actual: String = SmartAppBanner::new("123")
            .with_app_clip_card()
            .content("https://www.example.com/");
        assert_eq!(expected, actual);
    }

    #[test]
    fn commas_in_the_app_argument_are_percent_encoded() {
        let expected: String = String::from("app-id=123, app-argument=https://x.example/a%2Cb");
        let from_page: String = SmartAppBanner::new("123").content("https://x.example/a,b");
        assert_eq!(expected, from_page);
        let fixed: String = SmartAppBanner::new("123")
            .with_app_argument("https://x.example/a,b")
            .content("https://x.example/");
        assert_eq!(expected, fixed);
    }
}