xmtp_content_types/
leave_request.rs1use std::collections::HashMap;
2
3use prost::Message;
4
5use super::{CodecError, ContentCodec};
6use xmtp_proto::xmtp::mls::message_contents::content_types::LeaveRequest;
7use xmtp_proto::xmtp::mls::message_contents::{ContentTypeId, EncodedContent};
8
9pub struct LeaveRequestCodec {
10 #[allow(dead_code)]
11 authenticated_note: Option<Vec<u8>>,
12}
13
14impl LeaveRequestCodec {
15 const AUTHORITY_ID: &'static str = "xmtp.org";
16 pub const TYPE_ID: &'static str = "leave_request";
17 pub const MAJOR_VERSION: u32 = 1;
18 pub const MINOR_VERSION: u32 = 0;
19}
20
21impl ContentCodec<LeaveRequest> for LeaveRequestCodec {
22 fn content_type() -> ContentTypeId {
23 ContentTypeId {
24 authority_id: LeaveRequestCodec::AUTHORITY_ID.to_string(),
25 type_id: LeaveRequestCodec::TYPE_ID.to_string(),
26 version_major: LeaveRequestCodec::MAJOR_VERSION,
27 version_minor: LeaveRequestCodec::MINOR_VERSION,
28 }
29 }
30
31 fn encode(data: LeaveRequest) -> Result<EncodedContent, CodecError> {
32 let mut buf = Vec::new();
33 data.encode(&mut buf)
34 .map_err(|e| CodecError::Encode(e.to_string()))?;
35
36 Ok(EncodedContent {
37 r#type: Some(LeaveRequestCodec::content_type()),
38 parameters: HashMap::new(),
39 fallback: None,
40 compression: None,
41 content: buf,
42 })
43 }
44
45 fn decode(content: EncodedContent) -> Result<LeaveRequest, CodecError> {
46 let decoded = LeaveRequest::decode(content.content.as_slice())
47 .map_err(|e| CodecError::Decode(e.to_string()))?;
48
49 Ok(decoded)
50 }
51
52 fn should_push() -> bool {
53 false
54 }
55}
56
57#[cfg(test)]
58pub(crate) mod tests {
59 use super::*;
60
61 #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
62 #[cfg_attr(not(target_arch = "wasm32"), test)]
63 fn test_encode_decode() {
64 let data = LeaveRequest {
65 authenticated_note: None,
66 };
67
68 let encoded = LeaveRequestCodec::encode(data).unwrap();
69 assert_eq!(encoded.clone().r#type.unwrap().type_id, "leave_request");
70
71 let _ = LeaveRequestCodec::decode(encoded).unwrap();
72 }
73}