From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id C57421FF141 for ; Fri, 13 Feb 2026 15:35:39 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 256D065C8; Fri, 13 Feb 2026 15:36:23 +0100 (CET) From: Christoph Heiss To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox v2 2/8] serde: add base64 module for byte arrays Date: Fri, 13 Feb 2026 15:35:55 +0100 Message-ID: <20260213143601.1424613-3-c.heiss@proxmox.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260213143601.1424613-1-c.heiss@proxmox.com> References: <20260213143601.1424613-1-c.heiss@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1770993373849 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.052 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: RQGFEKFXI3LINHHEGIT4ODSK6VD6GUZI X-Message-ID-Hash: RQGFEKFXI3LINHHEGIT4ODSK6VD6GUZI X-MailFrom: c.heiss@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Allows to directly en-/decode [u8; N] to/from a base64 string, much like the already existing bytes_as_base64 allows for Vec. Signed-off-by: Christoph Heiss --- Changes v1 -> v2: * no changes proxmox-serde/src/lib.rs | 91 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/proxmox-serde/src/lib.rs b/proxmox-serde/src/lib.rs index 28c3054d..c3ec4f41 100644 --- a/proxmox-serde/src/lib.rs +++ b/proxmox-serde/src/lib.rs @@ -159,3 +159,94 @@ pub mod string_as_base64 { ::de::<'de, D>(deserializer) } } + +/// Serialize `[u8; N]` or `Option<[u8; N]>` as base64 encoded. +/// +/// If you do not need the convenience of handling both [u8; N] and Option transparently, you could +/// also use [`proxmox_base64`] directly. +/// +/// Usage example: +/// ``` +/// use serde::{Deserialize, Serialize}; +/// +/// #[derive(Debug, Deserialize, PartialEq, Serialize)] +/// struct Foo { +/// #[serde(with = "proxmox_serde::byte_array_as_base64")] +/// data: [u8; 4], +/// } +/// +/// let obj = Foo { data: [1, 2, 3, 4] }; +/// let json = serde_json::to_string(&obj).unwrap(); +/// assert_eq!(json, r#"{"data":"AQIDBA=="}"#); +/// +/// let deserialized: Foo = serde_json::from_str(&json).unwrap(); +/// assert_eq!(obj, deserialized); +/// ``` +pub mod byte_array_as_base64 { + use serde::{Deserialize, Deserializer, Serializer}; + + /// Private trait to enable `byte_array_as_base64` for `Option<[u8; N]>` in addition to `[u8; N]`. + #[doc(hidden)] + pub trait ByteArrayAsBase64: Sized { + fn ser(&self, serializer: S) -> Result; + fn de<'de, D: Deserializer<'de>>(deserializer: D) -> Result; + } + + fn finish_deserializing<'de, const N: usize, D: Deserializer<'de>>( + string: String, + ) -> Result<[u8; N], D::Error> { + use serde::de::Error; + + let vec = proxmox_base64::decode(string).map_err(|err| { + let msg = format!("base64 decode: {}", err); + Error::custom(msg) + })?; + + vec.as_slice().try_into().map_err(|_| { + let msg = format!("expected {N} bytes, got {}", vec.len()); + Error::custom(msg) + }) + } + + impl ByteArrayAsBase64 for [u8; N] { + fn ser(&self, serializer: S) -> Result { + serializer.serialize_str(&proxmox_base64::encode(self)) + } + + fn de<'de, D: Deserializer<'de>>(deserializer: D) -> Result { + finish_deserializing::<'de, N, D>(String::deserialize(deserializer)?) + } + } + + impl ByteArrayAsBase64 for Option<[u8; N]> { + fn ser(&self, serializer: S) -> Result { + match self { + Some(s) => Self::ser(&Some(*s), serializer), + None => serializer.serialize_none(), + } + } + + fn de<'de, D: Deserializer<'de>>(deserializer: D) -> Result { + match Option::::deserialize(deserializer)? { + Some(s) => Ok(Some(finish_deserializing::<'de, N, D>(s)?)), + None => Ok(None), + } + } + } + + pub fn serialize(data: &T, serializer: S) -> Result + where + S: Serializer, + T: ByteArrayAsBase64, + { + >::ser(data, serializer) + } + + pub fn deserialize<'de, const N: usize, D, T>(deserializer: D) -> Result + where + D: Deserializer<'de>, + T: ByteArrayAsBase64, + { + >::de::<'de, D>(deserializer) + } +} -- 2.52.0