@@ -8,7 +8,7 @@ use crate::{
88 attributes:: ObjectAttributes ,
99 interface_types:: algorithm:: { HashingAlgorithm , PublicAlgorithm } ,
1010 structures:: { Digest , EccPoint , PublicKeyRsa , SymmetricCipherParameters } ,
11- traits:: { impl_mu_standard, Marshall , UnMarshall } ,
11+ traits:: { impl_mu_standard, Marshall } ,
1212 tss2_esys:: { TPM2B_PUBLIC , TPMT_PUBLIC } ,
1313 Error , Result , ReturnCode , WrapperErrorKind ,
1414} ;
@@ -18,7 +18,6 @@ use keyed_hash::PublicKeyedHashParameters;
1818use rsa:: PublicRsaParameters ;
1919
2020use log:: error;
21- use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
2221use std:: convert:: { TryFrom , TryInto } ;
2322use tss_esapi_sys:: { TPMU_PUBLIC_ID , TPMU_PUBLIC_PARMS } ;
2423
@@ -299,6 +298,9 @@ impl Default for PublicBuilder {
299298///
300299/// # Details
301300/// This corresponds to TPMT_PUBLIC
301+ ///
302+ /// This object can be serialized and deserialized
303+ /// using serde if the `serde` feature is enabled.
302304#[ derive( Debug , Clone , Eq , PartialEq ) ]
303305pub enum Public {
304306 Rsa {
@@ -500,30 +502,35 @@ impl TryFrom<TPMT_PUBLIC> for Public {
500502
501503impl_mu_standard ! ( Public , TPMT_PUBLIC ) ;
502504
503- impl Serialize for Public {
504- /// Serialize the [Public] data into it's bytes representation of the TCG
505- /// TPMT_PUBLIC structure.
506- fn serialize < S > ( & self , serializer : S ) -> std:: result:: Result < S :: Ok , S :: Error >
507- where
508- S : Serializer ,
509- {
510- let bytes = self . marshall ( ) . map_err ( serde:: ser:: Error :: custom) ?;
511- serializer. serialize_bytes ( & bytes)
512- }
513- }
505+ cfg_if:: cfg_if! {
506+ if #[ cfg( feature = "serde" ) ] {
507+ use crate :: traits:: UnMarshall ;
508+
509+ impl serde:: Serialize for Public {
510+ /// Serialize the [Public] data into it's bytes representation of the TCG
511+ /// TPMT_PUBLIC structure.
512+ fn serialize<S >( & self , serializer: S ) -> std:: result:: Result <S :: Ok , S :: Error >
513+ where
514+ S : serde:: Serializer ,
515+ {
516+ let bytes = self . marshall( ) . map_err( serde:: ser:: Error :: custom) ?;
517+ serializer. serialize_bytes( & bytes)
518+ }
519+ }
514520
515- impl < ' de > Deserialize < ' de > for Public {
516- /// Deserialise the [Public] data from it's bytes representation of the TCG
517- /// TPMT_PUBLIC structure.
518- fn deserialize < D > ( deserializer : D ) -> std:: result:: Result < Self , D :: Error >
519- where
520- D : Deserializer < ' de > ,
521- {
522- let bytes = <Vec < u8 > >:: deserialize ( deserializer) ?;
523- Self :: unmarshall ( & bytes) . map_err ( serde:: de:: Error :: custom)
521+ impl <' de> serde:: Deserialize <' de> for Public {
522+ /// Deserialise the [Public] data from it's bytes representation of the TCG
523+ /// TPMT_PUBLIC structure.
524+ fn deserialize<D >( deserializer: D ) -> std:: result:: Result <Self , D :: Error >
525+ where
526+ D : serde:: Deserializer <' de>,
527+ {
528+ let bytes = <Vec <u8 >>:: deserialize( deserializer) ?;
529+ Self :: unmarshall( & bytes) . map_err( serde:: de:: Error :: custom)
530+ }
531+ }
524532 }
525533}
526-
527534impl TryFrom < TPM2B_PUBLIC > for Public {
528535 type Error = Error ;
529536
0 commit comments