Skip to content

Commit

Permalink
feat(core): add plugin::PermissionState (#10817)
Browse files Browse the repository at this point in the history
* feat(core): add `plugin::mobile::PermissionState`

* move to plugin module

* default, specta::Type
  • Loading branch information
lucasfernog authored Aug 29, 2024
1 parent cc799b2 commit d7e5c00
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/rust-permission-state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Added `plugin:::PermissionState` enum.
56 changes: 55 additions & 1 deletion crates/tauri/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -898,3 +901,54 @@ fn initialize<R: Runtime>(
)
.map_err(|e| Error::PluginInitialization(plugin.name().to_string(), e.to_string()))
}

/// Permission state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "specta", derive(specta::Type))]
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.
#[default]
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<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}

impl<'de> Deserialize<'de> for PermissionState {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = <String as Deserialize>::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}'"))),
}
}
}

0 comments on commit d7e5c00

Please sign in to comment.