use std::sync::Arc;
use crate::{
models::AccountReference,
options::{OperationOptions, Region},
};
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct DriverOptions {
account: AccountReference,
operation_options: Arc<OperationOptions>,
preferred_regions: Vec<Region>,
}
impl DriverOptions {
pub fn builder(account: AccountReference) -> DriverOptionsBuilder {
DriverOptionsBuilder::new(account)
}
pub fn account(&self) -> &AccountReference {
&self.account
}
pub fn operation_options(&self) -> &Arc<OperationOptions> {
&self.operation_options
}
pub fn preferred_regions(&self) -> &[Region] {
&self.preferred_regions
}
}
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct DriverOptionsBuilder {
account: AccountReference,
operation_options: Option<OperationOptions>,
preferred_regions: Vec<Region>,
}
impl DriverOptionsBuilder {
pub fn new(account: AccountReference) -> Self {
Self {
account,
operation_options: None,
preferred_regions: Vec::new(),
}
}
pub fn with_operation_options(mut self, options: OperationOptions) -> Self {
self.operation_options = Some(options);
self
}
pub fn with_preferred_regions(mut self, regions: Vec<Region>) -> Self {
self.preferred_regions = regions;
self
}
pub fn build(self) -> DriverOptions {
DriverOptions {
account: self.account,
operation_options: Arc::new(self.operation_options.unwrap_or_default()),
preferred_regions: self.preferred_regions,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::options::OperationOptionsBuilder;
use url::Url;
fn test_account() -> AccountReference {
AccountReference::with_master_key(
Url::parse("https://test.documents.azure.com:443/").unwrap(),
"test-key",
)
}
#[test]
fn builder_creates_options_with_account() {
let account = test_account();
let options = DriverOptionsBuilder::new(account.clone()).build();
assert_eq!(options.account(), &account);
assert!(options
.operation_options()
.read_consistency_strategy
.is_none());
assert!(options
.operation_options()
.max_failover_retry_count
.is_none());
assert!(options
.operation_options()
.max_session_retry_count
.is_none());
}
#[test]
fn builder_sets_operation_options() {
let operation = OperationOptionsBuilder::new()
.with_max_failover_retry_count(5)
.with_max_session_retry_count(3)
.build();
let options = DriverOptionsBuilder::new(test_account())
.with_operation_options(operation)
.build();
assert_eq!(
options.operation_options().max_failover_retry_count,
Some(5)
);
assert_eq!(options.operation_options().max_session_retry_count, Some(3));
}
#[test]
fn builder_sets_all_options() {
let operation = OperationOptionsBuilder::new()
.with_max_failover_retry_count(5)
.with_max_session_retry_count(2)
.build();
let options = DriverOptionsBuilder::new(test_account())
.with_operation_options(operation)
.build();
assert_eq!(
options.operation_options().max_failover_retry_count,
Some(5)
);
assert_eq!(options.operation_options().max_session_retry_count, Some(2));
assert!(options
.operation_options()
.read_consistency_strategy
.is_none());
assert!(options.preferred_regions().is_empty());
}
#[test]
fn builder_sets_preferred_regions() {
let regions = vec![Region::WEST_US_2, Region::EAST_US];
let options = DriverOptionsBuilder::new(test_account())
.with_preferred_regions(regions.clone())
.build();
assert_eq!(options.preferred_regions(), ®ions);
}
}