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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::boxed::Box;
use std::sync::Arc;
use futures::Future;
use std::io;

pub type MiddlewareReturnValue<T> = Box<Future<Item=T, Error=io::Error> + Send>;
pub type Middleware<T, M> = fn(T, next: M) -> MiddlewareReturnValue<T>;
pub type Runnable<T> = Box<Fn(T, &Option<Box<MiddlewareChain<T>>>) -> MiddlewareReturnValue<T> + Send + Sync>;

///
/// The MiddlewareChain is used to wrap a series of middleware functions in such a way that the tail can
/// be accessed and modified later on. This allows Thruster to properly compose pieces of middleware
/// into a single long chain rather than relying on disperate parts.
///
pub struct MiddlewareChain<T: 'static> {
  pub runnable: Arc<Runnable<T>>,
  is_assigned: bool,
  chained: Option<Box<MiddlewareChain<T>>>
}

impl<T: 'static> MiddlewareChain<T> {
  ///
  /// Creates a new, blank (i.e. will panic if run,) MiddlewareChain
  ///
  pub fn new() -> Self {
    MiddlewareChain {
      runnable: Arc::new(Box::new(|_: T, _: &Option<Box<MiddlewareChain<T>>>| panic!("Use of unassigned middleware chain, be sure to run `assign` first."))),
      is_assigned: false,
      chained: None
    }
  }

  ///
  /// Assign a runnable function to this middleware chain
  ///
  pub fn assign(&mut self, chain: Runnable<T>) {
    self.runnable = Arc::new(chain);
    self.is_assigned = true;
  }

  ///
  /// Run the middleware chain once
  ///
  pub fn run(&self, context: T) -> MiddlewareReturnValue<T> {
    if self.is_assigned {
      (self.runnable)(context, &self.chained)
    } else if let Some(ref chain) = self.chained {
      chain.run(context)
    } else {
      (self.runnable)(context, &self.chained)
    }
  }

  ///
  /// Concatenate two middleware chains. This will make this chains tail point
  /// to the next chain. That means that calling `next` in the final piece of
  /// this chain will invoke the next chain rather than an "End of chain" panic
  ///
  pub fn chain(&mut self, chain: MiddlewareChain<T>) {
    match self.chained {
      Some(ref mut existing_chain) => existing_chain.chain(chain),
      None => self.chained = Some(Box::new(chain))
    };
  }

  ///
  /// Tells if the chain has been assigned OR whether it is unassigned but has
  /// an assigned tail. If is only chained but has no assigned runnable, then
  /// this chain acts as a passthrough to the next one.
  ///
  pub fn is_assigned(&self) -> bool {
    if self.is_assigned {
      true
    } else {
      match self.chained {
        Some(ref chained) => chained.is_assigned(),
        None => false
      }
    }
  }
}

impl<T: 'static> Default for MiddlewareChain<T> {
  fn default() -> Self {
    Self::new()
  }
}

impl<T: 'static> Clone for MiddlewareChain<T> {
  fn clone(&self) -> Self {
    MiddlewareChain {
      runnable: self.runnable.clone(),
      is_assigned: self.is_assigned,
      chained: self.chained.clone()
    }
  }
}


///
/// The middleware macro takes a series of functions whose contexts all have `into` implemented,
/// and then links them together to run in series. The result of this macro is a single `MiddlewareChain`.
///
#[macro_export]
macro_rules! middleware {
  [ @tailtype $ctx:ty => $head:expr ] => { $ctx };
  [ @tailtype $ctx:ty => $head:expr, $($tail_t:ty => $tail_e:expr),+ ] => { middleware![@tailtype $( $tail_t => $tail_e ),*] };
  [ @internal $ctx:ty => $head:expr, $ender:expr ] => (
    |context: $ctx| {
      Box::new(
        $head(
          context.into(), |ctx| {
            $ender(ctx.into())
          })
          .map(|ctx| ctx.into()))
    }
  );
  [ @internal $ctx:ty => $head:expr, $($tail_t:ty => $tail_e:expr),+ , $ender:expr ] => (
    |context: $ctx| {
      Box::new(
        $head(
          context.into(), |ctx: $ctx| {
            middleware![@internal $( $tail_t => $tail_e ),*, $ender](ctx.into())
          })
          .map(|ctx| ctx.into()))
    }
  );
  [ $ctx:ty => $head:expr ] => {{
    use std::boxed::Box;
    use futures::future::Future;

    let mut chain: MiddlewareChain<$ctx> = MiddlewareChain::new();

    fn dummy(context: $ctx, next: impl Fn($ctx) -> MiddlewareReturnValue<$ctx>  + Send + Sync) -> MiddlewareReturnValue<$ctx> {
      next(context)
    }

    chain.assign(Box::new(|context, chain| {
      middleware![@internal $ctx => $head,
        $ctx => dummy,
        |ctx| {
          match chain {
            Some(val) => val.run(ctx),
            None => panic!("End of chain")
          }
        }](context)
      }));

    chain
  }};
  [ $ctx:ty => $head:expr, $($tail_t:ty => $tail_e:expr),+ ] => {{
    use std::boxed::Box;
    use futures::future::Future;

    let mut chain = MiddlewareChain::new();

    fn dummy(context: $ctx, next: impl Fn($ctx) -> MiddlewareReturnValue<$ctx>  + Send + Sync) -> MiddlewareReturnValue<$ctx> {
      next(context)
    }

    chain.assign(Box::new(|context, chain| {
      middleware![@internal $ctx => $head, $( $tail_t => $tail_e ),* ,
        $ctx => dummy,
        |ctx| {
          match chain {
            Some(val) => val.run(ctx),
            None => panic!("End of chain")
          }
        }](context)
      }));

    chain
  }};
}