use std::sync::Arc;
use crate::{
db_client::{raw::RawImpl, route_based::RouteBasedImpl, DbClient},
rpc_client::RpcClientImplFactory,
Authorization, RpcConfig,
};
#[derive(Debug, Clone)]
pub enum Mode {
Direct,
Proxy,
}
#[derive(Debug, Clone)]
pub struct Builder {
mode: Mode,
endpoint: String,
default_database: Option<String>,
rpc_config: RpcConfig,
authorization: Option<Authorization>,
}
impl Builder {
pub fn new(endpoint: String, mode: Mode) -> Self {
Self {
mode,
endpoint,
rpc_config: RpcConfig::default(),
default_database: None,
authorization: None,
}
}
#[inline]
pub fn default_database(mut self, default_database: impl Into<String>) -> Self {
self.default_database = Some(default_database.into());
self
}
#[inline]
pub fn rpc_config(mut self, rpc_config: RpcConfig) -> Self {
self.rpc_config = rpc_config;
self
}
#[inline]
pub fn authorization(mut self, authorization: Authorization) -> Self {
self.authorization = Some(authorization);
self
}
pub fn build(self) -> Arc<dyn DbClient> {
let rpc_client_factory = Arc::new(RpcClientImplFactory::new(
self.rpc_config,
self.authorization,
));
match self.mode {
Mode::Direct => Arc::new(RouteBasedImpl::new(
rpc_client_factory,
self.endpoint,
self.default_database,
)),
Mode::Proxy => Arc::new(RawImpl::new(
rpc_client_factory,
self.endpoint,
self.default_database,
)),
}
}
}