Skip to content

Commit

Permalink
[feat] A simplistic approach, agnostic to extension type
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 30, 2024
1 parent 9c383bc commit 62befe4
Showing 1 changed file with 42 additions and 77 deletions.
119 changes: 42 additions & 77 deletions src/action_dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use proxy_wasm::types::Status;
use std::cell::RefCell;

#[derive(PartialEq, Debug, Clone)]
Expand All @@ -16,67 +17,40 @@ impl State {
}
}
}
#[derive(PartialEq, Clone)]
pub(crate) enum Action {
Auth { state: State },
RateLimit { state: State },
#[derive(Clone)]
pub(crate) struct Action {

Check warning on line 21 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Check

struct `Action` is never constructed

Check warning on line 21 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

struct `Action` is never constructed

Check failure on line 21 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

struct `Action` is never constructed

Check warning on line 21 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

struct `Action` is never constructed
state: State,
result: Result<u32, Status>,
operation: Option<fn() -> Result<u32, Status>>,
}

impl Action {
pub fn trigger(&mut self) {
match self {
Action::Auth { .. } => self.auth(),
Action::RateLimit { .. } => self.rate_limit(),
pub fn default() -> Self {

Check warning on line 28 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Check

associated items `default`, `set_operation`, `trigger`, `get_state`, and `get_result` are never used

Check warning on line 28 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

associated items `default`, `set_operation`, `trigger`, `get_state`, and `get_result` are never used

Check failure on line 28 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

associated items `default`, `set_operation`, `trigger`, `get_state`, and `get_result` are never used

Check warning on line 28 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

associated items `default`, `set_operation`, `trigger`, `get_state`, and `get_result` are never used
Self {
state: State::Pending,
result: Err(Status::Empty),
operation: None,
}
}

fn get_state(&self) -> &State {
match self {
Action::Auth { state } => state,
Action::RateLimit { state } => state,
}
pub fn set_operation(&mut self, operation: fn() -> Result<u32, Status>) {
self.operation = Some(operation);
}

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 (?)");
}
}
pub fn trigger(&mut self) {
if let State::Done = self.state {
} else if let Some(operation) = self.operation {
self.result = operation();
self.state.next();
}
}

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 (?)");
}
}
}
fn get_state(&self) -> State {
self.state.clone()
}

fn get_result(&self) -> Result<u32, Status> {
self.result
}
}

Expand All @@ -91,10 +65,6 @@ impl ActionDispatcher {
}
}

pub fn new(/*vec of PluginConfig actions*/) -> ActionDispatcher {
ActionDispatcher::default()
}

pub fn push_actions(&self, actions: Vec<Action>) {
self.actions.borrow_mut().extend(actions);
}
Expand All @@ -106,6 +76,10 @@ impl ActionDispatcher {
.map(|action| action.get_state().clone())
}

pub fn get_current_action_result(&self) -> Result<u32, Status> {
self.actions.borrow().first().unwrap().get_result()
}

pub fn next(&self) -> bool {
let mut actions = self.actions.borrow_mut();
if let Some((i, action)) = actions.iter_mut().enumerate().next() {
Expand All @@ -128,57 +102,47 @@ mod tests {

#[test]
fn action_transition() {
let mut action = Action::Auth {
state: State::Pending,
};
assert_eq!(*action.get_state(), State::Pending);
let mut action = Action::default();
action.set_operation(|| -> Result<u32, Status> { Ok(200) });
assert_eq!(action.get_state(), State::Pending);
action.trigger();
assert_eq!(*action.get_state(), State::Waiting);
assert_eq!(action.get_state(), State::Waiting);
action.trigger();
assert_eq!(*action.get_state(), State::Done);
assert_eq!(action.result, Ok(200));
assert_eq!(action.get_state(), State::Done);
}

#[test]
fn action_dispatcher_push_actions() {
let mut action_dispatcher = ActionDispatcher {

Check failure on line 117 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

variable does not need to be mutable

Check warning on line 117 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variable does not need to be mutable
actions: RefCell::new(vec![Action::RateLimit {
state: State::Pending,
}]),
actions: RefCell::new(vec![Action::default()]),
};

assert_eq!(action_dispatcher.actions.borrow().len(), 1);

action_dispatcher.push_actions(vec![Action::Auth {
state: State::Pending,
}]);
action_dispatcher.push_actions(vec![Action::default()]);

assert_eq!(action_dispatcher.actions.borrow().len(), 2);
}

#[test]
fn action_dispatcher_get_current_action_state() {
let action_dispatcher = ActionDispatcher {
actions: RefCell::new(vec![Action::RateLimit {
state: State::Waiting,
}]),
actions: RefCell::new(vec![Action::default()]),
};

assert_eq!(
action_dispatcher.get_current_action_state(),
Some(State::Waiting)
Some(State::Pending)
);

let action_dispatcher2 = ActionDispatcher::default();

assert_eq!(action_dispatcher2.get_current_action_state(), None);
}

#[test]
fn action_dispatcher_next() {
let mut action = Action::default();
action.set_operation(|| -> Result<u32, Status> { Ok(200) });
let mut action_dispatcher = ActionDispatcher {

Check failure on line 144 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

variable does not need to be mutable

Check warning on line 144 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variable does not need to be mutable
actions: RefCell::new(vec![Action::RateLimit {
state: State::Pending,
}]),
actions: RefCell::new(vec![action]),
};
let mut res = action_dispatcher.next();
assert_eq!(res, true);

Check failure on line 148 in src/action_dispatcher.rs

View workflow job for this annotation

GitHub Actions / Clippy

used `assert_eq!` with a literal bool
Expand All @@ -193,6 +157,7 @@ mod tests {
action_dispatcher.get_current_action_state(),
Some(State::Done)
);
assert_eq!(action_dispatcher.get_current_action_result(), Ok(200));

res = action_dispatcher.next();
assert_eq!(res, false);
Expand Down

0 comments on commit 62befe4

Please sign in to comment.