From 224d52e9a2b8363289927e14d9f05f1a0992df49 Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Wed, 20 Mar 2024 12:29:45 +0000 Subject: [PATCH] Revert "Support deserializing events of previous and current version (#61)" This reverts commit 3fcc99a7aebba7d8668b3bbb108de435133b2c52. --- Cargo.lock | 2 - rs/canister/api/Cargo.toml | 4 - rs/canister/api/src/lib.rs | 158 +------------------------------------ 3 files changed, 2 insertions(+), 162 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 41251b3..aa4e8d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -499,10 +499,8 @@ name = "event_store_canister" version = "0.1.0" dependencies = [ "candid", - "rmp-serde", "serde", "serde_bytes", - "test-case", ] [[package]] diff --git a/rs/canister/api/Cargo.toml b/rs/canister/api/Cargo.toml index 1d97d20..b01975e 100644 --- a/rs/canister/api/Cargo.toml +++ b/rs/canister/api/Cargo.toml @@ -9,7 +9,3 @@ edition = "2021" candid.workspace = true serde.workspace = true serde_bytes.workspace = true - -[dev-dependencies] -rmp-serde = "1.1.2" -test-case.workspace = true diff --git a/rs/canister/api/src/lib.rs b/rs/canister/api/src/lib.rs index 411b775..f3083cc 100644 --- a/rs/canister/api/src/lib.rs +++ b/rs/canister/api/src/lib.rs @@ -1,10 +1,5 @@ use candid::{CandidType, Deserialize}; -use serde::__private::from_utf8_lossy; -use serde::de::{EnumAccess, Error, Unexpected, VariantAccess}; -use serde::{Deserializer, Serialize}; -use std::fmt; -use std::fmt::Formatter; -use std::marker::PhantomData; +use serde::Serialize; mod lifecycle; mod queries; @@ -50,7 +45,7 @@ pub struct IndexedEvent { pub payload: Vec, } -#[derive(CandidType, Serialize, Clone, Debug)] +#[derive(CandidType, Serialize, Deserialize, Clone, Debug)] pub enum Anonymizable { Public(String), Anonymize(String), @@ -76,152 +71,3 @@ impl Anonymizable { matches!(self, Anonymizable::Public(_)) } } - -impl<'de> Deserialize<'de> for Anonymizable { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - enum Field { - Field0, - Field1, - Value(String), - } - #[doc(hidden)] - struct FieldVisitor; - impl<'de> serde::de::Visitor<'de> for FieldVisitor { - type Value = Field; - fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { - write!(formatter, "variant identifier") - } - fn visit_u64(self, value: u64) -> Result - where - E: Error, - { - match value { - 0u64 => Ok(Field::Field0), - 1u64 => Ok(Field::Field1), - _ => Err(Error::invalid_value( - Unexpected::Unsigned(value), - &"variant index 0 <= i < 2", - )), - } - } - fn visit_str(self, value: &str) -> Result - where - E: Error, - { - match value { - "Public" => Ok(Field::Field0), - "Anonymize" => Ok(Field::Field1), - value => Ok(Field::Value(value.to_string())), - } - } - fn visit_bytes(self, value: &[u8]) -> Result - where - E: Error, - { - match value { - b"Public" => Ok(Field::Field0), - b"Anonymize" => Ok(Field::Field1), - _ => { - let value = &from_utf8_lossy(value); - Err(Error::unknown_variant(value, VARIANTS)) - } - } - } - } - impl<'de> Deserialize<'de> for Field { - #[inline] - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - Deserializer::deserialize_identifier(deserializer, FieldVisitor) - } - } - #[doc(hidden)] - struct Visitor<'de> { - marker: PhantomData, - lifetime: PhantomData<&'de ()>, - } - impl<'de> serde::de::Visitor<'de> for Visitor<'de> { - type Value = Anonymizable; - fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { - Formatter::write_str(formatter, "enum Anonymizable") - } - fn visit_enum(self, data: A) -> Result - where - A: EnumAccess<'de>, - { - match EnumAccess::variant(data)? { - (Field::Field0, variant) => Result::map( - VariantAccess::newtype_variant::(variant), - Anonymizable::Public, - ), - (Field::Field1, variant) => Result::map( - VariantAccess::newtype_variant::(variant), - Anonymizable::Anonymize, - ), - (Field::Value(value), _) => Ok(Self::Value::Public(value)), - } - } - } - #[doc(hidden)] - const VARIANTS: &[&str] = &["Public", "Anonymize"]; - Deserializer::deserialize_enum( - deserializer, - "Anonymizable", - VARIANTS, - Visitor { - marker: PhantomData::, - lifetime: PhantomData, - }, - ) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use test_case::test_case; - - #[test_case(true)] - #[test_case(false)] - fn deserialization_succeeds(current_version: bool) { - let bytes = if current_version { - let value = IdempotentEvent { - idempotency_key: 1, - name: "name".to_string(), - timestamp: 2, - user: Some(Anonymizable::Public("user".to_string())), - source: Some(Anonymizable::Public("source".to_string())), - payload: vec![1, 2, 3], - }; - - rmp_serde::to_vec_named(&value).unwrap() - } else { - let value = IdempotentEventPrevious { - idempotency_key: 1, - name: "name".to_string(), - timestamp: 2, - user: Some("user".to_string()), - source: Some("source".to_string()), - payload: vec![1, 2, 3], - }; - - rmp_serde::to_vec_named(&value).unwrap() - }; - - let deserialized: IdempotentEvent = rmp_serde::from_slice(&bytes).unwrap(); - - assert_eq!(deserialized.idempotency_key, 1); - assert_eq!(deserialized.name, "name"); - assert_eq!(deserialized.timestamp, 2); - assert_eq!(deserialized.user.clone().unwrap().as_str(), "user"); - assert!(deserialized.user.clone().unwrap().is_public()); - assert_eq!(deserialized.source.clone().unwrap().as_str(), "source"); - assert!(deserialized.source.clone().unwrap().is_public()); - assert_eq!(deserialized.payload, vec![1, 2, 3]); - } -}