Skip to content

Commit

Permalink
move to plugin module
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 28, 2024
1 parent 34a1394 commit 5580edf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .changes/rust-permission-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"tauri": patch:enhance
---

Added `plugin::mobile::PermissionState` type for plugin authors.
Added `plugin:::PermissionState` enum.
54 changes: 53 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,52 @@ fn initialize<R: Runtime>(
)
.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<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}'"))),
}
}
}
54 changes: 1 addition & 53 deletions crates/tauri/src/plugin/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<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::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<serde_json::Value>) {
CHANNELS
.get_or_init(Default::default)
Expand Down

0 comments on commit 5580edf

Please sign in to comment.