use std::collections::HashMap; use std::future::Future; use tokio::task::JoinHandle; use crate::BoxError; enum SignalKind { ClosePosition, OpenPosition, } struct EventMetadata { position_id: Option, order_id: Option, } #[derive(PartialEq, Eq, Hash)] enum EventKind { NewMinimum, NewMaximum, ReachedLoss, ReachedBreakEven, ReachedMinProfit, ReachedGoodProfit, ReachedMaxLoss, TrailingStopSet, TrailingStopMoved, OrderSubmitted, NewTick, } pub struct Event { kind: EventKind, tick: u64, metadata: Option, } impl Event { pub fn new(kind: EventKind, tick: u64, metadata: Option) -> Self { Event { kind, tick, metadata, } } fn has_metadata(&self) -> bool { self.metadata.is_some() } } pub struct EventDispatcher { event_handlers: HashMap JoinHandle<()>>> } impl EventDispatcher { pub fn new() -> Self { EventDispatcher { event_handlers: HashMap::new() } } pub fn call_handler(&self, event: &EventKind) { self.event_handlers.iter() .filter(|(e, _)| e == event) .map(|(_, f)| f("test".into())); } pub fn register_event_handler(&mut self, event: EventKind, f: F) where F: Fn(String) -> Fut, Fut: Future + Send, { self.event_handlers.insert(event, Box::new(move |args| tokio::spawn(f(args)))); } }