mls_rs_core/
protocol_version.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// Copyright by contributors to this project.
3// SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5use core::ops::Deref;
6
7use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
8
9/// Wrapper type representing a protocol version identifier.
10#[derive(
11    Debug, Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, MlsSize, MlsEncode, MlsDecode,
12)]
13#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
14#[cfg_attr(all(feature = "ffi", not(test)), safer_ffi_gen::ffi_type)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[repr(transparent)]
17pub struct ProtocolVersion(u16);
18
19impl From<u16> for ProtocolVersion {
20    fn from(value: u16) -> Self {
21        ProtocolVersion(value)
22    }
23}
24
25impl From<ProtocolVersion> for u16 {
26    fn from(value: ProtocolVersion) -> Self {
27        value.0
28    }
29}
30
31impl Deref for ProtocolVersion {
32    type Target = u16;
33
34    fn deref(&self) -> &Self::Target {
35        &self.0
36    }
37}
38
39impl ProtocolVersion {
40    /// MLS version 1.0
41    pub const MLS_10: ProtocolVersion = ProtocolVersion(1);
42
43    /// Protocol version from a raw value, useful for testing.
44    pub const fn new(value: u16) -> ProtocolVersion {
45        ProtocolVersion(value)
46    }
47
48    /// Raw numerical wrapped value.
49    pub const fn raw_value(&self) -> u16 {
50        self.0
51    }
52
53    /// An iterator over all of the defined MLS versions.
54    pub fn all() -> impl Iterator<Item = ProtocolVersion> {
55        [Self::MLS_10].into_iter()
56    }
57}