Skip to content

Commit 63921fa

Browse files
committed
refactor: change tauri::api::open with argument to an enum [TRI-022] (#19)
1 parent eed0172 commit 63921fa

5 files changed

Lines changed: 107 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
The `tauri::api::shell::open`'s `with` argument is now an enum value instead of any string.

core/tauri/src/api/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub enum Error {
8383
/// Shell error.
8484
#[error("shell error: {0}")]
8585
Shell(String),
86+
/// Unknown program name.
87+
#[error("unknown program name: {0}")]
88+
UnknownProgramName(String),
8689
}
8790

8891
#[cfg(feature = "cli")]

core/tauri/src/api/shell.rs

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,95 @@
44

55
//! Types and functions related to shell.
66
7+
use std::str::FromStr;
8+
9+
/// Program to use on the [`open`] call.
10+
pub enum Program {
11+
/// Use the `open` program.
12+
Open,
13+
/// Use the `start` program.
14+
Start,
15+
/// Use the `xdg-open` program.
16+
XdgOpen,
17+
/// Use the `gio` program.
18+
Gio,
19+
/// Use the `gnome-open` program.
20+
GnomeOpen,
21+
/// Use the `kde-open` program.
22+
KdeOpen,
23+
/// Use the `wslview` program.
24+
WslView,
25+
/// Use the `Firefox` program.
26+
Firefox,
27+
/// Use the `Google Chrome` program.
28+
Chrome,
29+
/// Use the `Chromium` program.
30+
Chromium,
31+
/// Use the `Safari` program.
32+
Safari,
33+
}
34+
35+
impl FromStr for Program {
36+
type Err = super::Error;
37+
38+
fn from_str(s: &str) -> Result<Self, Self::Err> {
39+
let p = match s.to_lowercase().as_str() {
40+
"open" => Self::Open,
41+
"start" => Self::Start,
42+
"xdg-open" => Self::XdgOpen,
43+
"gio" => Self::Gio,
44+
"gnome-open" => Self::GnomeOpen,
45+
"kde-open" => Self::KdeOpen,
46+
"wslview" => Self::WslView,
47+
"firefox" => Self::Firefox,
48+
"chrome" | "google chrome" => Self::Chrome,
49+
"chromium" => Self::Chromium,
50+
"safari" => Self::Safari,
51+
_ => return Err(super::Error::UnknownProgramName(s.to_string())),
52+
};
53+
Ok(p)
54+
}
55+
}
56+
57+
impl Program {
58+
fn name(self) -> &'static str {
59+
match self {
60+
Self::Open => "open",
61+
Self::Start => "start",
62+
Self::XdgOpen => "xdg-open",
63+
Self::Gio => "gio",
64+
Self::GnomeOpen => "gnome-open",
65+
Self::KdeOpen => "kde-open",
66+
Self::WslView => "wslview",
67+
68+
#[cfg(target_os = "macos")]
69+
Self::Firefox => "Firefox",
70+
#[cfg(not(target_os = "macos"))]
71+
Self::Firefox => "firefox",
72+
73+
#[cfg(target_os = "macos")]
74+
Self::Chrome => "Google Chrome",
75+
#[cfg(not(target_os = "macos"))]
76+
Self::Chrome => "google-chrome",
77+
78+
#[cfg(target_os = "macos")]
79+
Self::Chromium => "Chromium",
80+
#[cfg(not(target_os = "macos"))]
81+
Self::Chromium => "chromium",
82+
83+
#[cfg(target_os = "macos")]
84+
Self::Safari => "Safari",
85+
#[cfg(not(target_os = "macos"))]
86+
Self::Safari => "safari",
87+
}
88+
}
89+
}
90+
791
/// Opens path or URL with program specified in `with`, or system default if `None`.
8-
pub fn open(path: String, with: Option<String>) -> crate::api::Result<()> {
92+
pub fn open(path: String, with: Option<Program>) -> crate::api::Result<()> {
993
{
1094
let exit_status = if let Some(with) = with {
11-
open::with(&path, &with)
95+
open::with(&path, with.name())
1296
} else {
1397
open::that(&path)
1498
};

core/tauri/src/endpoints/shell.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,15 @@ impl Cmd {
158158
}
159159
Self::Open { path, with } => {
160160
#[cfg(shell_open)]
161-
match crate::api::shell::open(path, with) {
161+
match crate::api::shell::open(
162+
path,
163+
if let Some(w) = with {
164+
use std::str::FromStr;
165+
Some(crate::api::shell::Program::from_str(&w)?)
166+
} else {
167+
None
168+
},
169+
) {
162170
Ok(_) => Ok(().into()),
163171
Err(err) => Err(crate::Error::FailedToExecuteApi(err)),
164172
}

tooling/api/src/shell.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ type CommandEvent =
341341
/**
342342
* Opens a path or URL with the system's default app,
343343
* or the one specified with `openWith`.
344+
*
345+
* The `openWith` value must be one of `firefox`, `google chrome`, `chromium` `safari`,
346+
* `open`, `start`, `xdg-open`, `gio`, gnome-open`, `kde-open` or `wslview`.
347+
*
344348
* @example
345349
* ```typescript
346350
* // opens the given URL on the default browser:

0 commit comments

Comments
 (0)