use crate::instance::InstanceSoftware;
#[derive(Clone, PartialEq, Eq, Ord, PartialOrd, Debug, Default, Copy)]
pub struct GatewayOptions {
pub encoding: GatewayEncoding,
pub transport_compression: GatewayTransportCompression,
}
impl GatewayOptions {
pub fn for_instance_software(software: InstanceSoftware) -> GatewayOptions {
let encoding = GatewayEncoding::Json;
let transport_compression = match software.supports_gateway_zlib() {
true => GatewayTransportCompression::ZLibStream,
false => GatewayTransportCompression::None,
};
GatewayOptions {
encoding,
transport_compression,
}
}
pub(crate) fn add_to_url(&self, url: &str) -> String {
let mut url = url.to_string();
let mut parameters = Vec::with_capacity(2);
let encoding = self.encoding.to_url_parameter();
parameters.push(encoding);
let compression = self.transport_compression.to_url_parameter();
if let Some(some_compression) = compression {
parameters.push(some_compression);
}
let mut has_parameters = url.contains('?') && url.contains('=');
if !has_parameters {
if !url.ends_with('/') {
url.push('/');
}
}
for parameter in parameters {
if !has_parameters {
url = format!("{}?{}", url, parameter);
has_parameters = true;
} else {
url = format!("{}&{}", url, parameter);
}
}
url
}
}
#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Debug, Default)]
pub enum GatewayTransportCompression {
None,
#[default]
ZLibStream,
}
impl GatewayTransportCompression {
pub(crate) fn to_url_parameter(self) -> Option<String> {
match self {
Self::None => None,
Self::ZLibStream => Some(String::from("compress=zlib-stream")),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Debug, Default)]
pub enum GatewayEncoding {
#[default]
Json,
ETF,
}
impl GatewayEncoding {
pub(crate) fn to_url_parameter(self) -> String {
match self {
Self::Json => String::from("encoding=json"),
Self::ETF => String::from("encoding=etf"),
}
}
}