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
//! Functionality for registering an application with Discord so that Discord can
//! start it in the future eg. when the user accpets an invite to play the game
//! by another user

#[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 {
    /// A placeholder token that will be filled with the url that was opened
    Url,
    /// Generic argument
    Arg(String),
}

impl From<String> for BinArg {
    fn from(s: String) -> Self {
        Self::Arg(s)
    }
}

pub enum LaunchCommand {
    /// A URL
    Url(Url),
    /// A binary with optional args
    Bin {
        /// A full path or a name of a binary in PATH
        path: std::path::PathBuf,
        /// The arguments to pass
        args: Vec<BinArg>,
    },
    /// A Steam game identifier
    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 {
    /// The application's unique Discord identifier
    pub id: crate::AppId,
    /// The application name, defaults to the id if not specified
    pub name: Option<String>,
    /// The command to launch the application itself.
    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) => {
                // Only handle spaces, if there are other whitespace characters
                // well...
                if s.contains(' ') {
                    write!(&mut cmd, " \"{}\"", s)
                } else {
                    write!(&mut cmd, " {}", s)
                }
            }
        }
        .unwrap();
    }

    cmd
}