operating_system.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![allow(unused_imports)]
  5. use super::InvokeContext;
  6. use crate::Runtime;
  7. use serde::Deserialize;
  8. use std::path::PathBuf;
  9. use tauri_macros::{command_enum, module_command_handler, CommandModule};
  10. /// The API descriptor.
  11. #[command_enum]
  12. #[derive(Deserialize, CommandModule)]
  13. #[serde(tag = "cmd", rename_all = "camelCase")]
  14. pub enum Cmd {
  15. Platform,
  16. Version,
  17. OsType,
  18. Arch,
  19. Tempdir,
  20. }
  21. #[cfg(os_all)]
  22. impl Cmd {
  23. fn platform<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  24. Ok(os_platform())
  25. }
  26. fn version<R: Runtime>(_context: InvokeContext<R>) -> super::Result<String> {
  27. Ok(os_info::get().version().to_string())
  28. }
  29. fn os_type<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  30. Ok(os_type())
  31. }
  32. fn arch<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  33. Ok(std::env::consts::ARCH)
  34. }
  35. fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
  36. Ok(std::env::temp_dir())
  37. }
  38. }
  39. #[cfg(not(os_all))]
  40. impl Cmd {
  41. fn platform<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  42. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  43. }
  44. fn version<R: Runtime>(_context: InvokeContext<R>) -> super::Result<String> {
  45. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  46. }
  47. fn os_type<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  48. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  49. }
  50. fn arch<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  51. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  52. }
  53. fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
  54. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  55. }
  56. }
  57. #[cfg(os_all)]
  58. fn os_type() -> &'static str {
  59. #[cfg(target_os = "linux")]
  60. return "Linux";
  61. #[cfg(target_os = "windows")]
  62. return "Windows_NT";
  63. #[cfg(target_os = "macos")]
  64. return "Darwin";
  65. #[cfg(target_os = "ios")]
  66. return "iOS";
  67. #[cfg(target_os = "android")]
  68. return "Android";
  69. }
  70. #[cfg(os_all)]
  71. fn os_platform() -> &'static str {
  72. match std::env::consts::OS {
  73. "windows" => "win32",
  74. "macos" => "darwin",
  75. _ => std::env::consts::OS,
  76. }
  77. }
  78. #[cfg(test)]
  79. mod tests {
  80. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  81. #[quickcheck_macros::quickcheck]
  82. fn platform() {}
  83. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  84. #[quickcheck_macros::quickcheck]
  85. fn version() {}
  86. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  87. #[quickcheck_macros::quickcheck]
  88. fn os_type() {}
  89. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  90. #[quickcheck_macros::quickcheck]
  91. fn arch() {}
  92. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  93. #[quickcheck_macros::quickcheck]
  94. fn tempdir() {}
  95. }