rustrade_integration/lib.rs
1#![forbid(unsafe_code)]
2#![deny(
3 clippy::unwrap_used,
4 clippy::expect_used,
5 clippy::cast_possible_truncation,
6 clippy::cast_sign_loss
7)]
8#![warn(
9 unused,
10 clippy::cognitive_complexity,
11 unused_crate_dependencies,
12 unused_extern_crates,
13 clippy::unused_self,
14 clippy::useless_let_if_seq,
15 missing_debug_implementations,
16 rust_2018_idioms
17)]
18// Inherited from the barter-rs upstream: the generic, trait-heavy public API legitimately produces
19// complex nested types and many-argument constructors, and `type_alias_bounds` fires on
20// documentation-bearing alias bounds kept intentionally. Denying these adds churn without improving
21// the API.
22#![allow(clippy::type_complexity, clippy::too_many_arguments, type_alias_bounds)]
23// Clippy 1.99 checks each field's span rather than the whole expression's, so the
24// `Self { field: field }` that `derive_more::Constructor` expands to is reported at our own
25// field declarations. Remove once rust-lang/rust-clippy#17525 is fixed in a stable release.
26#![allow(clippy::redundant_field_names)]
27
28//! # Barter-Integration
29//! High-performance, low-level framework for composing flexible web integrations.
30//!
31//! Utilised by other Barter trading ecosystem crates to build robust financial execution integrations,
32//! primarily for public data collection & trade execution. It is:
33//! * **Low-Level**: Translates raw data streams communicated over the web into any desired data model using arbitrary data transformations.
34//! * **Flexible**: Compatible with any protocol (WebSocket, FIX, Http, etc.), any input/output model, and any user defined transformations.
35//!
36//! ## Core abstractions:
37//! - **RestClient** providing configurable signed Http communication between client & server.
38//! - **ExchangeStream** providing configurable communication over any asynchronous stream protocols (WebSocket, FIX, etc.).
39//!
40//! Both core abstractions provide the robust glue you need to conveniently translate between server & client data models.
41
42// Silence unused_crate_dependencies for dev-dependencies: each is used only by the tests or
43// examples of some features, so a test build with other features alone would report it.
44#[cfg(test)]
45use {
46 rustrade_instrument as _, serde_json as _, sha2 as _, tokio as _, tokio_stream as _,
47 tokio_test as _, wiremock as _,
48};
49
50use ::serde::{Deserialize, Serialize};
51
52/// All [`Error`](std::error::Error)s generated in Barter-Integration.
53#[cfg(feature = "error")]
54pub mod error;
55
56/// Contains `StreamParser` implementations for transforming communication protocol specific
57/// messages into a generic output data structure.
58#[cfg(feature = "protocol")]
59pub mod protocol;
60
61/// Contains the flexible `Metric` type used for representing real-time metrics generically.
62#[cfg(feature = "metric")]
63pub mod metric;
64
65/// Defines a [`SubscriptionId`](subscription::SubscriptionId) new type representing a unique
66/// `SmolStr` identifier for a data stream (market data, account data) that has been
67/// subscribed to.
68#[cfg(feature = "subscription")]
69pub mod subscription;
70
71/// Defines a trait [`Tx`](channel::Tx) abstraction over different channel kinds, as well as
72/// other channel utilities.
73///
74/// eg/ `UnboundedTx`, `ChannelTxDroppable`, etc.
75#[cfg(feature = "channel")]
76pub mod channel;
77
78#[cfg(feature = "collection")]
79pub mod collection;
80
81/// Serialisation and deserialisation transformers and other utilities.
82#[cfg(feature = "serde")]
83pub mod serde;
84
85/// `Stream` extensions and utilities.
86#[cfg(feature = "stream")]
87pub mod stream;
88
89/// `ReconnectingSocket` extension and utilities.
90#[cfg(feature = "socket")]
91pub mod socket;
92
93/// PULL-based corporate-action sourcing: the [`StockSplitSource`](corporate_action::StockSplitSource)
94/// trait + [`CorporateActionFilter`](corporate_action::CorporateActionFilter), re-exporting the
95/// `CorporateAction` descriptor from `rustrade-instrument`.
96#[cfg(feature = "corporate-action")]
97pub mod corporate_action;
98
99/// [`Validator`]s are capable of determining if their internal state is satisfactory to fulfill
100/// some use case defined by the implementor.
101pub trait Validator {
102 type Error;
103
104 /// Check if `Self` is valid for some use case.
105 fn validate(self) -> Result<Self, Self::Error>
106 where
107 Self: Sized;
108}
109
110/// [`Transformer`]s are capable of transforming any `Input` into an iterator of
111/// `Result<Self::Output, Self::Error>`s.
112pub trait Transformer {
113 type Error;
114 type Input;
115 type Output;
116 type OutputIter: IntoIterator<Item = Result<Self::Output, Self::Error>>;
117
118 fn transform(&mut self, input: Self::Input) -> Self::OutputIter;
119}
120
121/// Determines if something is considered "unrecoverable", such as an unrecoverable error.
122///
123/// Note that the meaning of [`Unrecoverable`] may vary depending on the context.
124pub trait Unrecoverable {
125 fn is_unrecoverable(&self) -> bool;
126}
127
128/// Trait that communicates if something is terminal (eg/ requires shutdown or restart).
129pub trait Terminal {
130 fn is_terminal(&self) -> bool;
131}
132
133/// Indicates an `Iterator` or `Stream` has ended.
134#[derive(
135 Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Deserialize, Serialize,
136)]
137pub struct FeedEnded;
138
139/// Either an "Admin" or a "Payload" message.
140#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
141pub enum Message<A, T> {
142 Admin(A),
143 Payload(T),
144}
145
146/// Admin message that's either a "Protocol" or "Application" level event.
147#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
148pub enum Admin<P, A> {
149 Protocol(P),
150 Application(A),
151}