Skip to content

Commit

Permalink
move content type enum to encrypted_store
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Dec 19, 2024
1 parent 0c3232f commit 158f543
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 69 deletions.
8 changes: 7 additions & 1 deletion xmtp_content_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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 }
67 changes: 0 additions & 67 deletions xmtp_content_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Sqlite> for ContentType
where
i32: ToSql<Integer, Sqlite>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
out.set_value(*self as i32);
Ok(IsNull::No)
}
}

impl FromSql<Integer, Sqlite> for ContentType
where
i32: FromSql<Integer, Sqlite>,
{
fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
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}")]
Expand Down
57 changes: 56 additions & 1 deletion xmtp_mls/src/storage/encrypted_store/group_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Integer, Sqlite> for ContentType
where
i32: ToSql<Integer, Sqlite>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
out.set_value(*self as i32);
Ok(IsNull::No)
}
}

impl FromSql<Integer, Sqlite> for ContentType
where
i32: FromSql<Integer, Sqlite>,
{
fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
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)]
Expand Down

0 comments on commit 158f543

Please sign in to comment.