forked from vhspace/sdl3-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.rs
72 lines (64 loc) · 1.88 KB
/
url.rs
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
//! Opening URLs in default system handlers
use crate::get_error;
use crate::Error;
use std::error;
use std::ffi::{CString, NulError};
use std::fmt;
use sys::misc::SDL_OpenURL;
#[derive(Debug, Clone)]
pub enum OpenUrlError {
InvalidUrl(NulError),
SdlError(Error),
}
impl fmt::Display for OpenUrlError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::OpenUrlError::*;
match *self {
InvalidUrl(ref e) => write!(f, "Invalid URL: {}", e),
SdlError(ref e) => write!(f, "SDL error: {}", e),
}
}
}
impl error::Error for OpenUrlError {
fn description(&self) -> &str {
use self::OpenUrlError::*;
match *self {
InvalidUrl(_) => "invalid URL",
SdlError(ref e) => &e.0,
}
}
}
/// Opens a URL/URI in the default system-provided application.
///
/// This will most likely open a web browser for http:// and https:// links,
/// the default handler application for file:// links, but this varies
/// between platforms and is not supported on all of them.
/// It might also cause your window to lose focus, or pause your process on mobile.
///
/// There is no way to tell if the system successfully opened the provided URL,
/// an `Ok` result only means that something was launched to try to handle it.
///
/// # Examples
///
/// ```no_run
/// use sdl3::url::open_url;
///
/// open_url("https://github.com/revmischa/sdl3-rs")
/// .expect("Opening URLs not supported on this platform");
/// ```
#[doc(alias = "SDL_OpenURL")]
pub fn open_url(url: &str) -> Result<(), OpenUrlError> {
use self::OpenUrlError::*;
let result = unsafe {
let url = match CString::new(url) {
Ok(s) => s,
Err(err) => return Err(InvalidUrl(err)),
};
SDL_OpenURL(url.as_ptr())
};
if result {
Ok(())
} else {
Err(SdlError(get_error()))
}
}