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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Provides operating system-related utility methods and properties.
//! 

use serde::{Deserialize, Serialize};
//use std::path::PathBuf;

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum Arch {
    #[serde(rename = "x86")]
    X86,
    #[serde(rename = "x86_64")]
    X86_64,
    #[serde(rename = "arm")]
    Arm,
    #[serde(rename = "aarch64")]
    Aarch64,
    #[serde(rename = "mips")]
    Mips,
    #[serde(rename = "mips64")]
    Mips64,
    #[serde(rename = "powerpc")]
    Powerpc,
    #[serde(rename = "powerpc64")]
    Powerpc64,
    #[serde(rename = "riscv64")]
    Riscv64,
    #[serde(rename = "s390x")]
    S390x,
    #[serde(rename = "sparc64")]
    Sparc64,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum Platform {
    #[serde(rename = "linux")]
    Linux,
    #[serde(rename = "macos")]
    Macos,
    #[serde(rename = "ios")]
    Ios,
    #[serde(rename = "freebsd")]
    Freebsd,
    #[serde(rename = "dragonfly")]
    Dragonfly,
    #[serde(rename = "netbsd")]
    Netbsd,
    #[serde(rename = "openbsd")]
    Openbsd,
    #[serde(rename = "solaris")]
    Solaris,
    #[serde(rename = "android")]
    Android,
    #[serde(rename = "windows")]
    Windows,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum OsKind {
    #[serde(rename = "linux")]
    Linux,
    #[serde(rename = "windows")]
    Windows,
    #[serde(rename = "macos")]
    Macos,
    #[serde(rename = "ios")]
    Ios,
    #[serde(rename = "android")]
    Android,
}

/// Returns the operating system CPU architecture for which the tauri app was compiled.
#[inline(always)]
pub async fn arch() -> crate::Result<Arch> {
    let raw = base::arch().await?;

    Ok(serde_wasm_bindgen::from_value(raw)?)
}

/// Returns the operating system eol.
#[inline(always)]
pub async fn eol() -> crate::Result<String> {
    let raw = base::eol().await?;

    Ok(serde_wasm_bindgen::from_value(raw)?)
}

/// Returns the binary extension.
#[inline(always)]
pub async fn exe_extension() -> crate::Result<String> {
    let raw = base::exe_extension().await?;

    Ok(serde_wasm_bindgen::from_value(raw)?)
}

/// Returns the operating system Family.
#[inline(always)]
pub async fn family() -> crate::Result<String> {
    let raw = base::family().await?;

    Ok(serde_wasm_bindgen::from_value(raw)?)
}

/// Returns the operating system Hostname.
#[inline(always)]
pub async fn hostname() -> crate::Result<String> {
    let raw = base::hostname().await?;

    Ok(serde_wasm_bindgen::from_value(raw)?)
}

/// Returns the operating system Local.
#[inline(always)]
pub async fn locale() -> crate::Result<String> {
    let raw = base::locale().await?;

    Ok(serde_wasm_bindgen::from_value(raw)?)
}

/// Returns a string identifying the operating system platform. The value is set at compile time.
#[inline(always)]
pub async fn platform() -> crate::Result<Platform> {
    let raw = base::platform().await?;

    Ok(serde_wasm_bindgen::from_value(raw)?)
}

/// Returns [`OsKind::Linux`] on Linux, [`OsKind::Darwin`] on macOS, and [`OsKind::WindowsNT`] on Windows.
#[inline(always)]
pub async fn kind() -> crate::Result<OsKind> {
    let raw = base::kind().await?;

    Ok(serde_wasm_bindgen::from_value(raw)?)
}

/// Returns a string identifying the kernel version.
#[inline(always)]
pub async fn version() -> crate::Result<String> {
    let raw = base::version().await?;

    Ok(serde_wasm_bindgen::from_value(raw)?)
}

mod base {
    use wasm_bindgen::prelude::*;

    #[wasm_bindgen(module = "/src/scripts/plugins/os.js")]
    extern "C" {
        #[wasm_bindgen(catch)]
        pub async fn arch() -> Result<JsValue, JsValue>;
        #[wasm_bindgen(catch)]
        pub async fn eol() -> Result<JsValue, JsValue>;
        #[wasm_bindgen(catch, js_name = "exeExtension")]
        pub async fn exe_extension() -> Result<JsValue, JsValue>;
        #[wasm_bindgen(catch)]
        pub async fn family() -> Result<JsValue, JsValue>;
        #[wasm_bindgen(catch)]
        pub async fn hostname() -> Result<JsValue, JsValue>;
        #[wasm_bindgen(catch)]
        pub async fn locale() -> Result<JsValue, JsValue>;
        #[wasm_bindgen(catch)]
        pub async fn platform() -> Result<JsValue, JsValue>;
        #[wasm_bindgen(catch, js_name = "type")]
        pub async fn kind() -> Result<JsValue, JsValue>;
        #[wasm_bindgen(catch)]
        pub async fn version() -> Result<JsValue, JsValue>;
    }
}