sqlx_exasol/options/
protocol_version.rs

1use std::{
2    fmt::{Display, Formatter},
3    str::FromStr,
4};
5
6use serde::{Deserialize, Serialize};
7
8use super::{error::ExaConfigError, PARAM_PROTOCOL_VERSION};
9
10/// Enum listing the protocol versions that can be used when establishing a websocket connection to
11/// Exasol. Defaults to the highest defined protocol version and falls back to the highest protocol
12/// version supported by the server.
13#[derive(Debug, Clone, Copy, Eq, PartialEq, Deserialize, Serialize)]
14#[serde(try_from = "u8")]
15#[serde(into = "u8")]
16#[repr(u8)]
17pub enum ProtocolVersion {
18    V1 = 1,
19    V2 = 2,
20    V3 = 3,
21    V4 = 4,
22}
23
24impl FromStr for ProtocolVersion {
25    type Err = ExaConfigError;
26
27    fn from_str(s: &str) -> Result<Self, Self::Err> {
28        match s {
29            "1" => Ok(ProtocolVersion::V1),
30            "2" => Ok(ProtocolVersion::V2),
31            "3" => Ok(ProtocolVersion::V3),
32            "4" => Ok(ProtocolVersion::V4),
33            _ => Err(ExaConfigError::InvalidParameter(PARAM_PROTOCOL_VERSION)),
34        }
35    }
36}
37
38impl From<ProtocolVersion> for u8 {
39    fn from(value: ProtocolVersion) -> Self {
40        value as Self
41    }
42}
43
44impl TryFrom<u8> for ProtocolVersion {
45    type Error = ExaConfigError;
46
47    fn try_from(value: u8) -> Result<Self, Self::Error> {
48        match value {
49            1 => Ok(Self::V1),
50            2 => Ok(Self::V2),
51            3 => Ok(Self::V3),
52            4 => Ok(Self::V4),
53            _ => Err(ExaConfigError::InvalidParameter(PARAM_PROTOCOL_VERSION)),
54        }
55    }
56}
57
58impl Display for ProtocolVersion {
59    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
60        write!(f, "{}", *self as u8)
61    }
62}