From 985f911ba23322f8947dc027b1ea1286d243a472 Mon Sep 17 00:00:00 2001 From: Flavio Castelli Date: Mon, 8 Apr 2024 08:41:00 +0200 Subject: [PATCH] fix: allow kwctl to build using latest version of this crate Previously, the `Serialize` and `Deserialize` traits have been removed from the `CallbackRequestType`. That was done because the `tokio::time::Instant` trait doesn't derive them. I initially though we didn't need these traits to be provided by this object. That proved true because I was working only on Policy Server, which doesn't require them. However, kwctl requires them to save and restore context aware sessions. This commit introduces custom serializer and deserializer functions for `tokio::time::Instant`, making possible to reintroduce the `Serialize` and `Deserialize` traits. This fixes the compilation of kwctl. Signed-off-by: Flavio Castelli --- src/callback_requests.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/callback_requests.rs b/src/callback_requests.rs index b4782d59..166a634b 100644 --- a/src/callback_requests.rs +++ b/src/callback_requests.rs @@ -3,6 +3,7 @@ use kubewarden_policy_sdk::host_capabilities::{ verification::{KeylessInfo, KeylessPrefixInfo}, SigstoreVerificationInputV1, SigstoreVerificationInputV2, }; +use serde::{Deserialize, Serialize}; use std::collections::HashMap; use tokio::{sync::oneshot, time::Instant}; @@ -25,7 +26,7 @@ pub struct CallbackRequest { /// Describes the different kinds of request a waPC guest can make to /// our host. -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] pub enum CallbackRequestType { /// Require the computation of the manifest digest of an OCI object (be /// it an image or anything else that can be stored into an OCI registry) @@ -190,10 +191,38 @@ pub enum CallbackRequestType { /// Defaults to everything if `None` field_selector: Option, /// The instant in time to compare the last change of the resources + #[serde(with = "tokio_instant_serializer")] since: Instant, }, } +mod tokio_instant_serializer { + use serde::de::Error; + use serde::{Deserialize, Deserializer, Serialize, Serializer}; + use std::time::Duration; + use tokio::time::Instant; + + pub fn serialize(instant: &Instant, serializer: S) -> Result + where + S: Serializer, + { + let duration = instant.elapsed(); + duration.serialize(serializer) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let duration = Duration::deserialize(deserializer)?; + let now = Instant::now(); + let instant = now + .checked_sub(duration) + .ok_or_else(|| Error::custom("Error checked_sub"))?; + Ok(instant) + } +} + impl From for CallbackRequestType { fn from(val: SigstoreVerificationInputV2) -> Self { match val {