diff --git a/rust/src/events/mod.rs b/rust/src/events/mod.rs index 64476a9c7b..d065f07228 100644 --- a/rust/src/events/mod.rs +++ b/rust/src/events/mod.rs @@ -356,19 +356,19 @@ struct EventCommonFields { content: JsonObject, hashes: HashMap, origin_server_ts: i64, - sender: String, + sender: Box, #[serde(skip_serializing_if = "Option::is_none")] - state_key: Option, + state_key: Option>, #[serde(rename = "type")] - type_: String, + type_: Box, - room_id: Option, + room_id: Option>, unsigned: JsonObject, signatures: Signatures, #[serde(flatten)] - other_fields: HashMap, + other_fields: HashMap, serde_json::Value>, } #[pyclass] @@ -415,6 +415,67 @@ impl Event { // ... } } + + #[getter] + fn content(&self) -> PyResult { + match &self.inner { + EventFormatEnum::V3(format) => Ok(format.common_fields.content.clone()), + // ... + } + } + + #[getter] + fn hashes(&self) -> PyResult<&HashMap> { + match &self.inner { + EventFormatEnum::V3(format) => Ok(&format.common_fields.hashes), + // ... + } + } + + #[getter] + fn origin_server_ts(&self) -> PyResult { + match &self.inner { + EventFormatEnum::V3(format) => Ok(format.common_fields.origin_server_ts), + // ... + } + } + + #[getter] + fn sender(&self) -> PyResult<&str> { + match &self.inner { + EventFormatEnum::V3(format) => Ok(&format.common_fields.sender), + // ... + } + } + + #[getter] + fn state_key(&self) -> PyResult<&str> { + let state_key = match &self.inner { + EventFormatEnum::V3(format) => &format.common_fields.state_key, + // ... + }; + + let Some(state_key) = state_key.as_deref() else { + return Err(PyKeyError::new_err("state_key")); + }; + Ok(state_key) + } + + #[getter] + fn r#type(&self) -> PyResult<&str> { + match &self.inner { + EventFormatEnum::V3(format) => Ok(&format.common_fields.type_), + // ... + } + } + + #[getter] + fn unsigned(&self) -> PyResult { + match &self.inner { + EventFormatEnum::V3(format) => Ok(format.common_fields.unsigned.clone()), + // ... + } + } } enum EventFormatEnum { @@ -448,11 +509,11 @@ mod tests { let event: EventFormatV3Container = serde_json::from_str(json).unwrap(); let parsed_value = serde_json::to_value(&event).unwrap(); - assert_eq!(event.common_fields.type_, "m.room.create".to_string()); + assert_eq!(&*event.common_fields.type_, "m.room.create"); assert_eq!( - event.common_fields.room_id, - Some("!qVoJSympOqdUQRUfiC:localhost:8800".to_string()) + event.common_fields.room_id.as_deref(), + Some("!qVoJSympOqdUQRUfiC:localhost:8800") ); assert_eq!(event_value, parsed_value);