From 35d8bdd5a1a992660e8808875e9e4a995ee47070 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 12 Sep 2022 12:03:29 +0200 Subject: [PATCH] events: Move StateUnsigned::_from_parts into a trait --- crates/ruma-common/src/events.rs | 2 +- crates/ruma-common/src/events/unsigned.rs | 10 +++++++--- crates/ruma-macros/src/events/event.rs | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/ruma-common/src/events.rs b/crates/ruma-common/src/events.rs index 47eb88cb..11991304 100644 --- a/crates/ruma-common/src/events.rs +++ b/crates/ruma-common/src/events.rs @@ -174,7 +174,7 @@ pub use self::{ kinds::*, relation::Relations, state_key::EmptyStateKey, - unsigned::{MessageLikeUnsigned, RedactedUnsigned, StateUnsigned}, + unsigned::{MessageLikeUnsigned, RedactedUnsigned, StateUnsigned, StateUnsignedFromParts}, }; /// Trait to define the behavior of redacting an event. diff --git a/crates/ruma-common/src/events/unsigned.rs b/crates/ruma-common/src/events/unsigned.rs index ec85e42c..55085a1d 100644 --- a/crates/ruma-common/src/events/unsigned.rs +++ b/crates/ruma-common/src/events/unsigned.rs @@ -101,10 +101,14 @@ impl CanBeEmpty for StateUnsigned { /// Helper functions for proc-macro code. /// -/// Needs to be public for UI tests. +/// Needs to be public for state events defined outside ruma-common. #[doc(hidden)] -impl StateUnsigned { - pub fn _from_parts(event_type: &str, object: &RawJsonValue) -> serde_json::Result { +pub trait StateUnsignedFromParts: Sized { + fn _from_parts(event_type: &str, object: &RawJsonValue) -> serde_json::Result; +} + +impl StateUnsignedFromParts for StateUnsigned { + fn _from_parts(event_type: &str, object: &RawJsonValue) -> serde_json::Result { #[derive(Deserialize)] #[serde(bound = "")] // Disable default C: Deserialize bound struct WithRawPrevContent { diff --git a/crates/ruma-macros/src/events/event.rs b/crates/ruma-macros/src/events/event.rs index 1d165556..98693296 100644 --- a/crates/ruma-macros/src/events/event.rs +++ b/crates/ruma-macros/src/events/event.rs @@ -228,8 +228,10 @@ fn expand_deserialize_event( if has_prev_content(kind, var) { quote! { let unsigned = unsigned.map(|json| { - #ruma_common::events::StateUnsigned::_from_parts(&event_type, &json) - .map_err(#serde::de::Error::custom) + #ruma_common::events::StateUnsignedFromParts::_from_parts( + &event_type, + &json, + ).map_err(#serde::de::Error::custom) }).transpose()?.unwrap_or_default(); } } else {