1use serde::{Deserialize, Serialize};
6use std::{fmt, io::Read};
7use thiserror::Error;
8use tls_codec::{
9 Deserialize as TlsDeserializeTrait, DeserializeBytes, Error, Serialize as TlsSerializeTrait,
10 Size,
11};
12
13#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
25#[repr(u16)]
26#[allow(missing_docs)]
27pub enum ProtocolVersion {
28 Mls10 = 1,
29 Other(u16),
30}
31
32impl Default for ProtocolVersion {
34 fn default() -> Self {
35 ProtocolVersion::Mls10
36 }
37}
38
39impl From<u16> for ProtocolVersion {
40 fn from(v: u16) -> Self {
42 match v {
43 1 => ProtocolVersion::Mls10,
44 _ => ProtocolVersion::Other(v),
45 }
46 }
47}
48
49impl TlsSerializeTrait for ProtocolVersion {
50 fn tls_serialize<W: std::io::Write>(&self, writer: &mut W) -> Result<usize, tls_codec::Error> {
51 match self {
52 ProtocolVersion::Mls10 => {
53 let v = 1u16;
54 v.tls_serialize(writer)
55 }
56 ProtocolVersion::Other(v) => v.tls_serialize(writer),
57 }
58 }
59}
60
61impl TlsDeserializeTrait for ProtocolVersion {
62 fn tls_deserialize<R: Read>(bytes: &mut R) -> Result<Self, Error>
63 where
64 Self: Sized,
65 {
66 u16::tls_deserialize(bytes).map(ProtocolVersion::from)
67 }
68}
69
70impl DeserializeBytes for ProtocolVersion {
71 fn tls_deserialize_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error>
72 where
73 Self: Sized,
74 {
75 let (v, bytes) = u16::tls_deserialize_bytes(bytes)?;
76 Ok((ProtocolVersion::from(v), bytes))
77 }
78}
79
80impl Size for ProtocolVersion {
81 fn tls_serialized_len(&self) -> usize {
82 2
83 }
84}
85
86impl fmt::Display for ProtocolVersion {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 match &self {
89 ProtocolVersion::Mls10 => write!(f, "MLS 1.0"),
90 ProtocolVersion::Other(v) => write!(f, "Other version: {}", v),
91 }
92 }
93}
94
95#[derive(Error, Debug, PartialEq, Eq, Clone)]
97pub enum VersionError {
98 #[error("Unsupported MLS version.")]
100 UnsupportedMlsVersion,
101}