events: Move StateUnsigned::_from_parts into a trait

This commit is contained in:
Jonas Platte 2022-09-12 12:03:29 +02:00
parent 9b5b5e7797
commit 35d8bdd5a1
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
3 changed files with 12 additions and 6 deletions

View File

@ -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.

View File

@ -101,10 +101,14 @@ impl<C: StateEventContent> CanBeEmpty for StateUnsigned<C> {
/// 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<C: StateEventContent> StateUnsigned<C> {
pub fn _from_parts(event_type: &str, object: &RawJsonValue) -> serde_json::Result<Self> {
pub trait StateUnsignedFromParts: Sized {
fn _from_parts(event_type: &str, object: &RawJsonValue) -> serde_json::Result<Self>;
}
impl<C: StateEventContent> StateUnsignedFromParts for StateUnsigned<C> {
fn _from_parts(event_type: &str, object: &RawJsonValue) -> serde_json::Result<Self> {
#[derive(Deserialize)]
#[serde(bound = "")] // Disable default C: Deserialize bound
struct WithRawPrevContent<C> {

View File

@ -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 {