1use core::fmt;
2
3use crate::enums::ProtocolVersion;
4
5#[non_exhaustive]
11#[derive(Eq, PartialEq)]
12pub struct SupportedProtocolVersion {
13    pub version: ProtocolVersion,
15}
16
17impl fmt::Debug for SupportedProtocolVersion {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        self.version.fmt(f)
20    }
21}
22
23#[cfg(feature = "tls12")]
25pub static TLS12: SupportedProtocolVersion = SupportedProtocolVersion {
26    version: ProtocolVersion::TLSv1_2,
27};
28
29pub static TLS13: SupportedProtocolVersion = SupportedProtocolVersion {
31    version: ProtocolVersion::TLSv1_3,
32};
33
34pub static ALL_VERSIONS: &[&SupportedProtocolVersion] = &[
36    &TLS13,
37    #[cfg(feature = "tls12")]
38    &TLS12,
39];
40
41pub static DEFAULT_VERSIONS: &[&SupportedProtocolVersion] = ALL_VERSIONS;
47
48#[derive(Clone, Copy)]
49pub(crate) struct EnabledVersions {
50    #[cfg(feature = "tls12")]
51    tls12: Option<&'static SupportedProtocolVersion>,
52    tls13: Option<&'static SupportedProtocolVersion>,
53}
54
55impl fmt::Debug for EnabledVersions {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        let mut list = &mut f.debug_list();
58        #[cfg(feature = "tls12")]
59        if let Some(v) = self.tls12 {
60            list = list.entry(v);
61        }
62        if let Some(v) = self.tls13 {
63            list = list.entry(v);
64        }
65        list.finish()
66    }
67}
68
69impl EnabledVersions {
70    pub(crate) fn new(versions: &[&'static SupportedProtocolVersion]) -> Self {
71        let mut ev = Self {
72            #[cfg(feature = "tls12")]
73            tls12: None,
74            tls13: None,
75        };
76
77        for v in versions {
78            match v.version {
79                #[cfg(feature = "tls12")]
80                ProtocolVersion::TLSv1_2 => ev.tls12 = Some(v),
81                ProtocolVersion::TLSv1_3 => ev.tls13 = Some(v),
82                _ => {}
83            }
84        }
85
86        ev
87    }
88
89    pub(crate) fn contains(&self, version: ProtocolVersion) -> bool {
90        match version {
91            #[cfg(feature = "tls12")]
92            ProtocolVersion::TLSv1_2 => self.tls12.is_some(),
93            ProtocolVersion::TLSv1_3 => self.tls13.is_some(),
94            _ => false,
95        }
96    }
97}