@@ -28,6 +28,9 @@ impl From<&BackgroundEvent> for Structure<'_> {
2828 BackgroundEvent :: HybridQrStateChanged ( state) => {
2929 tag_value_to_struct ( 0x02 , Some ( Value :: Structure ( state. into ( ) ) ) )
3030 }
31+ BackgroundEvent :: NfcStateChanged ( state) => {
32+ tag_value_to_struct ( 0x03 , Some ( Value :: Structure ( state. into ( ) ) ) )
33+ }
3134 }
3235 }
3336}
@@ -49,6 +52,10 @@ impl TryFrom<&Structure<'_>> for BackgroundEvent {
4952 ( & structure) . try_into ( ) ?,
5053 ) )
5154 }
55+ 0x03 => {
56+ let structure: Structure = value. downcast_ref ( ) ?;
57+ Ok ( BackgroundEvent :: NfcStateChanged ( ( & structure) . try_into ( ) ?) )
58+ }
5259 _ => Err ( zvariant:: Error :: Message ( format ! (
5360 "Unknown BackgroundEvent tag : {tag}"
5461 ) ) ) ,
@@ -336,6 +343,10 @@ impl Type for crate::model::UsbState {
336343 const SIGNATURE : & ' static Signature = TAG_VALUE_SIGNATURE ;
337344}
338345
346+ impl Type for crate :: model:: NfcState {
347+ const SIGNATURE : & ' static Signature = TAG_VALUE_SIGNATURE ;
348+ }
349+
339350impl From < & crate :: model:: UsbState > for Structure < ' _ > {
340351 fn from ( value : & crate :: model:: UsbState ) -> Self {
341352 let ( tag, value) : ( u8 , Option < Value > ) = match value {
@@ -462,6 +473,128 @@ impl<'de> Deserialize<'de> for crate::model::UsbState {
462473 }
463474}
464475
476+ impl From < & crate :: model:: NfcState > for Structure < ' _ > {
477+ fn from ( value : & crate :: model:: NfcState ) -> Self {
478+ let ( tag, value) : ( u8 , Option < Value > ) = match value {
479+ crate :: model:: NfcState :: Idle => ( 0x01 , None ) ,
480+ crate :: model:: NfcState :: Waiting => ( 0x02 , None ) ,
481+ crate :: model:: NfcState :: Connected => ( 0x04 , None ) ,
482+ // TODO: Add pin request reason to this struct
483+ crate :: model:: NfcState :: NeedsPin { attempts_left } => {
484+ let num = match attempts_left {
485+ Some ( num) => * num as i32 ,
486+ None => -1 ,
487+ } ;
488+ ( 0x05 , Some ( Value :: I32 ( num) ) )
489+ }
490+ crate :: model:: NfcState :: NeedsUserVerification { attempts_left } => {
491+ let num = match attempts_left {
492+ Some ( num) => * num as i32 ,
493+ None => -1 ,
494+ } ;
495+ ( 0x06 , Some ( Value :: I32 ( num) ) )
496+ }
497+ crate :: model:: NfcState :: SelectCredential { creds } => {
498+ let creds: Vec < Credential > = creds. iter ( ) . map ( Credential :: from) . collect ( ) ;
499+ let value = Value :: new ( creds) ;
500+ ( 0x08 , Some ( value) )
501+ }
502+ crate :: model:: NfcState :: Completed => ( 0x09 , None ) ,
503+ crate :: model:: NfcState :: Failed ( error) => {
504+ let value = Value :: < ' _ > :: from ( error. to_string ( ) ) ;
505+ ( 0x0A , Some ( value) )
506+ }
507+ } ;
508+ tag_value_to_struct ( tag, value)
509+ }
510+ }
511+
512+ impl TryFrom < & Structure < ' _ > > for crate :: model:: NfcState {
513+ type Error = zvariant:: Error ;
514+
515+ fn try_from ( structure : & Structure < ' _ > ) -> Result < Self , Self :: Error > {
516+ let ( tag, value) = parse_tag_value_struct ( structure) ?;
517+ match tag {
518+ 0x01 => Ok ( Self :: Idle ) ,
519+ 0x02 => Ok ( Self :: Waiting ) ,
520+ 0x04 => Ok ( Self :: Connected ) ,
521+ 0x05 => {
522+ let attempts_left: i32 = value. downcast_ref ( ) ?;
523+ let attempts_left = if attempts_left == -1 {
524+ None
525+ } else {
526+ Some ( attempts_left as u32 )
527+ } ;
528+ Ok ( Self :: NeedsPin { attempts_left } )
529+ }
530+ 0x06 => {
531+ let attempts_left: i32 = value. downcast_ref ( ) ?;
532+ let attempts_left = if attempts_left == -1 {
533+ None
534+ } else {
535+ Some ( attempts_left as u32 )
536+ } ;
537+ Ok ( Self :: NeedsUserVerification { attempts_left } )
538+ }
539+ 0x08 => {
540+ let creds: Array = value. downcast_ref ( ) ?;
541+ let creds: Result < Vec < crate :: model:: Credential > , zvariant:: Error > = creds
542+ . iter ( )
543+ . map ( |v| v. try_to_owned ( ) . unwrap ( ) )
544+ . map ( |v| {
545+ let cred: Result < crate :: model:: Credential , zvariant:: Error > =
546+ Value :: from ( v)
547+ . downcast :: < Credential > ( )
548+ . map ( crate :: model:: Credential :: from) ;
549+ cred
550+ } )
551+ . collect ( ) ;
552+ Ok ( Self :: SelectCredential { creds : creds? } )
553+ }
554+ 0x09 => Ok ( Self :: Completed ) ,
555+ 0x0A => {
556+ let err_code: & str = value. downcast_ref ( ) ?;
557+ let err = match err_code {
558+ "AuthenticatorError" => crate :: model:: Error :: AuthenticatorError ,
559+ "NoCredentials" => crate :: model:: Error :: NoCredentials ,
560+ "CredentialExcluded" => crate :: model:: Error :: CredentialExcluded ,
561+ "PinAttemptsExhausted" => crate :: model:: Error :: PinAttemptsExhausted ,
562+ s => crate :: model:: Error :: Internal ( String :: from ( s) ) ,
563+ } ;
564+ Ok ( Self :: Failed ( err) )
565+ }
566+ _ => Err ( zvariant:: Error :: IncorrectType ) ,
567+ }
568+ }
569+ }
570+
571+ impl TryFrom < Structure < ' _ > > for crate :: model:: NfcState {
572+ type Error = zvariant:: Error ;
573+
574+ fn try_from ( structure : Structure < ' _ > ) -> Result < Self , Self :: Error > {
575+ Self :: try_from ( & structure)
576+ }
577+ }
578+
579+ impl Serialize for crate :: model:: NfcState {
580+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
581+ where
582+ S : serde:: Serializer ,
583+ {
584+ let structure: Structure = self . into ( ) ;
585+ structure. serialize ( serializer)
586+ }
587+ }
588+
589+ impl < ' de > Deserialize < ' de > for crate :: model:: NfcState {
590+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
591+ where
592+ D : serde:: Deserializer < ' de > ,
593+ {
594+ deserialize_tag_value ( deserializer)
595+ }
596+ }
597+
465598fn deserialize_tag_value < ' a , ' de , T , D > ( deserializer : D ) -> Result < T , D :: Error >
466599where
467600 T : TryFrom < Structure < ' a > > ,
0 commit comments