Small refactor to ensure we call 'validate' for all formats

This commit is contained in:
Erik Johnston
2026-05-21 14:14:29 +01:00
parent 69c8d226b8
commit e82ebe17b2
3 changed files with 19 additions and 9 deletions
+10
View File
@@ -68,6 +68,7 @@
use std::{collections::HashMap, sync::Arc};
use anyhow::Error;
use serde::{Deserialize, Serialize};
use crate::events::{json_object::JsonObject, signatures::Signatures, unsigned::Unsigned};
@@ -128,6 +129,15 @@ impl FormattedEvent {
common_fields: Arc::clone(&self.common_fields),
}
}
pub fn validate(&self) -> Result<(), Error> {
match &*self.specific_fields {
EventFormatEnum::V1(format) => format.validate(&self.common_fields),
EventFormatEnum::V2V3(format) => format.validate(&self.common_fields),
EventFormatEnum::V4(format) => format.validate(&self.common_fields),
EventFormatEnum::VMSC4242(format) => format.validate(&self.common_fields),
}
}
}
impl<E> FormattedEvent<E>
+7
View File
@@ -25,8 +25,11 @@
use std::{collections::HashMap, sync::Arc};
use anyhow::Error;
use serde::{Deserialize, Serialize};
use crate::events::formats::EventCommonFields;
/// Version-specific fields for room versions 1 and 2.
#[derive(Serialize, Deserialize)]
pub struct EventFormatV1 {
@@ -37,6 +40,10 @@ pub struct EventFormatV1 {
}
impl EventFormatV1 {
pub fn validate(&self, _common_fields: &EventCommonFields) -> Result<(), Error> {
Ok(())
}
pub fn auth_event_ids(&self) -> Vec<String> {
self.auth_events.iter().map(|(id, _)| id.clone()).collect()
}
+2 -9
View File
@@ -525,23 +525,14 @@ fn depythonize_event_dict(
}
EventFormatVersions::ROOM_V3 | EventFormatVersions::ROOM_V4_PLUS => {
let event_format: FormattedEvent<EventFormatV2V3> = depythonize(event_dict)?;
event_format
.specific_fields
.validate(&event_format.common_fields)?;
event_format.into()
}
EventFormatVersions::ROOM_V11_HYDRA_PLUS => {
let event_format: FormattedEvent<EventFormatV4> = depythonize(event_dict)?;
event_format
.specific_fields
.validate(&event_format.common_fields)?;
event_format.into()
}
EventFormatVersions::ROOM_VMSC4242 => {
let event_format: FormattedEvent<EventFormatVMSC4242> = depythonize(event_dict)?;
event_format
.specific_fields
.validate(&event_format.common_fields)?;
event_format.into()
}
_ => {
@@ -552,6 +543,8 @@ fn depythonize_event_dict(
}
};
formatted_event.validate()?;
Ok(formatted_event)
}