From dc6428cd4633aab9f49ff2408921eed1461b714d Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Thu, 19 Dec 2024 14:51:52 -0800 Subject: [PATCH] impl Display and From string for ContentType --- xmtp_mls/src/groups/mls_sync.rs | 2 +- xmtp_mls/src/groups/mod.rs | 2 +- .../storage/encrypted_store/group_message.rs | 28 +++++++++++-------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/xmtp_mls/src/groups/mls_sync.rs b/xmtp_mls/src/groups/mls_sync.rs index b1a60ec87..4550abfc3 100644 --- a/xmtp_mls/src/groups/mls_sync.rs +++ b/xmtp_mls/src/groups/mls_sync.rs @@ -967,7 +967,7 @@ where sender_installation_id, sender_inbox_id, delivery_status: DeliveryStatus::Published, - content_type: ContentType::from_string(&content_type.type_id), + content_type: content_type.type_id.into(), version_major: content_type.version_major as i32, version_minor: content_type.version_minor as i32, authority_id: content_type.authority_id.to_string(), diff --git a/xmtp_mls/src/groups/mod.rs b/xmtp_mls/src/groups/mod.rs index 1431925b5..38ca46b01 100644 --- a/xmtp_mls/src/groups/mod.rs +++ b/xmtp_mls/src/groups/mod.rs @@ -748,7 +748,7 @@ impl MlsGroup { }; QueryableContentFields { - content_type: ContentType::from_string(&content_type_id.type_id), + content_type: content_type_id.type_id.into(), version_major: content_type_id.version_major as i32, version_minor: content_type_id.version_minor as i32, authority_id: content_type_id.authority_id.to_string(), diff --git a/xmtp_mls/src/storage/encrypted_store/group_message.rs b/xmtp_mls/src/storage/encrypted_store/group_message.rs index a452ecc3c..5c75c3a79 100644 --- a/xmtp_mls/src/storage/encrypted_store/group_message.rs +++ b/xmtp_mls/src/storage/encrypted_store/group_message.rs @@ -96,22 +96,26 @@ pub enum ContentType { GroupUpdated = 3, } -impl ContentType { - pub fn from_string(type_id: &str) -> Self { - match type_id { - text::TextCodec::TYPE_ID => Self::Text, - membership_change::GroupMembershipChangeCodec::TYPE_ID => Self::GroupMembershipChange, - group_updated::GroupUpdatedCodec::TYPE_ID => Self::GroupUpdated, - _ => Self::Unknown, - } - } - - pub fn to_string(&self) -> &'static str { - match self { +impl std::fmt::Display for ContentType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let as_string = match self { Self::Unknown => "unknown", Self::Text => text::TextCodec::TYPE_ID, Self::GroupMembershipChange => membership_change::GroupMembershipChangeCodec::TYPE_ID, Self::GroupUpdated => group_updated::GroupUpdatedCodec::TYPE_ID, + }; + + write!(f, "{}", as_string) + } +} + +impl From for ContentType { + fn from(type_id: String) -> Self { + match type_id.as_str() { + text::TextCodec::TYPE_ID => Self::Text, + membership_change::GroupMembershipChangeCodec::TYPE_ID => Self::GroupMembershipChange, + group_updated::GroupUpdatedCodec::TYPE_ID => Self::GroupUpdated, + _ => Self::Unknown, } } }