Skip to content

Commit

Permalink
[wip, feat] Action dispatcher state machine
Browse files Browse the repository at this point in the history
Signed-off-by: dd di cesare <[email protected]>
  • Loading branch information
didierofrivia committed Aug 29, 2024
1 parent ed8eec1 commit a66a992
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
125 changes: 125 additions & 0 deletions src/action_dispatcher.rs
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

View workflow job for this annotation

GitHub Actions / Check

enum `State` is never used

Check failure on line 1 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

enum `State` is never used

Check warning on line 1 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

enum `State` is never used

Check warning on line 1 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

enum `State` is never used
Pending,
Waiting,
Done,
}

impl State {
fn as_str(&self) -> &'static str {

Check warning on line 8 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Check

methods `as_str` and `next` are never used

Check failure on line 8 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

methods `as_str` and `next` are never used

Check warning on line 8 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

methods `as_str` and `next` are never used

Check warning on line 8 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

methods `as_str` and `next` are never used
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

View workflow job for this annotation

GitHub Actions / Check

enum `Action` is never used

Check failure on line 24 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

enum `Action` is never used

Check warning on line 24 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

enum `Action` is never used

Check warning on line 24 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

enum `Action` is never used
Auth { state: State },
RateLimit { state: State },

Check failure on line 26 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

variant `RateLimit` is never constructed

Check warning on line 26 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variant `RateLimit` is never constructed
}

impl Action {
fn trigger(&mut self) {

Check warning on line 30 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Check

methods `trigger`, `get_state`, `rate_limit`, and `auth` are never used

Check failure on line 30 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

methods `trigger`, `get_state`, `rate_limit`, and `auth` are never used

Check warning on line 30 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

methods `trigger`, `get_state`, `rate_limit`, and `auth` are never used

Check warning on line 30 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

methods `trigger`, `get_state`, `rate_limit`, and `auth` are never used
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

View workflow job for this annotation

GitHub Actions / Check

struct `ActionDispatcher` is never constructed

Check failure on line 87 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

struct `ActionDispatcher` is never constructed

Check warning on line 87 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

struct `ActionDispatcher` is never constructed

Check warning on line 87 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

struct `ActionDispatcher` is never constructed

Check warning on line 87 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

struct `ActionDispatcher` is never constructed
actions: Vec<Action>,
}

impl ActionDispatcher {
fn default() -> ActionDispatcher {

Check warning on line 92 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Check

associated functions `default` and `new` are never used

Check failure on line 92 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

associated functions `default` and `new` are never used

Check warning on line 92 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

associated functions `default` and `new` are never used

Check warning on line 92 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

associated functions `default` and `new` are never used

Check warning on line 92 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

associated functions `default` and `new` are never used
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");
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod action_dispatcher;
mod attribute;
mod configuration;
mod envoy;
Expand Down

0 comments on commit a66a992

Please sign in to comment.