From e2a851f68aacd74a248e925ab334c3cf9eabba18 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Fri, 24 Oct 2025 19:03:02 -0400 Subject: Move the VAPID public key encoding into a serde-compatible encoding module. The [Serde attribute docs][serde-attr] don't spell out that this will work, but experimentally, it looks like a module used with `#[serde(with)]` only needs to have the `encode`/`decode` functions if they're actually used, and can be "incomplete" if the missing ones are also unused in your code. That's the case here: we serialize VAPID keys, but never deserialize them. [serde-attr]: https://serde.rs/field-attrs.html#with This improves organization a bit in my view, but more importantly it also sets us up for a coming change where we _will_ start deserializing VAPID keys, and where I'd like to use the same logic: giving it its own module will make that easier to organize. --- src/vapid/event.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'src/vapid/event.rs') diff --git a/src/vapid/event.rs b/src/vapid/event.rs index af70ac2..cf3be77 100644 --- a/src/vapid/event.rs +++ b/src/vapid/event.rs @@ -1,6 +1,4 @@ -use base64::{Engine, engine::general_purpose::URL_SAFE}; use p256::ecdsa::VerifyingKey; -use serde::Serialize; use crate::event::{Instant, Sequenced}; @@ -22,7 +20,7 @@ impl Sequenced for Event { pub struct Changed { #[serde(flatten)] pub instant: Instant, - #[serde(serialize_with = "as_vapid_key")] + #[serde(with = "crate::vapid::ser::key")] pub key: VerifyingKey, } @@ -37,12 +35,3 @@ impl Sequenced for Changed { self.instant } } - -fn as_vapid_key(key: &VerifyingKey, serializer: S) -> Result -where - S: serde::Serializer, -{ - let key = key.to_sec1_bytes(); - let key = URL_SAFE.encode(key); - key.serialize(serializer) -} -- cgit v1.2.3