use std::marker::PhantomData;
use std::sync::Arc;
use crate::client::sync::Client;
use crate::client::StreamDecoder;
use crate::errors::Error;
use crate::messages::OutgoingMessages;
use crate::subscriptions::sync::Subscription;
use crate::subscriptions::DecoderContext;
use crate::transport::InternalSubscription;
#[allow(dead_code)]
pub(crate) struct RequestBuilder<'a> {
client: &'a Client,
request_id: i32,
}
#[allow(dead_code)]
impl<'a> RequestBuilder<'a> {
pub fn new(client: &'a Client) -> Self {
Self {
client,
request_id: client.next_request_id(),
}
}
pub fn with_id(client: &'a Client, request_id: i32) -> Self {
Self { client, request_id }
}
pub fn request_id(&self) -> i32 {
self.request_id
}
pub fn check_version(self, required_version: i32, feature: &str) -> Result<Self, Error> {
self.client.check_server_version(required_version, feature)?;
Ok(self)
}
pub fn send<T>(self, message: Vec<u8>) -> Result<Subscription<T>, Error>
where
T: StreamDecoder<T>,
{
SubscriptionBuilder::new(self.client).send_with_request_id(self.request_id, message)
}
pub fn send_with_context<T>(self, message: Vec<u8>, context: DecoderContext) -> Result<Subscription<T>, Error>
where
T: StreamDecoder<T>,
{
SubscriptionBuilder::new(self.client)
.with_context(context)
.send_with_request_id(self.request_id, message)
}
pub fn send_raw(self, message: Vec<u8>) -> Result<InternalSubscription, Error> {
self.client.send_request(self.request_id, message)
}
}
#[allow(dead_code)]
pub(crate) struct SharedRequestBuilder<'a> {
client: &'a Client,
message_type: OutgoingMessages,
}
#[allow(dead_code)]
impl<'a> SharedRequestBuilder<'a> {
pub fn new(client: &'a Client, message_type: OutgoingMessages) -> Self {
Self { client, message_type }
}
pub fn check_version(self, required_version: i32, feature: &str) -> Result<Self, Error> {
self.client.check_server_version(required_version, feature)?;
Ok(self)
}
pub fn send<T>(self, message: Vec<u8>) -> Result<Subscription<T>, Error>
where
T: StreamDecoder<T>,
{
SubscriptionBuilder::new(self.client).send_shared(self.message_type, message)
}
pub fn send_with_context<T>(self, message: Vec<u8>, context: DecoderContext) -> Result<Subscription<T>, Error>
where
T: StreamDecoder<T>,
{
SubscriptionBuilder::new(self.client)
.with_context(context)
.send_shared(self.message_type, message)
}
pub fn send_raw(self, message: Vec<u8>) -> Result<InternalSubscription, Error> {
self.client.send_shared_request(self.message_type, message)
}
}
#[allow(dead_code)]
pub(crate) struct OrderRequestBuilder<'a> {
client: &'a Client,
order_id: i32,
}
#[allow(dead_code)]
impl<'a> OrderRequestBuilder<'a> {
pub fn new(client: &'a Client) -> Self {
Self {
client,
order_id: client.next_order_id(),
}
}
pub fn with_id(client: &'a Client, order_id: i32) -> Self {
Self { client, order_id }
}
pub fn order_id(&self) -> i32 {
self.order_id
}
pub fn check_version(self, required_version: i32, feature: &str) -> Result<Self, Error> {
self.client.check_server_version(required_version, feature)?;
Ok(self)
}
pub fn send(self, message: Vec<u8>) -> Result<InternalSubscription, Error> {
self.client.send_order(self.order_id, message)
}
}
#[allow(dead_code)]
pub(crate) struct MessageBuilder<'a> {
client: &'a Client,
}
#[allow(dead_code)]
impl<'a> MessageBuilder<'a> {
pub fn new(client: &'a Client) -> Self {
Self { client }
}
pub fn check_version(self, required_version: i32, feature: &str) -> Result<Self, Error> {
self.client.check_server_version(required_version, feature)?;
Ok(self)
}
pub fn send(self, message: Vec<u8>) -> Result<(), Error> {
self.client.send_message(message)
}
}
#[allow(dead_code)]
pub(crate) struct SubscriptionBuilder<'a, T> {
client: &'a Client,
context: DecoderContext,
_phantom: PhantomData<T>,
}
#[allow(dead_code)]
impl<'a, T> SubscriptionBuilder<'a, T>
where
T: StreamDecoder<T>,
{
pub fn new(client: &'a Client) -> Self {
Self {
client,
context: client.decoder_context(),
_phantom: PhantomData,
}
}
pub fn with_context(mut self, context: DecoderContext) -> Self {
self.context = context;
self
}
pub fn with_smart_depth(mut self, is_smart_depth: bool) -> Self {
self.context.is_smart_depth = is_smart_depth;
self
}
pub fn build(self, subscription: InternalSubscription) -> Subscription<T> {
Subscription::new(Arc::clone(&self.client.message_bus), subscription, self.context)
}
pub fn send_with_request_id(self, request_id: i32, message: Vec<u8>) -> Result<Subscription<T>, Error> {
let subscription = self.client.send_request(request_id, message)?;
Ok(self.build(subscription))
}
pub fn send_shared(self, message_type: OutgoingMessages, message: Vec<u8>) -> Result<Subscription<T>, Error> {
let subscription = self.client.send_shared_request(message_type, message)?;
Ok(self.build(subscription))
}
pub fn send_order(self, order_id: i32, message: Vec<u8>) -> Result<Subscription<T>, Error> {
let subscription = self.client.send_order(order_id, message)?;
Ok(self.build(subscription))
}
}
#[allow(dead_code)]
pub(crate) trait ClientRequestBuilders {
fn request(&self) -> RequestBuilder<'_>;
fn request_with_id(&self, request_id: i32) -> RequestBuilder<'_>;
fn shared_request(&self, message_type: OutgoingMessages) -> SharedRequestBuilder<'_>;
fn order_request(&self) -> OrderRequestBuilder<'_>;
fn order_request_with_id(&self, order_id: i32) -> OrderRequestBuilder<'_>;
fn message(&self) -> MessageBuilder<'_>;
}
#[allow(dead_code)]
impl ClientRequestBuilders for Client {
fn request(&self) -> RequestBuilder<'_> {
RequestBuilder::new(self)
}
fn request_with_id(&self, request_id: i32) -> RequestBuilder<'_> {
RequestBuilder::with_id(self, request_id)
}
fn shared_request(&self, message_type: OutgoingMessages) -> SharedRequestBuilder<'_> {
SharedRequestBuilder::new(self, message_type)
}
fn order_request(&self) -> OrderRequestBuilder<'_> {
OrderRequestBuilder::new(self)
}
fn order_request_with_id(&self, order_id: i32) -> OrderRequestBuilder<'_> {
OrderRequestBuilder::with_id(self, order_id)
}
fn message(&self) -> MessageBuilder<'_> {
MessageBuilder::new(self)
}
}
pub(crate) trait SubscriptionBuilderExt {
fn subscription<T>(&self) -> SubscriptionBuilder<'_, T>
where
T: StreamDecoder<T>;
}
impl SubscriptionBuilderExt for Client {
fn subscription<T>(&self) -> SubscriptionBuilder<'_, T>
where
T: StreamDecoder<T>,
{
SubscriptionBuilder::new(self)
}
}
#[cfg(test)]
#[path = "sync_tests.rs"]
mod tests;