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#![allow(clippy::type_complexity, clippy::too_many_arguments, type_alias_bounds)]
19
20//! # Barter-Integration
21//! High-performance, low-level framework for composing flexible web integrations.
22//!
23//! Utilised by other Barter trading ecosystem crates to build robust financial execution integrations,
24//! primarily for public data collection & trade execution. It is:
25//! * **Low-Level**: Translates raw data streams communicated over the web into any desired data model using arbitrary data transformations.
26//! * **Flexible**: Compatible with any protocol (WebSocket, FIX, Http, etc.), any input/output model, and any user defined transformations.
27//!
28//! ## Core abstractions:
29//! - **RestClient** providing configurable signed Http communication between client & server.
30//! - **ExchangeStream** providing configurable communication over any asynchronous stream protocols (WebSocket, FIX, etc.).
31//!
32//! Both core abstractions provide the robust glue you need to conveniently translate between server & client data models.
33
34// Silence unused_crate_dependencies for dev-dependencies used only in tests
35#[cfg(test)]
36use sha2 as _;
37
38use ::serde::{Deserialize, Serialize};
39
40/// All [`Error`](std::error::Error)s generated in Barter-Integration.
41#[cfg(feature = "error")]
42pub mod error;
43
44/// Contains `StreamParser` implementations for transforming communication protocol specific
45/// messages into a generic output data structure.
46#[cfg(feature = "protocol")]
47pub mod protocol;
48
49/// Contains the flexible `Metric` type used for representing real-time metrics generically.
50#[cfg(feature = "metric")]
51pub mod metric;
52
53/// Defines a [`SubscriptionId`](subscription::SubscriptionId) new type representing a unique
54/// `SmolStr` identifier for a data stream (market data, account data) that has been
55/// subscribed to.
56#[cfg(feature = "subscription")]
57pub mod subscription;
58
59/// Defines a trait [`Tx`](channel::Tx) abstraction over different channel kinds, as well as
60/// other channel utilities.
61///
62/// eg/ `UnboundedTx`, `ChannelTxDroppable`, etc.
63#[cfg(feature = "channel")]
64pub mod channel;
65
66#[cfg(feature = "collection")]
67pub mod collection;
68
69/// Serialisation and deserialisation transformers and other utilities.
70#[cfg(feature = "serde")]
71pub mod serde;
72
73/// `Stream` extensions and utilities.
74#[cfg(feature = "stream")]
75pub mod stream;
76
77/// `ReconnectingSocket` extension and utilities.
78#[cfg(feature = "socket")]
79pub mod socket;
80
81/// [`Validator`]s are capable of determining if their internal state is satisfactory to fulfill
82/// some use case defined by the implementor.
83pub trait Validator {
84 type Error;
85
86 /// Check if `Self` is valid for some use case.
87 fn validate(self) -> Result<Self, Self::Error>
88 where
89 Self: Sized;
90}
91
92/// [`Transformer`]s are capable of transforming any `Input` into an iterator of
93/// `Result<Self::Output, Self::Error>`s.
94pub trait Transformer {
95 type Error;
96 type Input;
97 type Output;
98 type OutputIter: IntoIterator<Item = Result<Self::Output, Self::Error>>;
99
100 fn transform(&mut self, input: Self::Input) -> Self::OutputIter;
101}
102
103/// Determines if something is considered "unrecoverable", such as an unrecoverable error.
104///
105/// Note that the meaning of [`Unrecoverable`] may vary depending on the context.
106pub trait Unrecoverable {
107 fn is_unrecoverable(&self) -> bool;
108}
109
110/// Trait that communicates if something is terminal (eg/ requires shutdown or restart).
111pub trait Terminal {
112 fn is_terminal(&self) -> bool;
113}
114
115/// Indicates an `Iterator` or `Stream` has ended.
116#[derive(
117 Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Deserialize, Serialize,
118)]
119pub struct FeedEnded;
120
121/// Either an "Admin" or a "Payload" message.
122#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
123pub enum Message<A, T> {
124 Admin(A),
125 Payload(T),
126}
127
128/// Admin message that's either a "Protocol" or "Application" level event.
129#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
130pub enum Admin<P, A> {
131 Protocol(P),
132 Application(A),
133}