1use serde_json::{Value, json};
2#[cfg(all(feature = "deep-links", target_os = "android"))]
7#[manganis::ffi("src/android/deep_links")]
8unsafe extern "Kotlin" {
9 pub type DeepLinksPlugin;
10}
11#[cfg(all(feature = "deep-links", target_os = "android"))]
12const DEEP_LINKS_CLASS: &str = "dev.dioxus.g3_native_plugins.deep_links.DeepLinksPlugin";
13#[cfg(all(feature = "deep-links", target_os = "android"))]
14type DeepLinksHandle = crate::android_bridge::AndroidPlugin;
15#[cfg(all(feature = "deep-links", target_os = "ios"))]
16type DeepLinksHandle = DeepLinksPlugin;
17#[cfg(all(feature = "deep-links", target_os = "ios"))]
18#[allow(missing_docs)]
19#[manganis::ffi("src/ios")]
20unsafe extern "Swift" {
21 pub type DeepLinksPlugin;
23 pub fn prepareFromRust(this: &DeepLinksPlugin);
25 pub fn takeLinkFromRust(this: &DeepLinksPlugin) -> Option<String>;
27}
28#[cfg(all(feature = "deep-links", any(target_os = "android", target_os = "ios")))]
68pub struct DeepLinks {
69 plugin: Option<DeepLinksHandle>,
70}
71#[cfg(all(
73 feature = "deep-links",
74 any(target_arch = "wasm32", target_os = "macos")
75))]
76pub struct DeepLinks;
77#[cfg(all(feature = "deep-links", any(target_os = "android", target_os = "ios")))]
78impl DeepLinks {
79 pub fn new() -> Self {
86 Self { plugin: None }
87 }
88 fn get_plugin(&mut self) -> Result<&DeepLinksHandle, String> {
89 if self.plugin.is_none() {
90 #[cfg(target_os = "android")]
91 let created = DeepLinksHandle::new(DEEP_LINKS_CLASS)?;
92 #[cfg(target_os = "ios")]
93 let created = DeepLinksPlugin::new()
94 .map_err(|error| format!("Failed to create DeepLinksPlugin: {error:?}"))?;
95 self.plugin = Some(created);
96 }
97 Ok(self.plugin.as_ref().unwrap())
98 }
99 pub fn prepare(&mut self) -> Result<(), String> {
107 let plugin = self.get_plugin()?;
108 #[cfg(target_os = "android")]
109 plugin.call_unit("prepareFromRust")?;
110 #[cfg(target_os = "ios")]
111 prepareFromRust(plugin)?;
112 Ok(())
113 }
114 pub fn take_link(&mut self) -> Result<Option<String>, String> {
120 let plugin = self.get_plugin()?;
121 #[cfg(target_os = "android")]
122 return plugin.call_string("takeLinkFromRust");
123 #[cfg(target_os = "ios")]
124 return Ok(takeLinkFromRust(plugin)?);
125 }
126}
127#[cfg(all(
128 feature = "deep-links",
129 any(target_arch = "wasm32", target_os = "macos")
130))]
131impl DeepLinks {
132 pub fn new() -> Self {
134 Self
135 }
136 pub fn prepare(&mut self) -> Result<(), String> {
138 Ok(())
139 }
140 pub fn take_link(&mut self) -> Result<Option<String>, String> {
142 Ok(None)
143 }
144}
145#[cfg(all(
146 feature = "deep-links",
147 any(
148 target_arch = "wasm32",
149 target_os = "android",
150 target_os = "ios",
151 target_os = "macos"
152 )
153))]
154impl Default for DeepLinks {
155 fn default() -> Self {
156 Self::new()
157 }
158}
159#[derive(Debug, Clone, PartialEq, Eq)]
163pub struct AppleAppSiteAssociation {
164 pub team_id: String,
166 pub bundle_id: String,
168 pub paths: Vec<String>,
171}
172impl AppleAppSiteAssociation {
173 pub fn new(
175 team_id: impl Into<String>,
176 bundle_id: impl Into<String>,
177 paths: impl IntoIterator<Item = impl Into<String>>,
178 ) -> Self {
179 Self {
180 team_id: team_id.into(),
181 bundle_id: bundle_id.into(),
182 paths: paths.into_iter().map(Into::into).collect(),
183 }
184 }
185 pub fn app_id(&self) -> String {
187 format!("{}.{}", self.team_id, self.bundle_id)
188 }
189 pub fn to_json(&self) -> Value {
191 json!(
192 { "applinks" : { "apps" : [], "details" : [{ "appID" : self.app_id(), "paths"
193 : self.paths }] } }
194 )
195 }
196}
197#[derive(Debug, Clone, PartialEq, Eq)]
201pub struct AndroidAssetLinks {
202 pub package_name: String,
204 pub sha256_cert_fingerprints: Vec<String>,
208}
209impl AndroidAssetLinks {
210 pub fn new(
212 package_name: impl Into<String>,
213 sha256_cert_fingerprints: impl IntoIterator<Item = impl Into<String>>,
214 ) -> Self {
215 Self {
216 package_name: package_name.into(),
217 sha256_cert_fingerprints: sha256_cert_fingerprints
218 .into_iter()
219 .map(Into::into)
220 .collect(),
221 }
222 }
223 pub fn to_json(&self) -> Value {
225 json!(
226 [{ "relation" : ["delegate_permission/common.handle_all_urls"], "target" : {
227 "namespace" : "android_app", "package_name" : self.package_name,
228 "sha256_cert_fingerprints" : self.sha256_cert_fingerprints } }]
229 )
230 }
231}
232pub fn apple_app_site_association(
235 team_id: impl Into<String>,
236 bundle_id: impl Into<String>,
237 paths: impl IntoIterator<Item = impl Into<String>>,
238) -> Value {
239 AppleAppSiteAssociation::new(team_id, bundle_id, paths).to_json()
240}
241pub fn android_asset_links(
244 package_name: impl Into<String>,
245 sha256_cert_fingerprints: impl IntoIterator<Item = impl Into<String>>,
246) -> Value {
247 AndroidAssetLinks::new(package_name, sha256_cert_fingerprints).to_json()
248}
249#[macro_export]
255macro_rules! ios_app_site_association_route {
256 (
257 team_id : $team_id:expr, bundle_id : $bundle_id:expr, paths : [$($path:expr),*
258 $(,)?] $(,)?
259 ) => {
260 #[dioxus::prelude::get("/.well-known/apple-app-site-association")] async fn
261 get_apple_app_site_association() -> dioxus::prelude::Result <
262 dioxus_fullstack::Json < serde_json::Value >, dioxus::prelude::StatusCode > {
263 Ok(dioxus_fullstack::Json($crate::deep_links::apple_app_site_association($team_id,
264 $bundle_id, [$($path),*]))) }
265 };
266}
267#[macro_export]
272macro_rules! android_asset_links_route {
273 (
274 package_name : $package_name:expr, sha256_cert_fingerprints :
275 [$($fingerprint:expr),* $(,)?] $(,)?
276 ) => {
277 #[dioxus::prelude::get("/.well-known/assetlinks.json")] async fn
278 get_android_asset_links() -> dioxus::prelude::Result < dioxus_fullstack::Json <
279 serde_json::Value >, dioxus::prelude::StatusCode > {
280 Ok(dioxus_fullstack::Json($crate::deep_links::android_asset_links($package_name,
281 [$($fingerprint),*]))) }
282 };
283}
284#[cfg(test)]
285mod tests {
286 use super::*;
287 fn production_source() -> &'static str {
288 include_str!("deep_links.rs")
289 .split("#[cfg(test)]")
290 .next()
291 .expect("source should split before tests")
292 }
293 #[test]
294 fn generates_apple_app_site_association() {
295 let association = AppleAppSiteAssociation::new(
296 "TEAM123456",
297 "com.example.App",
298 ["/account", "/games/*/join"],
299 );
300 assert_eq!(association.app_id(), "TEAM123456.com.example.App");
301 assert_eq!(
302 association.to_json(),
303 json!(
304 { "applinks" : { "apps" : [], "details" : [{ "appID" :
305 "TEAM123456.com.example.App", "paths" : ["/account", "/games/*/join"] }]
306 } }
307 ),
308 );
309 }
310 #[test]
311 fn generates_android_asset_links() {
312 assert_eq!(
313 android_asset_links("com.example.App", ["AA:BB", "CC:DD"]),
314 json!(
315 [{ "relation" : ["delegate_permission/common.handle_all_urls"], "target"
316 : { "namespace" : "android_app", "package_name" : "com.example.App",
317 "sha256_cert_fingerprints" : ["AA:BB", "CC:DD"] } }]
318 ),
319 );
320 }
321 #[test]
322 fn exports_ios_and_android_route_macros() {
323 let source = production_source();
324 assert!(source.contains("macro_rules! ios_app_site_association_route"));
325 assert!(source.contains("macro_rules! android_asset_links_route"));
326 assert!(
327 source
328 .contains("#[dioxus::prelude::get(\"/.well-known/apple-app-site-association\")]",),
329 );
330 assert!(source.contains("#[dioxus::prelude::get(\"/.well-known/assetlinks.json\")]"),);
331 assert!(source.contains("dioxus_fullstack::Json"));
332 }
333}