Skip to main content

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 used only in tests
43#[cfg(test)]
44use sha2 as _;
45
46use ::serde::{Deserialize, Serialize};
47
48/// All [`Error`](std::error::Error)s generated in Barter-Integration.
49#[cfg(feature = "error")]
50pub mod error;
51
52/// Contains `StreamParser` implementations for transforming communication protocol specific
53/// messages into a generic output data structure.
54#[cfg(feature = "protocol")]
55pub mod protocol;
56
57/// Contains the flexible `Metric` type used for representing real-time metrics generically.
58#[cfg(feature = "metric")]
59pub mod metric;
60
61/// Defines a [`SubscriptionId`](subscription::SubscriptionId) new type representing a unique
62/// `SmolStr` identifier for a data stream (market data, account data) that has been
63/// subscribed to.
64#[cfg(feature = "subscription")]
65pub mod subscription;
66
67/// Defines a trait [`Tx`](channel::Tx) abstraction over different channel kinds, as well as
68/// other channel utilities.
69///
70/// eg/ `UnboundedTx`, `ChannelTxDroppable`, etc.
71#[cfg(feature = "channel")]
72pub mod channel;
73
74#[cfg(feature = "collection")]
75pub mod collection;
76
77/// Serialisation and deserialisation transformers and other utilities.
78#[cfg(feature = "serde")]
79pub mod serde;
80
81/// `Stream` extensions and utilities.
82#[cfg(feature = "stream")]
83pub mod stream;
84
85/// `ReconnectingSocket` extension and utilities.
86#[cfg(feature = "socket")]
87pub mod socket;
88
89/// PULL-based corporate-action sourcing: the [`StockSplitSource`](corporate_action::StockSplitSource)
90/// trait + [`CorporateActionFilter`](corporate_action::CorporateActionFilter), re-exporting the
91/// `CorporateAction` descriptor from `rustrade-instrument`.
92#[cfg(feature = "corporate-action")]
93pub mod corporate_action;
94
95/// [`Validator`]s are capable of determining if their internal state is satisfactory to fulfill
96/// some use case defined by the implementor.
97pub trait Validator {
98    type Error;
99
100    /// Check if `Self` is valid for some use case.
101    fn validate(self) -> Result<Self, Self::Error>
102    where
103        Self: Sized;
104}
105
106/// [`Transformer`]s are capable of transforming any `Input` into an iterator of
107/// `Result<Self::Output, Self::Error>`s.
108pub trait Transformer {
109    type Error;
110    type Input;
111    type Output;
112    type OutputIter: IntoIterator<Item = Result<Self::Output, Self::Error>>;
113
114    fn transform(&mut self, input: Self::Input) -> Self::OutputIter;
115}
116
117/// Determines if something is considered "unrecoverable", such as an unrecoverable error.
118///
119/// Note that the meaning of [`Unrecoverable`] may vary depending on the context.
120pub trait Unrecoverable {
121    fn is_unrecoverable(&self) -> bool;
122}
123
124/// Trait that communicates if something is terminal (eg/ requires shutdown or restart).
125pub trait Terminal {
126    fn is_terminal(&self) -> bool;
127}
128
129/// Indicates an `Iterator` or `Stream` has ended.
130#[derive(
131    Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Deserialize, Serialize,
132)]
133pub struct FeedEnded;
134
135/// Either an "Admin" or a "Payload" message.
136#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
137pub enum Message<A, T> {
138    Admin(A),
139    Payload(T),
140}
141
142/// Admin message that's either a "Protocol" or "Application" level event.
143#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
144pub enum Admin<P, A> {
145    Protocol(P),
146    Application(A),
147}