1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use serde::Serialize;

use super::{login::LoginRef, ExaConnectOptionsRef};

/// Serialization helper for [`ExaConnectOptionsRef`].
/// This actually represents a database request, used for finalizing the login process.
/// However, it's structure does not allow including it into [`crate::command::ExaCommand`].
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SerializableConOpts<'a> {
    #[serde(flatten)]
    login: LoginRef<'a>,
    client_name: &'static str,
    client_version: &'static str,
    client_os: &'static str,
    client_runtime: &'static str,
    use_compression: bool,
    attributes: LoginAttrs<'a>,
}

/// Serialization helper, analog to [`crate::responses::Attributes`]
/// but containing only the relevant fields for login.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct LoginAttrs<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    current_schema: Option<&'a str>,
    query_timeout: u64,
    autocommit: bool,
    feedback_interval: u8,
}

impl<'a> SerializableConOpts<'a> {
    const CLIENT_RUNTIME: &'static str = "Rust";
    const CLIENT_NAME: &'static str = "Rust Exasol";
}

impl<'a> From<ExaConnectOptionsRef<'a>> for SerializableConOpts<'a> {
    fn from(value: ExaConnectOptionsRef<'a>) -> Self {
        let crate_version = option_env!("CARGO_PKG_VERSION").unwrap_or("UNKNOWN");

        let attributes = LoginAttrs {
            current_schema: value.schema,
            query_timeout: value.query_timeout,
            autocommit: true,
            feedback_interval: value.feedback_interval,
        };

        Self {
            login: value.login,
            client_name: Self::CLIENT_NAME,
            client_version: crate_version,
            client_os: std::env::consts::OS,
            use_compression: value.compression,
            client_runtime: Self::CLIENT_RUNTIME,
            attributes,
        }
    }
}