123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- // Copyright 2019-2023 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,
- Locale,
- }
- #[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> {
- #[cfg(windows)]
- use dunce::canonicalize;
- #[cfg(not(windows))]
- use std::fs::canonicalize;
- Ok(canonicalize(std::env::temp_dir())?)
- }
- fn locale<R: Runtime>(_context: InvokeContext<R>) -> super::Result<Option<String>> {
- Ok(crate::api::os::locale())
- }
- }
- #[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())
- }
- fn locale<R: Runtime>(_context: InvokeContext<R>) -> super::Result<Option<String>> {
- 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(target_os = "ios")]
- return "iOS";
- }
- #[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() {}
- #[tauri_macros::module_command_test(os_all, "os > all", runtime)]
- #[quickcheck_macros::quickcheck]
- fn locale() {}
- }
|