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