diff --git a/src/action_dispatcher.rs b/src/action_dispatcher.rs new file mode 100644 index 0000000..a1d7a52 --- /dev/null +++ b/src/action_dispatcher.rs @@ -0,0 +1,125 @@ +enum State { + Pending, + Waiting, + Done, +} + +impl State { + fn as_str(&self) -> &'static str { + match self { + State::Pending => "pending", + State::Waiting => "waiting", + State::Done => "done", + } + } + fn next(&mut self) { + match self { + State::Pending => *self = State::Waiting, + State::Waiting => *self = State::Done, + _ => {} + } + } +} + +enum Action { + Auth { state: State }, + RateLimit { state: State }, +} + +impl Action { + fn trigger(&mut self) { + match self { + Action::Auth { .. } => self.auth(), + Action::RateLimit { .. } => self.rate_limit(), + } + } + + fn get_state(&self) -> &State { + match self { + Action::Auth { state } => state, + Action::RateLimit { state } => state, + } + } + + fn rate_limit(&mut self) { + // Specifics for RL, returning State + if let Action::RateLimit { state } = self { + match state { + State::Pending => { + println!("Trigger the request and return State::Waiting"); + state.next(); + } + State::Waiting => { + println!( + "When got on_grpc_response, process RL response and return State::Done" + ); + state.next(); + } + State::Done => { + println!("Done for RL... calling next action (?)"); + } + } + } + } + + fn auth(&mut self) { + // Specifics for Auth, returning State + if let Action::Auth { state } = self { + match state { + State::Pending => { + println!("Trigger the request and return State::Waiting"); + state.next(); + } + State::Waiting => { + println!( + "When got on_grpc_response, process Auth response and return State::Done" + ); + state.next(); + } + State::Done => { + println!("Done for Auth... calling next action (?)"); + } + } + } + } +} + +struct ActionDispatcher { + actions: Vec, +} + +impl ActionDispatcher { + fn default() -> ActionDispatcher { + ActionDispatcher { actions: vec![] } + } + fn new(/*vec of PluginConfig actions*/) -> ActionDispatcher { + ActionDispatcher { + // hardcoded for now + actions: vec![ + Action::Auth { + state: State::Pending, + }, + Action::RateLimit { + state: State::Pending, + }, + ], + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn action_transition() { + let mut action = Action::Auth { + state: State::Pending, + }; + assert_eq!(action.get_state().as_str(), "pending"); + action.trigger(); + assert_eq!(action.get_state().as_str(), "waiting"); + action.trigger(); + assert_eq!(action.get_state().as_str(), "done"); + } +} diff --git a/src/lib.rs b/src/lib.rs index fb1c60a..eb78d0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +mod action_dispatcher; mod attribute; mod configuration; mod envoy;