diff --git a/.changes/rust-permission-state.md b/.changes/rust-permission-state.md index 0aea29dea5f6..610624d99cd5 100644 --- a/.changes/rust-permission-state.md +++ b/.changes/rust-permission-state.md @@ -2,4 +2,4 @@ "tauri": patch:enhance --- -Added `plugin::mobile::PermissionState` type for plugin authors. +Added `plugin:::PermissionState` enum. diff --git a/crates/tauri/src/plugin.rs b/crates/tauri/src/plugin.rs index 3bef88e73092..8ed584af4612 100644 --- a/crates/tauri/src/plugin.rs +++ b/crates/tauri/src/plugin.rs @@ -12,7 +12,10 @@ use crate::{ webview::PageLoadPayload, AppHandle, Error, RunEvent, Runtime, Webview, Window, }; -use serde::de::DeserializeOwned; +use serde::{ + de::{Deserialize, DeserializeOwned, Deserializer, Error as DeError}, + Serialize, Serializer, +}; use serde_json::Value as JsonValue; use tauri_macros::default_runtime; use thiserror::Error; @@ -898,3 +901,52 @@ fn initialize( ) .map_err(|e| Error::PluginInitialization(plugin.name().to_string(), e.to_string())) } + +/// Permission state. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PermissionState { + /// Permission access has been granted. + Granted, + /// Permission access has been denied. + Denied, + /// Permission must be requested, but you must explain to the user why your app needs that permission. **Android only**. + PromptWithRationale, + /// Unknown state. Must request permission. + Unknown, +} + +impl std::fmt::Display for PermissionState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Granted => write!(f, "granted"), + Self::Denied => write!(f, "denied"), + Self::PromptWithRationale => write!(f, "prompt-with-rationale"), + Self::Unknown => write!(f, "Unknown"), + } + } +} + +impl Serialize for PermissionState { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: Serializer, + { + serializer.serialize_str(self.to_string().as_ref()) + } +} + +impl<'de> Deserialize<'de> for PermissionState { + fn deserialize(deserializer: D) -> std::result::Result + where + D: Deserializer<'de>, + { + let s = ::deserialize(deserializer)?; + match s.to_lowercase().as_str() { + "granted" => Ok(Self::Granted), + "denied" => Ok(Self::Denied), + "prompt-with-rationale" => Ok(Self::PromptWithRationale), + "prompt" => Ok(Self::Unknown), + _ => Err(DeError::custom(format!("unknown permission state '{s}'"))), + } + } +} diff --git a/crates/tauri/src/plugin/mobile.rs b/crates/tauri/src/plugin/mobile.rs index 2b9646460b8e..689be85fdc42 100644 --- a/crates/tauri/src/plugin/mobile.rs +++ b/crates/tauri/src/plugin/mobile.rs @@ -14,10 +14,7 @@ use crate::{ #[cfg(mobile)] use std::sync::atomic::{AtomicI32, Ordering}; -use serde::{ - de::{Deserialize, DeserializeOwned, Deserializer, Error as DeError}, - Serialize, Serializer, -}; +use serde::{de::DeserializeOwned, Serialize}; use std::{ collections::HashMap, @@ -56,55 +53,6 @@ pub enum PluginInvokeError { CannotSerializePayload(serde_json::Error), } -/// Permission state. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum PermissionState { - /// Permission access has been granted. - Granted, - /// Permission access has been denied. - Denied, - /// Permission must be requested, but you must explain to the user why your app needs that permission. **Android only**. - PromptWithRationale, - /// Unknown state. Must request permission. - Unknown, -} - -impl std::fmt::Display for PermissionState { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Granted => write!(f, "granted"), - Self::Denied => write!(f, "denied"), - Self::PromptWithRationale => write!(f, "prompt-with-rationale"), - Self::Unknown => write!(f, "Unknown"), - } - } -} - -impl Serialize for PermissionState { - fn serialize(&self, serializer: S) -> std::result::Result - where - S: Serializer, - { - serializer.serialize_str(self.to_string().as_ref()) - } -} - -impl<'de> Deserialize<'de> for PermissionState { - fn deserialize(deserializer: D) -> std::result::Result - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - match s.to_lowercase().as_str() { - "granted" => Ok(Self::Granted), - "denied" => Ok(Self::Denied), - "prompt-with-rationale" => Ok(Self::PromptWithRationale), - "prompt" => Ok(Self::Unknown), - _ => Err(DeError::custom(format!("unknown permission state '{s}'"))), - } - } -} - pub(crate) fn register_channel(channel: Channel) { CHANNELS .get_or_init(Default::default)