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
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

#![allow(unused_imports)]

use super::InvokeContext;
use crate::Runtime;
use serde::Deserialize;
use std::path::PathBuf;
use tauri_macros::{command_enum, module_command_handler, CommandModule};

/// The API descriptor.
#[command_enum]
#[derive(Deserialize, CommandModule)]
#[serde(tag = "cmd", rename_all = "camelCase")]
pub enum Cmd {
  Platform,
  Version,
  OsType,
  Arch,
  Tempdir,
}

#[cfg(os_all)]
impl Cmd {
  fn platform<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
    Ok(os_platform())
  }

  fn version<R: Runtime>(_context: InvokeContext<R>) -> super::Result<String> {
    Ok(os_info::get().version().to_string())
  }

  fn os_type<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
    Ok(os_type())
  }

  fn arch<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
    Ok(std::env::consts::ARCH)
  }

  fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
    Ok(std::env::temp_dir())
  }
}

#[cfg(not(os_all))]
impl Cmd {
  fn platform<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
    Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  }

  fn version<R: Runtime>(_context: InvokeContext<R>) -> super::Result<String> {
    Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  }

  fn os_type<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
    Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  }

  fn arch<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
    Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  }

  fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
    Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  }
}

#[cfg(os_all)]
fn os_type() -> &'static str {
  #[cfg(target_os = "linux")]
  return "Linux";
  #[cfg(target_os = "windows")]
  return "Windows_NT";
  #[cfg(target_os = "macos")]
  return "Darwin";
}

#[cfg(os_all)]
fn os_platform() -> &'static str {
  match std::env::consts::OS {
    "windows" => "win32",
    "macos" => "darwin",
    _ => std::env::consts::OS,
  }
}

#[cfg(test)]
mod tests {
  #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  #[quickcheck_macros::quickcheck]
  fn platform() {}

  #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  #[quickcheck_macros::quickcheck]
  fn version() {}

  #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  #[quickcheck_macros::quickcheck]
  fn os_type() {}

  #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  #[quickcheck_macros::quickcheck]
  fn arch() {}

  #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  #[quickcheck_macros::quickcheck]
  fn tempdir() {}
}