use crate::chain::Chain;
use crate::transport::DefaultTransport;
#[cfg(not(target_arch = "wasm32"))]
use crate::transport::ReqwestTransport;
use super::api::OrderBookApi;
use super::builder_state;
#[derive(Debug, Clone)]
pub struct OrderBookApiBuilder<Target = builder_state::Missing> {
chain: Option<Chain>,
base_url: Option<url::Url>,
transport: Option<DefaultTransport>,
_state: core::marker::PhantomData<Target>,
}
impl OrderBookApiBuilder {
const fn new() -> Self {
Self {
chain: None,
base_url: None,
transport: None,
_state: core::marker::PhantomData,
}
}
}
impl<Target> OrderBookApiBuilder<Target> {
fn cast<NextTarget>(self) -> OrderBookApiBuilder<NextTarget> {
OrderBookApiBuilder {
chain: self.chain,
base_url: self.base_url,
transport: self.transport,
_state: core::marker::PhantomData,
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn with_client(mut self, client: reqwest::Client) -> Self {
self.transport = Some(ReqwestTransport::new(client));
self
}
#[allow(clippy::missing_const_for_fn)]
pub fn with_transport(mut self, transport: DefaultTransport) -> Self {
self.transport = Some(transport);
self
}
pub fn with_chain(self, chain: Chain) -> OrderBookApiBuilder<builder_state::Set> {
let mut next = self.cast::<builder_state::Set>();
next.chain = Some(chain);
next.base_url = Some(chain.orderbook_base_url());
next
}
pub fn with_base_url(self, base_url: url::Url) -> OrderBookApiBuilder<builder_state::Set> {
let mut next = self.cast::<builder_state::Set>();
next.chain = None;
next.base_url = Some(base_url);
next
}
}
impl OrderBookApiBuilder<builder_state::Set> {
pub fn build(self) -> OrderBookApi {
let base_url = self.base_url.expect("target typestate sets base_url");
let transport = self.transport.unwrap_or_default();
let api = OrderBookApi::new_with_transport(base_url, transport);
match self.chain {
Some(chain) => api.with_chain_hint(chain),
None => api,
}
}
}
impl OrderBookApi {
pub const fn builder() -> OrderBookApiBuilder {
OrderBookApiBuilder::new()
}
pub fn with_chain(chain: Chain) -> OrderBookApiBuilder<builder_state::Set> {
Self::builder().with_chain(chain)
}
pub fn new(chain: Chain) -> Self {
Self::new_with_transport(chain.orderbook_base_url(), DefaultTransport::default())
.with_chain_hint(chain)
}
pub fn new_with_base_url(base_url: url::Url) -> Self {
Self::new_with_transport(base_url, DefaultTransport::default())
}
#[cfg(not(target_arch = "wasm32"))]
pub fn with_client(base_url: url::Url, client: reqwest::Client) -> Self {
Self::new_with_transport(base_url, ReqwestTransport::new(client))
}
}