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
#[macro_export]
macro_rules! __impl_try_collect_tuple {
  () => { };
  ($A:ident $($I:ident)*) => {
      __impl_try_collect_tuple!($($I)*);

      impl<$A: Iterator> TryCollect<($A::Item, $($I::Item),*)> for $A {
          fn try_collect(&mut self) -> Option<($A::Item, $($I::Item),*)> {
              let r = (__try_opt!(self.next()),
                      // hack: we need to use $I in the expasion
                      $({ let a: $I::Item = __try_opt!(self.next()); a}),* );
              Some(r)
          }
      }
  }
}

#[macro_export]
macro_rules! __try_opt {
  ($e:expr) => (match $e { Some(e) => e, None => return None })
}

#[macro_export]
macro_rules! __try_collect {
  () => {
    trait TryCollect<T> {
      fn try_collect(&mut self) -> Option<T>;
    }
    
    // implement TryCollect<T> where T is a tuple with size 1, 2, .., 10
    __impl_try_collect_tuple!(A A A A A A A A A A);
  }
}

#[macro_export]
macro_rules! actor_messaging_handlers {
  ( $( $key:pat => $handler:expr ),* $(,)? ) => {

    // use $crate::wascc_actor::prelude::{actor_handlers};

    __try_collect!();



    fn handle_nats_message(msg: BrokerMessage) -> HandlerResult<()> {
      //println(&format!("Received broker message: {:?}", msg));
      let mut fillup_to_ten: Vec<&str> = msg.subject.split('.').collect() ;
      loop{ 
        if fillup_to_ten.len() >= 10 {break};
        fillup_to_ten.push("");
      }
      let splited: (&str, &str, &str, &str, &str, &str, &str, &str, &str, &str) = fillup_to_ten.into_iter().try_collect().unwrap();
      println(&format!("splited: {:?}", &splited));
      let mut result: HandlerResult<()>;
      match splited {
        $(
          $key => { result = $handler(&msg); },
        )*

        _=>{
          println(&format!("Unhandled broker message: {:?}", msg));
          result = Ok(());

        },
      }
      match &result{
        Ok(r)=> (),
        Err(e)=>{
          println(&format!("handling msg {:?} has error {:?}", &msg, e));
        },
      };
      result
    }
  }
}