Skip to main content

barter_integration/
lib.rs

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