@@ -12,7 +12,10 @@ use crate::{
1212 webview:: PageLoadPayload ,
1313 AppHandle , Error , RunEvent , Runtime , Webview , Window ,
1414} ;
15- use serde:: de:: DeserializeOwned ;
15+ use serde:: {
16+ de:: { Deserialize , DeserializeOwned , Deserializer , Error as DeError } ,
17+ Serialize , Serializer ,
18+ } ;
1619use serde_json:: Value as JsonValue ;
1720use tauri_macros:: default_runtime;
1821use thiserror:: Error ;
@@ -898,3 +901,54 @@ fn initialize<R: Runtime>(
898901 )
899902 . map_err ( |e| Error :: PluginInitialization ( plugin. name ( ) . to_string ( ) , e. to_string ( ) ) )
900903}
904+
905+ /// Permission state.
906+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Default ) ]
907+ #[ cfg_attr( feature = "specta" , derive( specta:: Type ) ) ]
908+ pub enum PermissionState {
909+ /// Permission access has been granted.
910+ Granted ,
911+ /// Permission access has been denied.
912+ Denied ,
913+ /// Permission must be requested, but you must explain to the user why your app needs that permission. **Android only**.
914+ PromptWithRationale ,
915+ /// Unknown state. Must request permission.
916+ #[ default]
917+ Unknown ,
918+ }
919+
920+ impl std:: fmt:: Display for PermissionState {
921+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
922+ match self {
923+ Self :: Granted => write ! ( f, "granted" ) ,
924+ Self :: Denied => write ! ( f, "denied" ) ,
925+ Self :: PromptWithRationale => write ! ( f, "prompt-with-rationale" ) ,
926+ Self :: Unknown => write ! ( f, "Unknown" ) ,
927+ }
928+ }
929+ }
930+
931+ impl Serialize for PermissionState {
932+ fn serialize < S > ( & self , serializer : S ) -> std:: result:: Result < S :: Ok , S :: Error >
933+ where
934+ S : Serializer ,
935+ {
936+ serializer. serialize_str ( self . to_string ( ) . as_ref ( ) )
937+ }
938+ }
939+
940+ impl < ' de > Deserialize < ' de > for PermissionState {
941+ fn deserialize < D > ( deserializer : D ) -> std:: result:: Result < Self , D :: Error >
942+ where
943+ D : Deserializer < ' de > ,
944+ {
945+ let s = <String as Deserialize >:: deserialize ( deserializer) ?;
946+ match s. to_lowercase ( ) . as_str ( ) {
947+ "granted" => Ok ( Self :: Granted ) ,
948+ "denied" => Ok ( Self :: Denied ) ,
949+ "prompt-with-rationale" => Ok ( Self :: PromptWithRationale ) ,
950+ "prompt" => Ok ( Self :: Unknown ) ,
951+ _ => Err ( DeError :: custom ( format ! ( "unknown permission state '{s}'" ) ) ) ,
952+ }
953+ }
954+ }
0 commit comments