webserver_base/templates/
smart_app_banner.rs1use serde::{Deserialize, Serialize};
4
5#[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 #[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 #[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 #[must_use]
41 pub const fn with_app_clip_card(mut self) -> Self {
42 self.clip_card = true;
43 self
44 }
45
46 #[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 #[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 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}