@@ -8,10 +8,10 @@ use serde::{
88} ;
99use zvariant:: {
1010 self , Array , DeserializeDict , DynamicDeserialize , NoneValue , Optional , OwnedValue ,
11- SerializeDict , Signature , Structure , StructureBuilder , Type , Value , signature:: Fields ,
11+ SerializeDict , Signature , Str , Structure , StructureBuilder , Type , Value , signature:: Fields ,
1212} ;
1313
14- use crate :: model:: { Device , Operation , RequestingApplication } ;
14+ use crate :: model:: { BackendRequest , Device , Operation , RequestId , RequestingApplication } ;
1515
1616const TAG_VALUE_SIGNATURE : & Signature = & Signature :: Structure ( Fields :: Static {
1717 fields : & [ & Signature :: U32 , & Signature :: Variant ] ,
@@ -49,6 +49,13 @@ const BACKGROUND_EVENT_ERROR_CREDENTIAL_EXCLUDED: u32 = 0x80000006;
4949const BACKGROUND_EVENT_ERROR_PIN_ATTEMPTS_EXHAUSTED : u32 = 0x80000007 ;
5050const BACKGROUND_EVENT_ERROR_PIN_NOT_SET : u32 = 0x80000008 ;
5151
52+ const BACKEND_REQUEST_START_HYBRID_DISCOVERY : u32 = 0x01 ;
53+ const BACKEND_REQUEST_START_USB_DISCOVERY : u32 = 0x02 ;
54+ const BACKEND_REQUEST_START_NFC_DISCOVERY : u32 = 0x03 ;
55+ const BACKEND_REQUEST_ENTER_CLIENT_PIN : u32 = 0x04 ;
56+ const BACKEND_REQUEST_SELECT_CREDENTIAL : u32 = 0x05 ;
57+ const BACKEND_REQUEST_CANCEL_REQUEST : u32 = 0x06 ;
58+
5259/// Flattened enum BackgroundEvent for sending across D-Bus.
5360#[ derive( Debug , Clone , PartialEq ) ]
5461pub enum BackgroundEvent {
@@ -267,6 +274,94 @@ impl<'de> Deserialize<'de> for BackgroundEvent {
267274 }
268275}
269276
277+ impl Type for BackendRequest {
278+ const SIGNATURE : & ' static Signature = TAG_VALUE_SIGNATURE ;
279+ }
280+
281+ impl From < & BackendRequest > for Structure < ' _ > {
282+ fn from ( value : & BackendRequest ) -> Self {
283+ match value {
284+ BackendRequest :: StartHybridDiscovery => tag_value_to_struct ( 0x01 , None ) ,
285+ BackendRequest :: StartNfcDiscovery => tag_value_to_struct ( 0x02 , None ) ,
286+ BackendRequest :: StartUsbDiscovery => tag_value_to_struct ( 0x03 , None ) ,
287+ BackendRequest :: EnterClientPin ( pin) => {
288+ tag_value_to_struct ( 0x04 , Some ( Value :: Str ( pin. into ( ) ) ) )
289+ }
290+ BackendRequest :: SelectCredential ( credential_id) => {
291+ tag_value_to_struct ( 0x05 , Some ( Value :: Str ( credential_id. into ( ) ) ) )
292+ }
293+ BackendRequest :: CancelRequest => tag_value_to_struct ( 0x06 , None ) ,
294+ }
295+ }
296+ }
297+
298+ impl TryFrom < & Structure < ' _ > > for BackendRequest {
299+ type Error = zvariant:: Error ;
300+
301+ fn try_from ( value : & Structure < ' _ > ) -> Result < Self , Self :: Error > {
302+ let ( tag, value) = parse_tag_value_struct ( value) ?;
303+
304+ match tag {
305+ 0x01 => Ok ( BackendRequest :: StartHybridDiscovery ) ,
306+ 0x02 => Ok ( BackendRequest :: StartNfcDiscovery ) ,
307+ 0x03 => Ok ( BackendRequest :: StartUsbDiscovery ) ,
308+ 0x04 => {
309+ let s: Str = value. downcast_ref ( ) ?;
310+ if s. is_empty ( ) {
311+ return Err ( zvariant:: Error :: invalid_length (
312+ s. len ( ) ,
313+ & "a non-empty string" ,
314+ ) ) ;
315+ }
316+ Ok ( BackendRequest :: EnterClientPin ( s. as_str ( ) . to_string ( ) ) )
317+ }
318+ 0x05 => {
319+ let s: Str = value. downcast_ref ( ) ?;
320+ if s. is_empty ( ) {
321+ return Err ( zvariant:: Error :: invalid_length (
322+ s. len ( ) ,
323+ & "a non-empty string" ,
324+ ) ) ;
325+ }
326+ Ok ( BackendRequest :: SelectCredential ( s. as_str ( ) . to_string ( ) ) )
327+ }
328+ 0x06 => Ok ( BackendRequest :: CancelRequest ) ,
329+ _ => Err ( zvariant:: Error :: Message ( format ! (
330+ "Unknown BackendRequest tag : {tag}"
331+ ) ) ) ,
332+ }
333+ }
334+ }
335+
336+ impl Serialize for BackendRequest {
337+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
338+ where
339+ S : serde:: Serializer ,
340+ {
341+ let structure: Structure = self . into ( ) ;
342+ structure. serialize ( serializer)
343+ }
344+ }
345+
346+ impl < ' de > Deserialize < ' de > for BackendRequest {
347+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
348+ where
349+ D : serde:: Deserializer < ' de > ,
350+ {
351+ let d = Structure :: deserializer_for_signature ( TAG_VALUE_SIGNATURE ) . map_err ( |err| {
352+ D :: Error :: custom ( format ! (
353+ "could not create deserializer for tag-value struct: {err}"
354+ ) )
355+ } ) ?;
356+ let structure = d. deserialize ( deserializer) ?;
357+ ( & structure) . try_into ( ) . map_err ( |err| {
358+ D :: Error :: custom ( format ! (
359+ "could not deserialize structure into BackendRequest: {err}"
360+ ) )
361+ } )
362+ }
363+ }
364+
270365#[ derive( Clone , Debug , DeserializeDict , Type ) ]
271366#[ zvariant( signature = "dict" ) ]
272367pub struct CreateCredentialRequest {
@@ -412,10 +507,7 @@ impl From<GetPublicKeyCredentialResponse> for GetCredentialResponse {
412507 }
413508}
414509
415- /// Identifier for a request to be used for cancellation.
416- pub type RequestId = u32 ;
417-
418- #[ derive( Serialize , Deserialize , Type ) ]
510+ #[ derive( Clone , Debug , Serialize , Deserialize , Type ) ]
419511pub struct ViewRequest {
420512 pub operation : Operation ,
421513
@@ -435,7 +527,7 @@ pub struct ViewRequest {
435527 pub window_handle : Optional < WindowHandle > ,
436528}
437529
438- #[ derive( Type , PartialEq , Debug ) ]
530+ #[ derive( Clone , Debug , PartialEq , Type ) ]
439531#[ zvariant( signature = "s" ) ]
440532pub enum WindowHandle {
441533 Wayland ( String ) ,
0 commit comments