use serde_json::{Value, json};
#[cfg(all(feature = "deep-links", target_os = "android"))]
#[manganis::ffi("src/android/deep_links")]
unsafe extern "Kotlin" {
pub type DeepLinksPlugin;
}
#[cfg(all(feature = "deep-links", target_os = "android"))]
const DEEP_LINKS_CLASS: &str = "dev.dioxus.g3_native_plugins.deep_links.DeepLinksPlugin";
#[cfg(all(feature = "deep-links", target_os = "android"))]
type DeepLinksHandle = crate::android_bridge::AndroidPlugin;
#[cfg(all(feature = "deep-links", target_os = "ios"))]
type DeepLinksHandle = DeepLinksPlugin;
#[cfg(all(feature = "deep-links", target_os = "ios"))]
#[allow(missing_docs)]
#[manganis::ffi("src/ios")]
unsafe extern "Swift" {
pub type DeepLinksPlugin;
pub fn prepareFromRust(this: &DeepLinksPlugin);
pub fn takeLinkFromRust(this: &DeepLinksPlugin) -> Option<String>;
}
#[cfg(all(feature = "deep-links", any(target_os = "android", target_os = "ios")))]
pub struct DeepLinks {
plugin: Option<DeepLinksHandle>,
}
#[cfg(all(
feature = "deep-links",
any(target_arch = "wasm32", target_os = "macos")
))]
pub struct DeepLinks;
#[cfg(all(feature = "deep-links", any(target_os = "android", target_os = "ios")))]
impl DeepLinks {
pub fn new() -> Self {
Self { plugin: None }
}
fn get_plugin(&mut self) -> Result<&DeepLinksHandle, String> {
if self.plugin.is_none() {
#[cfg(target_os = "android")]
let created = DeepLinksHandle::new(DEEP_LINKS_CLASS)?;
#[cfg(target_os = "ios")]
let created = DeepLinksPlugin::new()
.map_err(|error| format!("Failed to create DeepLinksPlugin: {error:?}"))?;
self.plugin = Some(created);
}
Ok(self.plugin.as_ref().unwrap())
}
pub fn prepare(&mut self) -> Result<(), String> {
let plugin = self.get_plugin()?;
#[cfg(target_os = "android")]
plugin.call_unit("prepareFromRust")?;
#[cfg(target_os = "ios")]
prepareFromRust(plugin)?;
Ok(())
}
pub fn take_link(&mut self) -> Result<Option<String>, String> {
let plugin = self.get_plugin()?;
#[cfg(target_os = "android")]
return plugin.call_string("takeLinkFromRust");
#[cfg(target_os = "ios")]
return Ok(takeLinkFromRust(plugin)?);
}
}
#[cfg(all(
feature = "deep-links",
any(target_arch = "wasm32", target_os = "macos")
))]
impl DeepLinks {
pub fn new() -> Self {
Self
}
pub fn prepare(&mut self) -> Result<(), String> {
Ok(())
}
pub fn take_link(&mut self) -> Result<Option<String>, String> {
Ok(None)
}
}
#[cfg(all(
feature = "deep-links",
any(
target_arch = "wasm32",
target_os = "android",
target_os = "ios",
target_os = "macos"
)
))]
impl Default for DeepLinks {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppleAppSiteAssociation {
pub team_id: String,
pub bundle_id: String,
pub paths: Vec<String>,
}
impl AppleAppSiteAssociation {
pub fn new(
team_id: impl Into<String>,
bundle_id: impl Into<String>,
paths: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
Self {
team_id: team_id.into(),
bundle_id: bundle_id.into(),
paths: paths.into_iter().map(Into::into).collect(),
}
}
pub fn app_id(&self) -> String {
format!("{}.{}", self.team_id, self.bundle_id)
}
pub fn to_json(&self) -> Value {
json!(
{ "applinks" : { "apps" : [], "details" : [{ "appID" : self.app_id(), "paths"
: self.paths }] } }
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AndroidAssetLinks {
pub package_name: String,
pub sha256_cert_fingerprints: Vec<String>,
}
impl AndroidAssetLinks {
pub fn new(
package_name: impl Into<String>,
sha256_cert_fingerprints: impl IntoIterator<Item = impl Into<String>>,
) -> Self {
Self {
package_name: package_name.into(),
sha256_cert_fingerprints: sha256_cert_fingerprints
.into_iter()
.map(Into::into)
.collect(),
}
}
pub fn to_json(&self) -> Value {
json!(
[{ "relation" : ["delegate_permission/common.handle_all_urls"], "target" : {
"namespace" : "android_app", "package_name" : self.package_name,
"sha256_cert_fingerprints" : self.sha256_cert_fingerprints } }]
)
}
}
pub fn apple_app_site_association(
team_id: impl Into<String>,
bundle_id: impl Into<String>,
paths: impl IntoIterator<Item = impl Into<String>>,
) -> Value {
AppleAppSiteAssociation::new(team_id, bundle_id, paths).to_json()
}
pub fn android_asset_links(
package_name: impl Into<String>,
sha256_cert_fingerprints: impl IntoIterator<Item = impl Into<String>>,
) -> Value {
AndroidAssetLinks::new(package_name, sha256_cert_fingerprints).to_json()
}
#[macro_export]
macro_rules! ios_app_site_association_route {
(
team_id : $team_id:expr, bundle_id : $bundle_id:expr, paths : [$($path:expr),*
$(,)?] $(,)?
) => {
#[dioxus::prelude::get("/.well-known/apple-app-site-association")] async fn
get_apple_app_site_association() -> dioxus::prelude::Result <
dioxus_fullstack::Json < serde_json::Value >, dioxus::prelude::StatusCode > {
Ok(dioxus_fullstack::Json($crate::deep_links::apple_app_site_association($team_id,
$bundle_id, [$($path),*]))) }
};
}
#[macro_export]
macro_rules! android_asset_links_route {
(
package_name : $package_name:expr, sha256_cert_fingerprints :
[$($fingerprint:expr),* $(,)?] $(,)?
) => {
#[dioxus::prelude::get("/.well-known/assetlinks.json")] async fn
get_android_asset_links() -> dioxus::prelude::Result < dioxus_fullstack::Json <
serde_json::Value >, dioxus::prelude::StatusCode > {
Ok(dioxus_fullstack::Json($crate::deep_links::android_asset_links($package_name,
[$($fingerprint),*]))) }
};
}
#[cfg(test)]
mod tests {
use super::*;
fn production_source() -> &'static str {
include_str!("deep_links.rs")
.split("#[cfg(test)]")
.next()
.expect("source should split before tests")
}
#[test]
fn generates_apple_app_site_association() {
let association = AppleAppSiteAssociation::new(
"TEAM123456",
"com.example.App",
["/account", "/games/*/join"],
);
assert_eq!(association.app_id(), "TEAM123456.com.example.App");
assert_eq!(
association.to_json(),
json!(
{ "applinks" : { "apps" : [], "details" : [{ "appID" :
"TEAM123456.com.example.App", "paths" : ["/account", "/games/*/join"] }]
} }
),
);
}
#[test]
fn generates_android_asset_links() {
assert_eq!(
android_asset_links("com.example.App", ["AA:BB", "CC:DD"]),
json!(
[{ "relation" : ["delegate_permission/common.handle_all_urls"], "target"
: { "namespace" : "android_app", "package_name" : "com.example.App",
"sha256_cert_fingerprints" : ["AA:BB", "CC:DD"] } }]
),
);
}
#[test]
fn exports_ios_and_android_route_macros() {
let source = production_source();
assert!(source.contains("macro_rules! ios_app_site_association_route"));
assert!(source.contains("macro_rules! android_asset_links_route"));
assert!(
source
.contains("#[dioxus::prelude::get(\"/.well-known/apple-app-site-association\")]",),
);
assert!(source.contains("#[dioxus::prelude::get(\"/.well-known/assetlinks.json\")]"),);
assert!(source.contains("dioxus_fullstack::Json"));
}
}