-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
Signed-off-by: dd di cesare <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
enum State { | ||
Check warning on line 1 in src/action_dispatcher.rs GitHub Actions / Check
Check warning on line 1 in src/action_dispatcher.rs GitHub Actions / Rustfmt
Check failure on line 1 in src/action_dispatcher.rs GitHub Actions / Clippy
|
||
Pending, | ||
Waiting, | ||
Done, | ||
} | ||
|
||
impl State { | ||
fn as_str(&self) -> &'static str { | ||
Check warning on line 8 in src/action_dispatcher.rs GitHub Actions / Check
Check warning on line 8 in src/action_dispatcher.rs GitHub Actions / Rustfmt
Check failure on line 8 in src/action_dispatcher.rs GitHub Actions / Clippy
|
||
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 { | ||
Check warning on line 24 in src/action_dispatcher.rs GitHub Actions / Check
Check warning on line 24 in src/action_dispatcher.rs GitHub Actions / Rustfmt
Check failure on line 24 in src/action_dispatcher.rs GitHub Actions / Clippy
|
||
Auth { state: State }, | ||
RateLimit { state: State }, | ||
Check failure on line 26 in src/action_dispatcher.rs GitHub Actions / Clippy
|
||
} | ||
|
||
impl Action { | ||
fn trigger(&mut self) { | ||
Check warning on line 30 in src/action_dispatcher.rs GitHub Actions / Check
Check warning on line 30 in src/action_dispatcher.rs GitHub Actions / Rustfmt
Check failure on line 30 in src/action_dispatcher.rs GitHub Actions / Clippy
|
||
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 { | ||
Check warning on line 87 in src/action_dispatcher.rs GitHub Actions / Check
Check warning on line 87 in src/action_dispatcher.rs GitHub Actions / Rustfmt
Check failure on line 87 in src/action_dispatcher.rs GitHub Actions / Clippy
Check warning on line 87 in src/action_dispatcher.rs GitHub Actions / Test Suite
|
||
actions: Vec<Action>, | ||
} | ||
|
||
impl ActionDispatcher { | ||
fn default() -> ActionDispatcher { | ||
Check warning on line 92 in src/action_dispatcher.rs GitHub Actions / Check
Check warning on line 92 in src/action_dispatcher.rs GitHub Actions / Rustfmt
Check failure on line 92 in src/action_dispatcher.rs GitHub Actions / Clippy
Check warning on line 92 in src/action_dispatcher.rs GitHub Actions / Test Suite
|
||
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"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod action_dispatcher; | ||
mod attribute; | ||
mod configuration; | ||
mod envoy; | ||
|