Skip to main content

webserver_base/templates/
smart_app_banner.rs

1//! The Safari Smart App Banner (`<meta name="apple-itunes-app">`).
2
3use serde::{Deserialize, Serialize};
4
5/// A site's iOS app, advertised by Safari as a banner or App Clip card.
6///
7/// Declared once on [`BaseTemplateData`](super::BaseTemplateData) and emitted on
8/// every page. Safari ignores the tag on other browsers and platforms.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub struct SmartAppBanner {
11    id: String,
12    clip_bundle_id: Option<String>,
13    #[serde(default)]
14    clip_card: bool,
15    argument: Option<String>,
16}
17
18impl SmartAppBanner {
19    /// A banner for the App Store app with this numeric id.
20    #[must_use]
21    pub fn new(app_id: impl Into<String>) -> Self {
22        Self {
23            id: app_id.into(),
24            clip_bundle_id: None,
25            clip_card: false,
26            argument: None,
27        }
28    }
29
30    /// Adds the App Clip's bundle id, so Safari shows the App Clip card.
31    #[must_use]
32    pub fn with_app_clip_bundle_id(mut self, bundle_id: impl Into<String>) -> Self {
33        self.clip_bundle_id = Some(bundle_id.into());
34        self
35    }
36
37    /// Adds `app-clip-display=card`, which iOS 15+ requires to show the App Clip card.
38    ///
39    /// Has no effect without [`with_app_clip_bundle_id`](Self::with_app_clip_bundle_id).
40    #[must_use]
41    pub const fn with_app_clip_card(mut self) -> Self {
42        self.clip_card = true;
43        self
44    }
45
46    /// Fixes `app-argument` on every page.
47    ///
48    /// Without this, each page passes its own canonical URL, so the app opens
49    /// where the reader was; a fixed value is rarely what you want.
50    #[must_use]
51    pub fn with_app_argument(mut self, app_argument: impl Into<String>) -> Self {
52        self.argument = Some(app_argument.into());
53        self
54    }
55
56    /// The `content` of the meta tag for the page at `canonical_url`.
57    #[must_use]
58    pub fn content(&self, canonical_url: &str) -> String {
59        let mut parts: Vec<String> = vec![format!("app-id={}", self.id)];
60        if let Some(bundle_id) = &self.clip_bundle_id {
61            parts.push(format!("app-clip-bundle-id={bundle_id}"));
62            if self.clip_card {
63                parts.push(String::from("app-clip-display=card"));
64            }
65        }
66        // A comma would end the `app-argument` value early in the comma-separated content.
67        let argument: String = self
68            .argument
69            .as_deref()
70            .unwrap_or(canonical_url)
71            .replace(',', "%2C");
72        parts.push(format!("app-argument={argument}"));
73        parts.join(", ")
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::SmartAppBanner;
80
81    #[test]
82    fn the_app_argument_defaults_to_the_page_url() {
83        let expected: String =
84            String::from("app-id=123, app-argument=https://www.example.com/play");
85        let actual: String = SmartAppBanner::new("123").content("https://www.example.com/play");
86        assert_eq!(expected, actual);
87    }
88
89    #[test]
90    fn the_app_clip_bundle_id_sits_between_the_id_and_the_argument() {
91        let expected: String = String::from(
92            "app-id=123, app-clip-bundle-id=com.example.app.Clip, app-argument=https://www.example.com/",
93        );
94        let actual: String = SmartAppBanner::new("123")
95            .with_app_clip_bundle_id("com.example.app.Clip")
96            .content("https://www.example.com/");
97        assert_eq!(expected, actual);
98    }
99
100    #[test]
101    fn a_fixed_app_argument_beats_the_page_url() {
102        let expected: String = String::from("app-id=123, app-argument=example://home");
103        let actual: String = SmartAppBanner::new("123")
104            .with_app_argument("example://home")
105            .content("https://www.example.com/play");
106        assert_eq!(expected, actual);
107    }
108
109    #[test]
110    fn the_app_clip_card_follows_the_bundle_id() {
111        let expected: String = String::from(
112            "app-id=123, app-clip-bundle-id=com.example.app.Clip, app-clip-display=card, app-argument=https://www.example.com/",
113        );
114        let actual: String = SmartAppBanner::new("123")
115            .with_app_clip_bundle_id("com.example.app.Clip")
116            .with_app_clip_card()
117            .content("https://www.example.com/");
118        assert_eq!(expected, actual);
119    }
120
121    #[test]
122    fn the_app_clip_card_needs_a_bundle_id() {
123        let expected: String = String::from("app-id=123, app-argument=https://www.example.com/");
124        let actual: String = SmartAppBanner::new("123")
125            .with_app_clip_card()
126            .content("https://www.example.com/");
127        assert_eq!(expected, actual);
128    }
129
130    #[test]
131    fn commas_in_the_app_argument_are_percent_encoded() {
132        let expected: String = String::from("app-id=123, app-argument=https://x.example/a%2Cb");
133        let from_page: String = SmartAppBanner::new("123").content("https://x.example/a,b");
134        assert_eq!(expected, from_page);
135        let fixed: String = SmartAppBanner::new("123")
136            .with_app_argument("https://x.example/a,b")
137            .content("https://x.example/");
138        assert_eq!(expected, fixed);
139    }
140}