mls_spec/messages/
content_type.rs1use crate::{
2 MlsSpecError,
3 group::{commits::Commit, proposals::Proposal},
4};
5
6#[derive(
7 Debug,
8 Clone,
9 Copy,
10 PartialEq,
11 Eq,
12 Hash,
13 tls_codec::TlsSerialize,
14 tls_codec::TlsDeserialize,
15 tls_codec::TlsSize,
16 strum::Display,
17)]
18#[strum(prefix = "ContentType")]
19#[cfg_attr(
20 feature = "serde",
21 derive(serde_repr::Serialize_repr, serde_repr::Deserialize_repr)
22)]
23#[repr(u8)]
24#[non_exhaustive]
25pub enum ContentType {
26 Reserved = 0x00,
27 Application = 0x01,
28 Proposal = 0x02,
29 Commit = 0x03,
30 #[cfg(feature = "draft-mahy-mls-new-content-types")]
31 Status = crate::drafts::new_content_types::CONTENT_TYPE_STATUS,
32 #[cfg(feature = "draft-mahy-mls-new-content-types")]
33 Ephemeral = crate::drafts::new_content_types::CONTENT_TYPE_EPHEMERAL,
34 #[cfg(feature = "draft-mularczyk-mls-splitcommit")]
35 SplitCommit = crate::drafts::split_commit::CONTENT_TYPE_SPLIT_COMMIT,
36}
37
38impl TryFrom<u8> for ContentType {
39 type Error = MlsSpecError;
40
41 fn try_from(value: u8) -> Result<Self, Self::Error> {
42 let ct = match value {
43 0x00 => Self::Reserved,
44 0x01 => Self::Application,
45 0x02 => Self::Proposal,
46 0x03 => Self::Commit,
47 #[cfg(feature = "draft-mahy-mls-new-content-types")]
48 crate::drafts::new_content_types::CONTENT_TYPE_STATUS => Self::Status,
49 #[cfg(feature = "draft-mahy-mls-new-content-types")]
50 crate::drafts::new_content_types::CONTENT_TYPE_EPHEMERAL => Self::Ephemeral,
51 #[cfg(feature = "draft-mularczyk-mls-splitcommit")]
52 crate::drafts::split_commit::CONTENT_TYPE_SPLIT_COMMIT => Self::SplitCommit,
53 _ => return Err(MlsSpecError::InvalidContentType),
54 };
55
56 Ok(ct)
57 }
58}
59
60#[derive(
61 Debug,
62 Clone,
63 PartialEq,
64 Eq,
65 tls_codec::TlsSerialize,
66 tls_codec::TlsDeserialize,
67 tls_codec::TlsSize,
68)]
69#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
70#[repr(u8)]
71pub enum ContentTypeInner {
72 #[tls_codec(discriminant = "ContentType::Application")]
73 Application {
74 #[tls_codec(with = "crate::tlspl::bytes")]
75 application_data: Vec<u8>,
76 },
77 #[tls_codec(discriminant = "ContentType::Proposal")]
78 Proposal { proposal: Proposal },
79 #[tls_codec(discriminant = "ContentType::Commit")]
80 Commit { commit: Commit },
81 #[cfg(feature = "draft-mularczyk-mls-splitcommit")]
82 #[tls_codec(discriminant = "ContentType::SplitCommit")]
83 SplitCommit {
84 split_commit: crate::drafts::split_commit::SplitCommit,
85 },
86 #[cfg(feature = "draft-mahy-mls-new-content-types")]
87 #[tls_codec(discriminant = "ContentType::Status")]
88 Status {
89 #[tls_codec(with = "crate::tlspl::bytes")]
90 application_data: Vec<u8>,
91 },
92 #[cfg(feature = "draft-mahy-mls-new-content-types")]
93 #[tls_codec(discriminant = "ContentType::Ephemeral")]
94 Ephemeral {
95 #[tls_codec(with = "crate::tlspl::bytes")]
96 application_data: Vec<u8>,
97 },
98}
99
100impl From<&ContentTypeInner> for ContentType {
101 fn from(value: &ContentTypeInner) -> Self {
102 match value {
103 ContentTypeInner::Application { .. } => ContentType::Application,
104 ContentTypeInner::Proposal { .. } => ContentType::Proposal,
105 ContentTypeInner::Commit { .. } => ContentType::Commit,
106 #[cfg(feature = "draft-mularczyk-mls-splitcommit")]
107 ContentTypeInner::SplitCommit { .. } => ContentType::SplitCommit,
108 #[cfg(feature = "draft-mahy-mls-new-content-types")]
109 ContentTypeInner::Status { .. } => ContentType::Status,
110 #[cfg(feature = "draft-mahy-mls-new-content-types")]
111 ContentTypeInner::Ephemeral { .. } => ContentType::Ephemeral,
112 }
113 }
114}