use std::fmt;
use adbc_core::{
error::Result,
options::{OptionConnection, OptionValue},
Database as _,
};
#[cfg(feature = "env")]
use crate::{builder::env_parse_map_err, database};
use crate::{builder::BuilderIter, Connection, Database};
#[derive(Clone, Default)]
#[non_exhaustive]
pub struct Builder {
pub use_high_precision: Option<bool>,
pub other: Vec<(OptionConnection, OptionValue)>,
}
impl fmt::Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Builder").field("...", &self.other).finish()
}
}
#[cfg(feature = "env")]
impl Builder {
pub const USE_HIGH_PRECISION_ENV: &str = database::Builder::USE_HIGH_PRECISION_ENV;
pub fn from_env() -> Result<Self> {
#[cfg(feature = "dotenv")]
let _ = dotenvy::dotenv();
let use_high_precision = env_parse_map_err(Self::USE_HIGH_PRECISION_ENV, str::parse)?;
Ok(Self {
use_high_precision,
..Default::default()
})
}
}
impl Builder {
const COUNT: usize = 1;
pub const USE_HIGH_PRECISION: &str = "adbc.snowflake.sql.client_option.use_high_precision";
pub fn with_high_precision(mut self, use_high_precision: bool) -> Self {
self.use_high_precision = Some(use_high_precision);
self
}
}
impl Builder {
pub fn build(self, database: &Database) -> Result<Connection> {
database.new_connection_with_opts(self)
}
}
impl IntoIterator for Builder {
type Item = (OptionConnection, OptionValue);
type IntoIter = BuilderIter<OptionConnection, { Builder::COUNT }>;
fn into_iter(self) -> Self::IntoIter {
BuilderIter::new(
[self
.use_high_precision
.as_ref()
.map(ToString::to_string)
.map(OptionValue::String)
.map(|value| (Builder::USE_HIGH_PRECISION.into(), value))],
self.other,
)
}
}