tauri_plugin_opener/
open.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5//! Types and functions related to shell.
6
7use std::{ffi::OsStr, path::Path};
8
9pub(crate) fn open<P: AsRef<OsStr>, S: AsRef<str>>(path: P, with: Option<S>) -> crate::Result<()> {
10    match with {
11        Some(program) => ::open::with_detached(path, program.as_ref()),
12        None => ::open::that_detached(path),
13    }
14    .map_err(Into::into)
15}
16
17/// Opens URL with the program specified in `with`, or system default if `None`.
18///
19/// ## Platform-specific:
20///
21/// - **Android / iOS**: Always opens using default program.
22///
23/// # Examples
24///
25/// ```rust,no_run
26/// tauri::Builder::default()
27///   .setup(|app| {
28///     // open the given URL on the system default browser
29///     tauri_plugin_opener::open_url("https://github.com/tauri-apps/tauri", None::<&str>)?;
30///     Ok(())
31///   });
32/// ```
33pub fn open_url<P: AsRef<str>, S: AsRef<str>>(url: P, with: Option<S>) -> crate::Result<()> {
34    let url = url.as_ref();
35    open(url, with)
36}
37
38/// Opens path with the program specified in `with`, or system default if `None`.
39///
40/// ## Platform-specific:
41///
42/// - **Android / iOS**: Always opens using default program.
43///
44/// # Examples
45///
46/// ```rust,no_run
47/// tauri::Builder::default()
48///   .setup(|app| {
49///     // open the given URL on the system default explorer
50///     tauri_plugin_opener::open_path("/path/to/file", None::<&str>)?;
51///     Ok(())
52///   });
53/// ```
54pub fn open_path<P: AsRef<Path>, S: AsRef<str>>(path: P, with: Option<S>) -> crate::Result<()> {
55    let path = path.as_ref();
56    if with.is_none() {
57        // Returns an IO error if not exists, and besides `exists()` is a shorthand for `metadata()`
58        _ = path.metadata()?;
59    }
60    open(path, with)
61}