price_adapter/types/
source.rs

1use crate::error::Error;
2use crate::types::{PriceInfo, WebsocketMessage};
3use futures_util::{stream::FusedStream, StreamExt};
4
5#[async_trait::async_trait]
6/// Represents a source for fetching prices through HTTP requests.
7///
8/// This trait defines methods for obtaining price information for a given set
9/// of symbols. Implementors are expected to provide an
10/// asynchronous implementation for retrieving prices.
11pub trait Source: Send + Sync + Unpin + 'static {
12    /// Asynchronously retrieves prices for the specified symbols.
13    ///
14    /// Return a vector of `Result<PriceInfo, Error>`. Each result represents the outcome of
15    /// attempting to fetch price information for a specific symbol.
16    async fn get_prices(&self, symbols: &[&str]) -> Vec<Result<PriceInfo, Error>>;
17
18    /// Asynchronously retrieves the price for a specified symbol.
19    ///
20    /// Return price information for the specified symbol.
21    async fn get_price(&self, symbol: &str) -> Result<PriceInfo, Error>;
22}
23
24#[async_trait::async_trait]
25/// Represents a source for streaming WebSocket messages.
26///
27/// This trait defines methods for connecting to a WebSocket, subscribing and
28/// unsubscribing to symbols, checking the connection status, and streaming
29/// WebSocket messages.
30pub trait WebSocketSource:
31    Send + Sync + StreamExt<Item = Result<WebsocketMessage, Error>> + FusedStream + Unpin + 'static
32{
33    /// Asynchronously establishes a connection to the WebSocket.
34    async fn connect(&mut self) -> Result<(), Error>;
35
36    /// Asynchronously subscribes to the specified symbols on the WebSocket.
37    ///
38    /// Returns the number of symbols successfully subscribed.
39    async fn subscribe(&mut self, symbols: &[&str]) -> Result<u32, Error>;
40
41    /// Asynchronously unsubscribes from the specified symbols on the WebSocket.
42    ///
43    /// Returns the number of symbols successfully unsubscribed.
44    async fn unsubscribe(&mut self, symbols: &[&str]) -> Result<u32, Error>;
45
46    /// Checks whether the WebSocket is currently connected.
47    async fn is_connected(&self) -> bool;
48}