use serde::{Deserialize, Serialize};
#[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 {
#[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,
}
}
#[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
}
#[must_use]
pub const fn with_app_clip_card(mut self) -> Self {
self.clip_card = true;
self
}
#[must_use]
pub fn with_app_argument(mut self, app_argument: impl Into<String>) -> Self {
self.argument = Some(app_argument.into());
self
}
#[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"));
}
}
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);
}
}