Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ExaConnectOptionsBuilder ergonomics #15

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 41 additions & 18 deletions src/options/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,87 +103,110 @@ impl ExaConnectOptionsBuilder {
Ok(opts)
}

pub fn host(&mut self, host: String) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn host(mut self, host: String) -> Self {
self.host = Some(host);
self
}

pub fn port(&mut self, port: u16) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn port(mut self, port: u16) -> Self {
self.port = port;
self
}

pub fn ssl_mode(&mut self, ssl_mode: ExaSslMode) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn ssl_mode(mut self, ssl_mode: ExaSslMode) -> Self {
self.ssl_mode = ssl_mode;
self
}

pub fn ssl_ca(&mut self, ssl_ca: CertificateInput) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn ssl_ca(mut self, ssl_ca: CertificateInput) -> Self {
self.ssl_ca = Some(ssl_ca);
self
}

pub fn ssl_client_cert(&mut self, ssl_client_cert: CertificateInput) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn ssl_client_cert(mut self, ssl_client_cert: CertificateInput) -> Self {
self.ssl_client_cert = Some(ssl_client_cert);
self
}

pub fn ssl_client_key(&mut self, ssl_client_key: CertificateInput) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn ssl_client_key(mut self, ssl_client_key: CertificateInput) -> Self {
self.ssl_client_key = Some(ssl_client_key);
self
}

pub fn statement_cache_capacity(&mut self, capacity: NonZeroUsize) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn statement_cache_capacity(mut self, capacity: NonZeroUsize) -> Self {
self.statement_cache_capacity = capacity;
self
}

pub fn username(&mut self, username: String) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn username(mut self, username: String) -> Self {
self.username = Some(username);
self
}

pub fn password(&mut self, password: String) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn password(mut self, password: String) -> Self {
self.password = Some(password);
self
}

pub fn access_token(&mut self, access_token: String) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn access_token(mut self, access_token: String) -> Self {
self.access_token = Some(access_token);
self
}

pub fn refresh_token(&mut self, refresh_token: String) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn refresh_token(mut self, refresh_token: String) -> Self {
self.refresh_token = Some(refresh_token);
self
}

pub fn schema(&mut self, schema: String) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn schema(mut self, schema: String) -> Self {
self.schema = Some(schema);
self
}

pub fn protocol_version(&mut self, protocol_version: ProtocolVersion) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn protocol_version(mut self, protocol_version: ProtocolVersion) -> Self {
self.protocol_version = protocol_version;
self
}

pub fn fetch_size(&mut self, fetch_size: usize) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn fetch_size(mut self, fetch_size: usize) -> Self {
self.fetch_size = fetch_size;
self
}

pub fn query_timeout(&mut self, query_timeout: u64) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn query_timeout(mut self, query_timeout: u64) -> Self {
self.query_timeout = query_timeout;
self
}

pub fn compression(&mut self, compression: bool) -> &mut Self {
self.compression = compression;
#[must_use = "call build() to get connection options"]
pub fn compression(mut self, compression: bool) -> Self {
let feature_flag = cfg!(feature = "compression");

if feature_flag && !compression {
tracing::warn!("compression cannot be enabled without the 'compression' feature");
}

self.compression = compression && feature_flag;
self
}

pub fn feedback_interval(&mut self, feedback_interval: u8) -> &mut Self {
#[must_use = "call build() to get connection options"]
pub fn feedback_interval(mut self, feedback_interval: u8) -> Self {
self.feedback_interval = feedback_interval;
self
}
Expand Down
34 changes: 17 additions & 17 deletions src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,92 +96,92 @@ impl ConnectOptions for ExaConnectOptions {
let mut builder = Self::builder();

if let Some(host) = url.host_str() {
builder.host(host.to_owned());
builder = builder.host(host.to_owned());
}

let username = url.username();
if !username.is_empty() {
builder.username(username.to_owned());
builder = builder.username(username.to_owned());
}

if let Some(password) = url.password() {
builder.password(password.to_owned());
builder = builder.password(password.to_owned());
}

if let Some(port) = url.port() {
builder.port(port);
builder = builder.port(port);
}

let opt_schema = url.path_segments().into_iter().flatten().next();

if let Some(schema) = opt_schema {
builder.schema(schema.to_owned());
builder = builder.schema(schema.to_owned());
}

for (name, value) in url.query_pairs() {
match name.as_ref() {
PARAM_ACCESS_TOKEN => builder.access_token(value.to_string()),
PARAM_ACCESS_TOKEN => builder = builder.access_token(value.to_string()),

PARAM_REFRESH_TOKEN => builder.refresh_token(value.to_string()),
PARAM_REFRESH_TOKEN => builder = builder.refresh_token(value.to_string()),

PARAM_PROTOCOL_VERSION => {
let protocol_version = value.parse::<ProtocolVersion>()?;
builder.protocol_version(protocol_version)
builder = builder.protocol_version(protocol_version);
}

PARAM_SSL_MODE => {
let ssl_mode = value.parse::<ExaSslMode>()?;
builder.ssl_mode(ssl_mode)
builder = builder.ssl_mode(ssl_mode);
}

PARAM_SSL_CA => {
let ssl_ca = CertificateInput::File(PathBuf::from(value.to_string()));
builder.ssl_ca(ssl_ca)
builder = builder.ssl_ca(ssl_ca);
}

PARAM_SSL_CERT => {
let ssl_cert = CertificateInput::File(PathBuf::from(value.to_string()));
builder.ssl_client_cert(ssl_cert)
builder = builder.ssl_client_cert(ssl_cert);
}

PARAM_SSL_KEY => {
let ssl_key = CertificateInput::File(PathBuf::from(value.to_string()));
builder.ssl_client_key(ssl_key)
builder = builder.ssl_client_key(ssl_key);
}

PARAM_CACHE_CAP => {
let capacity = value
.parse::<NonZeroUsize>()
.map_err(|_| ExaConfigError::InvalidParameter(PARAM_CACHE_CAP))?;
builder.statement_cache_capacity(capacity)
builder = builder.statement_cache_capacity(capacity);
}

PARAM_FETCH_SIZE => {
let fetch_size = value
.parse::<usize>()
.map_err(|_| ExaConfigError::InvalidParameter(PARAM_FETCH_SIZE))?;
builder.fetch_size(fetch_size)
builder = builder.fetch_size(fetch_size);
}

PARAM_QUERY_TIMEOUT => {
let query_timeout = value
.parse::<u64>()
.map_err(|_| ExaConfigError::InvalidParameter(PARAM_QUERY_TIMEOUT))?;
builder.query_timeout(query_timeout)
builder = builder.query_timeout(query_timeout);
}

PARAM_COMPRESSION => {
let compression = value
.parse::<bool>()
.map_err(|_| ExaConfigError::InvalidParameter(PARAM_COMPRESSION))?;
builder.compression(compression)
builder = builder.compression(compression);
}

PARAM_FEEDBACK_INTERVAL => {
let feedback_interval = value
.parse::<u8>()
.map_err(|_| ExaConfigError::InvalidParameter(PARAM_FEEDBACK_INTERVAL))?;
builder.feedback_interval(feedback_interval)
builder = builder.feedback_interval(feedback_interval);
}

_ => {
Expand Down
Loading