From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 999711FF144 for ; Tue, 24 Mar 2026 13:57:18 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id ED6E61024A; Tue, 24 Mar 2026 13:57:38 +0100 (CET) Date: Tue, 24 Mar 2026 13:57:34 +0100 From: Wolfgang Bumiller To: Christoph Heiss Subject: Re: [PATCH proxmox v2 2/8] serde: add base64 module for byte arrays Message-ID: References: <20260213143601.1424613-1-c.heiss@proxmox.com> <20260213143601.1424613-3-c.heiss@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260213143601.1424613-3-c.heiss@proxmox.com> X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1774357008204 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.084 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 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. 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: UQT6JVJHZ4MA6FIRZMN442J3OX4WTL77 X-Message-ID-Hash: UQT6JVJHZ4MA6FIRZMN442J3OX4WTL77 X-MailFrom: w.bumiller@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 CC: pve-devel@lists.proxmox.com X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Fri, Feb 13, 2026 at 03:35:55PM +0100, Christoph Heiss wrote: > 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| { Something to think about (not necessarily act on just yet...): The `proxmox-base64` crate contains commented-out `decode_slice` functions, which I mainly left out because we had not needed them before and I wasn't sure whether they need some bike shedding (eg. `decode_*to*_slice?)` or, they could technically also accept a `MaybeUninit<[u8]>` for convenience which could allow you to do something like let mut data = MaybeUninit::<[u8; N]>::uninit(); let size = proxmox_base64::decode_to_uninit(&mut data)?; check_that(size == N); Ok(data.assume_init()) Just not sure how much of an API surface we want the base64 crate to have in the first place... (that mainly depends on how much of a headache the future base64 crate APIs are going to be (probably very painful...)) > + 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 > > > > >