From 4da3f363d14000eb43f7cb881ca9ce7ea0910696 Mon Sep 17 00:00:00 2001 From: Cameron Voell <1103838+cameronvoell@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:18:45 -0800 Subject: [PATCH] Move content types to new crate (#1401) * feat: adding new xmtp_content_types crate * move content types to new crate * fix wasm bindings build --------- Co-authored-by: cameronvoell --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 2 ++ examples/cli/Cargo.toml | 1 + examples/cli/cli-client.rs | 2 +- examples/cli/serializable.rs | 8 ++------ xmtp_content_types/Cargo.toml | 16 ++++++++++++++++ .../src}/group_updated.rs | 2 +- .../mod.rs => xmtp_content_types/src/lib.rs | 9 ++++++++- .../src}/membership_change.rs | 2 +- xmtp_content_types/src/test_utils.rs | 15 +++++++++++++++ .../codecs => xmtp_content_types/src}/text.rs | 2 +- xmtp_mls/Cargo.toml | 1 + xmtp_mls/src/groups/mls_sync.rs | 4 ++-- xmtp_mls/src/groups/mod.rs | 2 +- xmtp_mls/src/lib.rs | 1 - 15 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 xmtp_content_types/Cargo.toml rename {xmtp_mls/src/codecs => xmtp_content_types/src}/group_updated.rs (98%) rename xmtp_mls/src/codecs/mod.rs => xmtp_content_types/src/lib.rs (82%) rename {xmtp_mls/src/codecs => xmtp_content_types/src}/membership_change.rs (98%) create mode 100644 xmtp_content_types/src/test_utils.rs rename {xmtp_mls/src/codecs => xmtp_content_types/src}/text.rs (97%) diff --git a/Cargo.lock b/Cargo.lock index 17a96844d..96b598356 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7294,12 +7294,24 @@ dependencies = [ "valuable", "valuable-serde", "xmtp_api_grpc", + "xmtp_content_types", "xmtp_cryptography", "xmtp_id", "xmtp_mls", "xmtp_proto", ] +[[package]] +name = "xmtp_content_types" +version = "0.1.0" +dependencies = [ + "prost", + "rand", + "thiserror 2.0.6", + "tonic", + "xmtp_proto", +] + [[package]] name = "xmtp_cryptography" version = "0.1.0" @@ -7422,6 +7434,7 @@ dependencies = [ "web-time", "xmtp_api_grpc", "xmtp_api_http", + "xmtp_content_types", "xmtp_cryptography", "xmtp_id", "xmtp_proto", diff --git a/Cargo.toml b/Cargo.toml index 0226f5a35..c72428bfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ members = [ "bindings_ffi", "xtask", "xmtp_debug", + "xmtp_content_types", ] # Make the feature resolver explicit. @@ -102,6 +103,7 @@ xmtp_cryptography = { path = "xmtp_cryptography" } xmtp_id = { path = "xmtp_id" } xmtp_mls = { path = "xmtp_mls" } xmtp_proto = { path = "xmtp_proto" } +xmtp_content_types = { path = "xmtp_content_types" } [profile.dev] # Disabling debug info speeds up builds a bunch, diff --git a/examples/cli/Cargo.toml b/examples/cli/Cargo.toml index 9d837d9a1..830931bce 100644 --- a/examples/cli/Cargo.toml +++ b/examples/cli/Cargo.toml @@ -39,6 +39,7 @@ valuable = { version = "0.1", features = ["derive"] } valuable-serde = "0.1" xmtp_api_grpc = { path = "../../xmtp_api_grpc" } xmtp_cryptography = { path = "../../xmtp_cryptography" } +xmtp_content_types = { path = "../../xmtp_content_types" } xmtp_id = { path = "../../xmtp_id" } xmtp_mls = { path = "../../xmtp_mls" } xmtp_proto = { path = "../../xmtp_proto", features = ["proto_full"] } diff --git a/examples/cli/cli-client.rs b/examples/cli/cli-client.rs index a9513bec5..ec285b1d9 100755 --- a/examples/cli/cli-client.rs +++ b/examples/cli/cli-client.rs @@ -32,6 +32,7 @@ use tracing_subscriber::{ use valuable::Valuable; use xmtp_api_grpc::grpc_api_helper::Client as ClientV3; use xmtp_api_grpc::replication_client::ClientV4; +use xmtp_content_types::{text::TextCodec, ContentCodec}; use xmtp_cryptography::{ signature::{RecoverableSignature, SignatureError}, utils::rng, @@ -47,7 +48,6 @@ use xmtp_mls::XmtpApi; use xmtp_mls::{ builder::ClientBuilderError, client::ClientError, - codecs::{text::TextCodec, ContentCodec}, groups::{device_sync::MessageHistoryUrls, GroupMetadataOptions}, identity::IdentityStrategy, storage::{ diff --git a/examples/cli/serializable.rs b/examples/cli/serializable.rs index cbc3190a6..c6ee793ce 100644 --- a/examples/cli/serializable.rs +++ b/examples/cli/serializable.rs @@ -1,12 +1,8 @@ use prost::Message; use serde::Serialize; use valuable::Valuable; -use xmtp_mls::{ - codecs::{text::TextCodec, ContentCodec}, - groups::MlsGroup, - storage::group_message::StoredGroupMessage, - XmtpApi, -}; +use xmtp_content_types::{text::TextCodec, ContentCodec}; +use xmtp_mls::{groups::MlsGroup, storage::group_message::StoredGroupMessage, XmtpApi}; use xmtp_proto::xmtp::mls::message_contents::EncodedContent; #[derive(Serialize, Debug, Valuable)] diff --git a/xmtp_content_types/Cargo.toml b/xmtp_content_types/Cargo.toml new file mode 100644 index 000000000..4d0a2b2ee --- /dev/null +++ b/xmtp_content_types/Cargo.toml @@ -0,0 +1,16 @@ +[package] +edition = "2021" +name = "xmtp_content_types" +version.workspace = true +license.workspace = true + +[dependencies] +thiserror = { workspace = true } +prost = { workspace = true, features = ["prost-derive"] } +rand = { workspace = true } + +# XMTP/Local +xmtp_proto = { workspace = true, features = ["convert"] } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +tonic = { version = "0.12", features = ["transport"] } diff --git a/xmtp_mls/src/codecs/group_updated.rs b/xmtp_content_types/src/group_updated.rs similarity index 98% rename from xmtp_mls/src/codecs/group_updated.rs rename to xmtp_content_types/src/group_updated.rs index 09ebea595..7aa797ee0 100644 --- a/xmtp_mls/src/codecs/group_updated.rs +++ b/xmtp_content_types/src/group_updated.rs @@ -52,7 +52,7 @@ pub(crate) mod tests { use xmtp_proto::xmtp::mls::message_contents::{group_updated::Inbox, GroupUpdated}; - use crate::utils::test::rand_string; + use crate::test_utils::rand_string; use super::*; diff --git a/xmtp_mls/src/codecs/mod.rs b/xmtp_content_types/src/lib.rs similarity index 82% rename from xmtp_mls/src/codecs/mod.rs rename to xmtp_content_types/src/lib.rs index dc44e7cf4..b882891a1 100644 --- a/xmtp_mls/src/codecs/mod.rs +++ b/xmtp_content_types/src/lib.rs @@ -1,11 +1,18 @@ pub mod group_updated; pub mod membership_change; +#[cfg(test)] +mod test_utils; pub mod text; use thiserror::Error; - use xmtp_proto::xmtp::mls::message_contents::{ContentTypeId, EncodedContent}; +pub enum ContentType { + GroupMembershipChange, + GroupUpdated, + Text, +} + #[derive(Debug, Error)] pub enum CodecError { #[error("encode error {0}")] diff --git a/xmtp_mls/src/codecs/membership_change.rs b/xmtp_content_types/src/membership_change.rs similarity index 98% rename from xmtp_mls/src/codecs/membership_change.rs rename to xmtp_content_types/src/membership_change.rs index e64f50df6..b83483806 100644 --- a/xmtp_mls/src/codecs/membership_change.rs +++ b/xmtp_content_types/src/membership_change.rs @@ -54,7 +54,7 @@ pub(crate) mod tests { use xmtp_proto::xmtp::mls::message_contents::MembershipChange; - use crate::utils::test::{rand_string, rand_vec}; + use crate::test_utils::{rand_string, rand_vec}; use super::*; diff --git a/xmtp_content_types/src/test_utils.rs b/xmtp_content_types/src/test_utils.rs new file mode 100644 index 000000000..dead0c7e6 --- /dev/null +++ b/xmtp_content_types/src/test_utils.rs @@ -0,0 +1,15 @@ +#[cfg(test)] +use rand::{ + distributions::{Alphanumeric, DistString}, + Rng, +}; + +#[cfg(test)] +pub(crate) fn rand_string() -> String { + Alphanumeric.sample_string(&mut rand::thread_rng(), 24) +} + +#[cfg(test)] +pub(crate) fn rand_vec() -> Vec { + rand::thread_rng().gen::<[u8; 24]>().to_vec() +} diff --git a/xmtp_mls/src/codecs/text.rs b/xmtp_content_types/src/text.rs similarity index 97% rename from xmtp_mls/src/codecs/text.rs rename to xmtp_content_types/src/text.rs index d016f1513..124fb7831 100644 --- a/xmtp_mls/src/codecs/text.rs +++ b/xmtp_content_types/src/text.rs @@ -58,7 +58,7 @@ pub(crate) mod tests { #[cfg(target_arch = "wasm32")] wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_dedicated_worker); - use crate::codecs::{text::TextCodec, ContentCodec}; + use crate::{text::TextCodec, ContentCodec}; #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), test)] diff --git a/xmtp_mls/Cargo.toml b/xmtp_mls/Cargo.toml index c1f8b4c72..e9d16676d 100644 --- a/xmtp_mls/Cargo.toml +++ b/xmtp_mls/Cargo.toml @@ -73,6 +73,7 @@ web-time.workspace = true zeroize.workspace = true # XMTP/Local +xmtp_content_types = { path = "../xmtp_content_types" } xmtp_cryptography = { workspace = true } xmtp_id = { path = "../xmtp_id" } xmtp_proto = { workspace = true, features = ["convert"] } diff --git a/xmtp_mls/src/groups/mls_sync.rs b/xmtp_mls/src/groups/mls_sync.rs index d1131d40c..a613551f8 100644 --- a/xmtp_mls/src/groups/mls_sync.rs +++ b/xmtp_mls/src/groups/mls_sync.rs @@ -9,7 +9,6 @@ use super::{ GroupError, HmacKey, IntentError, MlsGroup, ScopedGroupClient, }; use crate::{ - codecs::{group_updated::GroupUpdatedCodec, ContentCodec}, configuration::{ GRPC_DATA_LIMIT, HMAC_SALT, MAX_GROUP_SIZE, MAX_INTENT_PUBLISH_ATTEMPTS, MAX_PAST_EPOCHS, SYNC_UPDATE_INSTALLATIONS_INTERVAL_NS, @@ -64,6 +63,7 @@ use std::{ ops::RangeInclusive, }; use thiserror::Error; +use xmtp_content_types::{group_updated::GroupUpdatedCodec, CodecError, ContentCodec}; use xmtp_id::{InboxId, InboxIdRef}; use xmtp_proto::xmtp::mls::{ api::v1::{ @@ -122,7 +122,7 @@ pub enum GroupMessageProcessingError { #[error(transparent)] Intent(#[from] IntentError), #[error(transparent)] - Codec(#[from] crate::codecs::CodecError), + Codec(#[from] CodecError), #[error("wrong credential type")] WrongCredentialType(#[from] BasicCredentialError), #[error(transparent)] diff --git a/xmtp_mls/src/groups/mod.rs b/xmtp_mls/src/groups/mod.rs index 360245fd1..c8e19e933 100644 --- a/xmtp_mls/src/groups/mod.rs +++ b/xmtp_mls/src/groups/mod.rs @@ -1609,6 +1609,7 @@ pub(crate) mod tests { use openmls::prelude::Member; use prost::Message; use std::sync::Arc; + use xmtp_content_types::{group_updated::GroupUpdatedCodec, ContentCodec}; use xmtp_cryptography::utils::generate_local_wallet; use xmtp_proto::xmtp::mls::api::v1::group_message::Version; use xmtp_proto::xmtp::mls::message_contents::EncodedContent; @@ -1616,7 +1617,6 @@ pub(crate) mod tests { use crate::{ assert_err, builder::ClientBuilder, - codecs::{group_updated::GroupUpdatedCodec, ContentCodec}, groups::{ build_dm_protected_metadata_extension, build_mutable_metadata_extension_default, build_protected_metadata_extension, diff --git a/xmtp_mls/src/lib.rs b/xmtp_mls/src/lib.rs index fb21ba10b..71d2ede3f 100644 --- a/xmtp_mls/src/lib.rs +++ b/xmtp_mls/src/lib.rs @@ -4,7 +4,6 @@ pub mod api; pub mod builder; pub mod client; -pub mod codecs; pub mod configuration; pub mod groups; mod hpke;