1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#[cfg_attr(target_os = "linux", path = "registration/linux.rs")]
#[cfg_attr(target_os = "windows", path = "registration/windows.rs")]
#[cfg_attr(target_os = "macos", path = "registration/mac.rs")]
#[cfg_attr(
all(
not(target_os = "linux"),
not(target_os = "windows"),
not(target_os = "macos")
),
path = "registration/empty.rs"
)]
mod registrar;
use crate::Error;
pub use registrar::register_app;
pub use url::Url;
#[derive(PartialEq)]
pub enum BinArg {
Url,
Arg(String),
}
impl From<String> for BinArg {
fn from(s: String) -> Self {
Self::Arg(s)
}
}
pub enum LaunchCommand {
Url(Url),
Bin {
path: std::path::PathBuf,
args: Vec<BinArg>,
},
Steam(u32),
}
impl LaunchCommand {
pub fn current_exe(args: Vec<BinArg>) -> Result<Self, Error> {
let path = std::env::current_exe()
.map_err(|e| Error::io("retrieving current executable path", e))?;
if args.iter().filter(|a| **a == BinArg::Url).count() > 1 {
return Err(Error::TooManyUrls);
}
Ok(Self::Bin { path, args })
}
}
pub struct Application {
pub id: crate::AppId,
pub name: Option<String>,
pub command: LaunchCommand,
}
#[allow(unused)]
pub(crate) fn create_command(path: std::path::PathBuf, args: Vec<BinArg>, url_str: &str) -> String {
use std::fmt::Write;
let mut cmd = format!("\"{}\"", path.display());
for arg in args {
match arg {
BinArg::Url => write!(&mut cmd, " {}", url_str),
BinArg::Arg(s) => {
if s.contains(' ') {
write!(&mut cmd, " \"{}\"", s)
} else {
write!(&mut cmd, " {}", s)
}
}
}
.unwrap();
}
cmd
}