diff --git a/src/lib/header/encryption_header.rs b/src/lib/header/encryption_header.rs index 89d2372..616fabe 100644 --- a/src/lib/header/encryption_header.rs +++ b/src/lib/header/encryption_header.rs @@ -120,7 +120,7 @@ pub struct EncryptionHeader { #[cfg_attr(feature = "serde", serde(serialize_with = "crate::helper::buffer_to_base64", deserialize_with = "crate::helper::base64_to_buffer"))] pub encrypted_encryption_key: Vec, /// The decrypted encryption key. - #[cfg_attr(feature = "serde", serde(serialize_with = "crate::helper::buffer_to_base64"))] + #[cfg_attr(feature = "serde", serde(serialize_with = "crate::helper::option_buffer_to_base64"))] pub decrypted_encryption_key: Option> } diff --git a/src/lib/helper.rs b/src/lib/helper.rs index c83959b..14de20c 100644 --- a/src/lib/helper.rs +++ b/src/lib/helper.rs @@ -89,10 +89,23 @@ pub fn hex_to_buffer<'de, D>(deserializer: D) -> std::result::Result, D: #[cfg(feature = "serde")] /// Serializes `buffer` to a lowercase base64 string. pub fn buffer_to_base64(buffer: &T, serializer: S) -> std::result::Result - where T: AsRef<[u8]>, - S: serde::Serializer +where + T: AsRef<[u8]>, + S: serde::Serializer { - serializer.serialize_str(&base64engine.encode(&buffer)) + serializer.serialize_str(&base64engine.encode(&buffer)) +} + +#[cfg(feature = "serde")] +/// Serializes `buffer` (Option) to a lowecase base64 Option. +pub fn option_buffer_to_base64(buffer: &Option>, serializer: S) -> std::result::Result +where + S: serde::Serializer +{ + match buffer { + Some(buffer) => buffer_to_base64(&buffer, serializer), + None => serializer.serialize_none(), + } } #[cfg(feature = "serde")] @@ -100,6 +113,6 @@ pub fn buffer_to_base64(buffer: &T, serializer: S) -> std::result::Result< pub fn base64_to_buffer<'de, D>(deserializer: D) -> std::result::Result, D::Error> where D: serde::Deserializer<'de> { - use serde::de::Error; - String::deserialize(deserializer).and_then(|string| base64engine.decode(&string).map_err(|err| Error::custom(err.to_string()))) + use serde::de::Error; + String::deserialize(deserializer).and_then(|string| base64engine.decode(&string).map_err(|err| Error::custom(err.to_string()))) } \ No newline at end of file