tauri_plugin_shell/
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 serde::{Deserialize, Deserializer};
8
9use crate::scope::OpenScope;
10use std::str::FromStr;
11
12/// Program to use on the [`open()`] call.
13#[deprecated(since = "2.1.0", note = "Use tauri-plugin-opener instead.")]
14pub enum Program {
15    /// Use the `open` program.
16    Open,
17    /// Use the `start` program.
18    Start,
19    /// Use the `xdg-open` program.
20    XdgOpen,
21    /// Use the `gio` program.
22    Gio,
23    /// Use the `gnome-open` program.
24    GnomeOpen,
25    /// Use the `kde-open` program.
26    KdeOpen,
27    /// Use the `wslview` program.
28    WslView,
29    /// Use the `Firefox` program.
30    Firefox,
31    /// Use the `Google Chrome` program.
32    Chrome,
33    /// Use the `Chromium` program.
34    Chromium,
35    /// Use the `Safari` program.
36    Safari,
37}
38
39impl FromStr for Program {
40    type Err = super::Error;
41
42    fn from_str(s: &str) -> Result<Self, Self::Err> {
43        let p = match s.to_lowercase().as_str() {
44            "open" => Self::Open,
45            "start" => Self::Start,
46            "xdg-open" => Self::XdgOpen,
47            "gio" => Self::Gio,
48            "gnome-open" => Self::GnomeOpen,
49            "kde-open" => Self::KdeOpen,
50            "wslview" => Self::WslView,
51            "firefox" => Self::Firefox,
52            "chrome" | "google chrome" => Self::Chrome,
53            "chromium" => Self::Chromium,
54            "safari" => Self::Safari,
55            _ => return Err(crate::Error::UnknownProgramName(s.to_string())),
56        };
57        Ok(p)
58    }
59}
60
61impl<'de> Deserialize<'de> for Program {
62    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
63    where
64        D: Deserializer<'de>,
65    {
66        let s = String::deserialize(deserializer)?;
67        Program::from_str(&s).map_err(|e| serde::de::Error::custom(e.to_string()))
68    }
69}
70
71impl Program {
72    pub(crate) fn name(self) -> &'static str {
73        match self {
74            Self::Open => "open",
75            Self::Start => "start",
76            Self::XdgOpen => "xdg-open",
77            Self::Gio => "gio",
78            Self::GnomeOpen => "gnome-open",
79            Self::KdeOpen => "kde-open",
80            Self::WslView => "wslview",
81
82            #[cfg(target_os = "macos")]
83            Self::Firefox => "Firefox",
84            #[cfg(not(target_os = "macos"))]
85            Self::Firefox => "firefox",
86
87            #[cfg(target_os = "macos")]
88            Self::Chrome => "Google Chrome",
89            #[cfg(not(target_os = "macos"))]
90            Self::Chrome => "google-chrome",
91
92            #[cfg(target_os = "macos")]
93            Self::Chromium => "Chromium",
94            #[cfg(not(target_os = "macos"))]
95            Self::Chromium => "chromium",
96
97            #[cfg(target_os = "macos")]
98            Self::Safari => "Safari",
99            #[cfg(not(target_os = "macos"))]
100            Self::Safari => "safari",
101        }
102    }
103}
104
105/// Opens path or URL with the program specified in `with`, or system default if `None`.
106///
107/// The path will be matched against the shell open validation regex, defaulting to `^((mailto:\w+)|(tel:\w+)|(https?://\w+)).+`.
108/// A custom validation regex may be supplied in the config in `plugins > shell > scope > open`.
109///
110/// # Examples
111///
112/// ```rust,no_run
113/// use tauri_plugin_shell::ShellExt;
114/// tauri::Builder::default()
115///   .setup(|app| {
116///     // open the given URL on the system default browser
117///     app.shell().open("https://github.com/tauri-apps/tauri", None)?;
118///     Ok(())
119///   });
120/// ```
121#[deprecated(since = "2.1.0", note = "Use tauri-plugin-opener instead.")]
122pub fn open<P: AsRef<str>>(scope: &OpenScope, path: P, with: Option<Program>) -> crate::Result<()> {
123    scope.open(path.as_ref(), with).map_err(Into::into)
124}