diff --git a/xmtp_content_types/Cargo.toml b/xmtp_content_types/Cargo.toml index 52fa200b5..09d7e907a 100644 --- a/xmtp_content_types/Cargo.toml +++ b/xmtp_content_types/Cargo.toml @@ -4,11 +4,18 @@ name = "xmtp_content_types" version.workspace = true license.workspace = true +targets = [ + "x86_64-unknown-linux-gnu", + "wasm32-unknown-unknown", + "aarch64-apple-darwin", +] + [dependencies] thiserror = { workspace = true } prost = { workspace = true, features = ["prost-derive"] } rand = { workspace = true } serde = { workspace = true } +diesel = { workspace = true } # XMTP/Local xmtp_proto = { workspace = true, features = ["convert"] } @@ -19,4 +26,3 @@ xmtp_common = { workspace = true, features = ['test-utils'] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tonic = { version = "0.12", features = ["transport"] } -diesel = { workspace = true } diff --git a/xmtp_content_types/src/lib.rs b/xmtp_content_types/src/lib.rs index c3453908a..3bafb2a03 100644 --- a/xmtp_content_types/src/lib.rs +++ b/xmtp_content_types/src/lib.rs @@ -2,76 +2,9 @@ pub mod group_updated; pub mod membership_change; pub mod text; -use diesel::{ - backend::Backend, - deserialize::{self, FromSql, FromSqlRow}, - expression::AsExpression, - serialize::{self, IsNull, Output, ToSql}, - sql_types::Integer, - sqlite::Sqlite, -}; -use serde::{Deserialize, Serialize}; use thiserror::Error; use xmtp_proto::xmtp::mls::message_contents::{ContentTypeId, EncodedContent}; -/// ContentType and their corresponding string representation -/// are derived from the `ContentTypeId` enum in the xmtp-proto crate -/// that each content type in this crate establishes for itself -#[repr(i32)] -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, FromSqlRow, AsExpression)] -#[diesel(sql_type = diesel::sql_types::Integer)] -pub enum ContentType { - Unknown = 0, - Text = 1, - GroupMembershipChange = 2, - 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 { - Self::Unknown => "unknown", - Self::Text => text::TextCodec::TYPE_ID, - Self::GroupMembershipChange => membership_change::GroupMembershipChangeCodec::TYPE_ID, - Self::GroupUpdated => group_updated::GroupUpdatedCodec::TYPE_ID, - } - } -} - -impl ToSql for ContentType -where - i32: ToSql, -{ - fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result { - out.set_value(*self as i32); - Ok(IsNull::No) - } -} - -impl FromSql for ContentType -where - i32: FromSql, -{ - fn from_sql(bytes: ::RawValue<'_>) -> deserialize::Result { - match i32::from_sql(bytes)? { - 0 => Ok(ContentType::Unknown), - 1 => Ok(ContentType::Text), - 2 => Ok(ContentType::GroupMembershipChange), - 3 => Ok(ContentType::GroupUpdated), - x => Err(format!("Unrecognized variant {}", x).into()), - } - } -} - #[derive(Debug, Error)] pub enum CodecError { #[error("encode error {0}")] diff --git a/xmtp_mls/src/storage/encrypted_store/group_message.rs b/xmtp_mls/src/storage/encrypted_store/group_message.rs index 75ee41213..42d1ab07a 100644 --- a/xmtp_mls/src/storage/encrypted_store/group_message.rs +++ b/xmtp_mls/src/storage/encrypted_store/group_message.rs @@ -7,7 +7,7 @@ use diesel::{ sql_types::Integer, }; use serde::{Deserialize, Serialize}; -use xmtp_content_types::ContentType; +use xmtp_content_types::{group_updated, membership_change, text, ContentType}; use super::{ db_connection::DbConnection, @@ -86,6 +86,61 @@ where } } +#[repr(i32)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, FromSqlRow, AsExpression)] +#[diesel(sql_type = diesel::sql_types::Integer)] +pub enum ContentType { + Unknown = 0, + Text = 1, + GroupMembershipChange = 2, + 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 { + Self::Unknown => "unknown", + Self::Text => text::TextCodec::TYPE_ID, + Self::GroupMembershipChange => membership_change::GroupMembershipChangeCodec::TYPE_ID, + Self::GroupUpdated => group_updated::GroupUpdatedCodec::TYPE_ID, + } + } +} + +impl ToSql for ContentType +where + i32: ToSql, +{ + fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result { + out.set_value(*self as i32); + Ok(IsNull::No) + } +} + +impl FromSql for ContentType +where + i32: FromSql, +{ + fn from_sql(bytes: ::RawValue<'_>) -> deserialize::Result { + match i32::from_sql(bytes)? { + 0 => Ok(ContentType::Unknown), + 1 => Ok(ContentType::Text), + 2 => Ok(ContentType::GroupMembershipChange), + 3 => Ok(ContentType::GroupUpdated), + x => Err(format!("Unrecognized variant {}", x).into()), + } + } +} + #[repr(i32)] #[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq, FromSqlRow, AsExpression)] #[diesel(sql_type = Integer)]