Skip to main content

cripsy_binary_api/broker/
registry.rs

1//! Registro de adaptadores disponibles.
2
3#[cfg(feature = "async")]
4use crate::broker::config::BrokerParams;
5#[cfg(feature = "async")]
6use crate::broker::error::BrokerError;
7#[cfg(feature = "async")]
8use crate::broker::trait_::Broker;
9
10/// Nombres de los adaptadores registrados.
11pub struct BrokerRegistry;
12
13impl BrokerRegistry {
14    /// Adaptadores disponibles.
15    pub fn list() -> Vec<&'static str> {
16        vec!["iq_option"]
17    }
18
19    /// Existe el adaptador con este nombre.
20    pub fn contains(name: &str) -> bool {
21        Self::list().contains(&name)
22    }
23
24    /// Crea y conecta un adaptador async.
25    #[cfg(feature = "async")]
26    pub async fn create_async(params: BrokerParams) -> Result<Box<dyn Broker>, BrokerError> {
27        match params {
28            BrokerParams::IqOption(_) => {
29                let broker = <crate::broker::IqOptionBroker as Broker>::connect(params).await?;
30                Ok(Box::new(broker))
31            }
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn registro_incluye_iq_option() {
42        assert_eq!(BrokerRegistry::list(), vec!["iq_option"]);
43        assert!(BrokerRegistry::contains("iq_option"));
44        assert!(!BrokerRegistry::contains("inexistente"));
45    }
46}