1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use super::*;
use derive::{FromInner, FromValue, TryIntoAnyInner};
use netidx_derive::Pack;
use serde::{Deserialize, Serialize};

/// TypedMessage is a wrapper enum for component messages, for all components that
/// this version of Architect is compiled with and supports.  This lets components
/// define and operate over their own independent message types while still allowing 
/// cross-component communication.
///
/// Architect installations are mutually intelligible to the extent of TypedMessage
/// variants they share in common.
///
/// TypedMessage should follow sensible rules for versioning and cross-
/// compatibility, such as explicit tagging of variants, and avoiding breaking 
/// changes to the component message types.
#[derive(Debug, Clone, Pack, FromValue, Serialize, Deserialize, FromInner, TryIntoAnyInner)]
#[transitive(B2C2Cpty <-> Orderflow)]
#[transitive(CboeDigitalCpty <-> Folio)]
#[transitive(CoinbaseCpty <-> Folio)]
#[transitive(CoinbaseCpty <-> Orderflow)]
#[transitive(CoinbasePrimeCpty <-> Folio)]
#[transitive(CoinbasePrimeCpty <-> Orderflow)]
#[transitive(CumberlandCpty <-> Orderflow)]
#[transitive(CumberlandCpty <-> Folio)]
#[transitive(DeribitCpty <-> Folio)]
#[transitive(DeribitCpty <-> Orderflow)]
#[transitive(FalconXCpty <-> Folio)]
#[transitive(FalconXCpty <-> Orderflow)]
#[transitive(GalaxyCpty <-> Orderflow)]
#[transitive(KrakenCpty <-> Folio)]
#[transitive(KrakenCpty <-> Orderflow)]
#[transitive(MockCpty <-> Folio)]
#[transitive(MockCpty <-> Orderflow)]
#[transitive(WintermuteCpty <-> Folio)]
#[transitive(WintermuteCpty <-> Orderflow)]
#[transitive(Orderflow <-> Oms)]
#[transitive(Algo <-> TwapAlgo <- Orderflow)]
#[transitive(Algo <-> SmartOrderRouterAlgo)]
#[transitive(Algo <-> MMAlgo <- Orderflow)]
#[transitive(Algo <-> PovAlgo <- Orderflow)]
#[rustfmt::skip]
pub enum TypedMessage {
    #[pack(tag(  0))] SystemControl(system_control::SystemControlMessage),
    #[pack(tag(  1))] Symbology(symbology::SymbologyUpdate),
    #[pack(tag(  2))] OrderAuthority(orderflow::OrderAuthorityMessage),
    #[pack(tag(  3))] Orderflow(orderflow::OrderflowMessage),
    #[pack(tag(  4))] Oms(oms::OmsMessage),
    #[pack(tag(  5))] Algo(algo::AlgoMessage),
    #[pack(tag(  6))] Folio(folio::FolioMessage),
    #[pack(tag(  7))] AccountMaster(orderflow::account::AccountMessage),
    #[pack(tag( 99))] MockCpty(cpty::mock::MockCptyMessage),
    #[pack(tag(100))] CoinbaseCpty(cpty::coinbase::CoinbaseMessage),
    #[pack(tag(101))] B2C2Cpty(cpty::b2c2::B2C2Message),
    #[pack(tag(103))] KrakenCpty(cpty::kraken::KrakenMessage),
    #[pack(tag(104))] DeribitCpty(cpty::deribit::DeribitMessage),
    #[pack(tag(105))] WintermuteCpty(cpty::wintermute::WintermuteMessage),
    #[pack(tag(106))] FalconXCpty(cpty::falconx::FalconXMessage),
    #[pack(tag(107))] CoinbasePrimeCpty(cpty::coinbase_prime::CoinbasePrimeMessage),
    #[pack(tag(108))] GalaxyCpty(cpty::galaxy::GalaxyMessage),
    #[pack(tag(109))] CumberlandCpty(cpty::cumberland::CumberlandMessage),
    #[pack(tag(110))] CboeDigitalCpty(cpty::cboe_digital::CboeDigitalMessage),
    // #[pack(tag(111))] CqgCpty(cpty::cqg::CqgMessage),
    #[pack(tag(200))] TwapAlgo(algo::twap::TwapMessage),
    #[pack(tag(201))] SmartOrderRouterAlgo(algo::smart_order_router::SmartOrderRouterMessage),
    #[pack(tag(202))] MMAlgo(algo::mm::MMAlgoMessage),
    #[pack(tag(203))] PovAlgo(algo::pov::PovAlgoMessage),
}

impl TypedMessage {
    pub fn is_system_control(&self) -> bool {
        matches!(self, TypedMessage::SystemControl(..))
    }

    pub fn downcast<T>(self) -> Option<T>
    where
        TypedMessage: TryInto<MaybeSplit<TypedMessage, T>>,
    {
        if let Ok((_, downcasted)) =
            TryInto::<MaybeSplit<TypedMessage, T>>::try_into(self).map(MaybeSplit::parts)
        {
            Some(downcasted)
        } else {
            None
        }
    }
}

pub enum MaybeSplit<A, B> {
    Just(B),
    Split(A, B),
}

impl<A, B> MaybeSplit<A, B> {
    pub fn parts(self) -> (Option<A>, B) {
        match self {
            MaybeSplit::Just(b) => (None, b),
            MaybeSplit::Split(a, b) => (Some(a), b),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{
        orderflow::{OrderBuilder, OrderId, OrderSource, Out},
        symbology::MarketId,
    };
    use anyhow::Result;
    use rust_decimal::Decimal;

    #[test]
    fn test_try_into_any_variant() -> Result<()> {
        use crate::orderflow::OrderflowMessage;
        let m = TypedMessage::Orderflow(OrderflowMessage::Order(
            OrderBuilder::new(
                OrderId::new_unchecked(123),
                OrderSource::API,
                MarketId::try_from("BTC Crypto/USD*COINBASE/DIRECT")?,
            )
            .limit(Dir::Buy, Decimal::new(100, 0), Decimal::new(1, 0), false)
            .build()?,
        ));
        let m2: std::result::Result<MaybeSplit<TypedMessage, oms::OmsMessage>, _> =
            m.try_into();
        assert_eq!(m2.is_ok(), true);
        Ok(())
    }

    /// test transitive closure of length 3 (B2C2 -> Orderflow -> Algo -> TWAPAlgo)
    #[test]
    fn test_try_into_any_variant_3() -> Result<()> {
        use crate::{algo::twap::TwapMessage, cpty::b2c2::B2C2Message};
        let src = TypedMessage::B2C2Cpty(B2C2Message::Out(Out {
            order_id: OrderId::new_unchecked(123),
        }));
        let dst: std::result::Result<MaybeSplit<TypedMessage, TwapMessage>, _> =
            src.try_into();
        assert_eq!(dst.is_ok(), true);
        Ok(())
    }
}