operating_system.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. Locale,
  21. }
  22. #[cfg(os_all)]
  23. impl Cmd {
  24. fn platform<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  25. Ok(os_platform())
  26. }
  27. fn version<R: Runtime>(_context: InvokeContext<R>) -> super::Result<String> {
  28. Ok(os_info::get().version().to_string())
  29. }
  30. fn os_type<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  31. Ok(os_type())
  32. }
  33. fn arch<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  34. Ok(std::env::consts::ARCH)
  35. }
  36. fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
  37. #[cfg(windows)]
  38. use dunce::canonicalize;
  39. #[cfg(not(windows))]
  40. use std::fs::canonicalize;
  41. Ok(canonicalize(std::env::temp_dir())?)
  42. }
  43. fn locale<R: Runtime>(_context: InvokeContext<R>) -> super::Result<Option<String>> {
  44. Ok(crate::api::os::locale())
  45. }
  46. }
  47. #[cfg(not(os_all))]
  48. impl Cmd {
  49. fn platform<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  50. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  51. }
  52. fn version<R: Runtime>(_context: InvokeContext<R>) -> super::Result<String> {
  53. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  54. }
  55. fn os_type<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  56. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  57. }
  58. fn arch<R: Runtime>(_context: InvokeContext<R>) -> super::Result<&'static str> {
  59. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  60. }
  61. fn tempdir<R: Runtime>(_context: InvokeContext<R>) -> super::Result<PathBuf> {
  62. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  63. }
  64. fn locale<R: Runtime>(_context: InvokeContext<R>) -> super::Result<Option<String>> {
  65. Err(crate::Error::ApiNotAllowlisted("os > all".into()).into_anyhow())
  66. }
  67. }
  68. #[cfg(os_all)]
  69. fn os_type() -> &'static str {
  70. #[cfg(target_os = "linux")]
  71. return "Linux";
  72. #[cfg(target_os = "windows")]
  73. return "Windows_NT";
  74. #[cfg(target_os = "macos")]
  75. return "Darwin";
  76. #[cfg(target_os = "ios")]
  77. return "iOS";
  78. }
  79. #[cfg(os_all)]
  80. fn os_platform() -> &'static str {
  81. match std::env::consts::OS {
  82. "windows" => "win32",
  83. "macos" => "darwin",
  84. _ => std::env::consts::OS,
  85. }
  86. }
  87. #[cfg(test)]
  88. mod tests {
  89. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  90. #[quickcheck_macros::quickcheck]
  91. fn platform() {}
  92. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  93. #[quickcheck_macros::quickcheck]
  94. fn version() {}
  95. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  96. #[quickcheck_macros::quickcheck]
  97. fn os_type() {}
  98. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  99. #[quickcheck_macros::quickcheck]
  100. fn arch() {}
  101. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  102. #[quickcheck_macros::quickcheck]
  103. fn tempdir() {}
  104. #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
  105. #[quickcheck_macros::quickcheck]
  106. fn locale() {}
  107. }