use crate::{bson::Document, error::Result, options::ClientOptions, Client};
use super::options::AutoEncryptionOptions;
pub struct EncryptedClientBuilder {
client_options: ClientOptions,
enc_opts: AutoEncryptionOptions,
}
impl EncryptedClientBuilder {
pub(crate) fn new(client_options: ClientOptions, enc_opts: AutoEncryptionOptions) -> Self {
EncryptedClientBuilder {
client_options,
enc_opts,
}
}
pub fn key_vault_client(mut self, client: impl Into<Option<Client>>) -> Self {
self.enc_opts.key_vault_client = client.into();
self
}
pub fn schema_map(
mut self,
map: impl IntoIterator<Item = (impl Into<String>, Document)>,
) -> Self {
self.enc_opts.schema_map = Some(map.into_iter().map(|(is, d)| (is.into(), d)).collect());
self
}
pub fn bypass_auto_encryption(mut self, bypass: impl Into<Option<bool>>) -> Self {
self.enc_opts.bypass_auto_encryption = bypass.into();
self
}
pub fn extra_options(mut self, extra_opts: impl Into<Option<Document>>) -> Self {
self.enc_opts.extra_options = extra_opts.into();
self
}
pub fn encrypted_fields_map(
mut self,
map: impl IntoIterator<Item = (impl Into<String>, Document)>,
) -> Self {
self.enc_opts.encrypted_fields_map =
Some(map.into_iter().map(|(is, d)| (is.into(), d)).collect());
self
}
pub fn bypass_query_analysis(mut self, bypass: impl Into<Option<bool>>) -> Self {
self.enc_opts.bypass_query_analysis = bypass.into();
self
}
#[cfg(test)]
pub(crate) fn disable_crypt_shared(mut self, disable: bool) -> Self {
self.enc_opts.disable_crypt_shared = Some(disable);
self
}
pub async fn build(self) -> Result<Client> {
let client = Client::with_options(self.client_options)?;
*client.inner.csfle.write().await =
Some(super::ClientState::new(&client, self.enc_opts).await?);
Ok(client)
}
}