Skip to content

Commit

Permalink
🔥 module crate::limit::conditions et al
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Nov 27, 2024
1 parent 3e7a428 commit 80c994d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 685 deletions.
18 changes: 12 additions & 6 deletions limitador-server/src/envoy_rls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ mod tests {
namespace,
1,
60,
vec!["req_method == 'GET'"],
vec!["req_method == 'GET'".to_string()],
vec!["app_id"],
)
.expect("This must be a valid limit!");
Expand Down Expand Up @@ -395,10 +395,16 @@ mod tests {
let namespace = "test_namespace";

vec![
Limit::new(namespace, 10, 60, vec!["x == '1'"], vec!["z"])
.expect("This must be a valid limit!"),
Limit::new(namespace, 0, 60, vec!["x == '1'", "y == '2'"], vec!["z"])
Limit::new(namespace, 10, 60, vec!["x == '1'".to_string()], vec!["z"])
.expect("This must be a valid limit!"),
Limit::new(
namespace,
0,
60,
vec!["x == '1'".to_string(), "y == '2'".to_string()],
vec!["z"],
)
.expect("This must be a valid limit!"),
]
.into_iter()
.for_each(|limit| {
Expand Down Expand Up @@ -462,7 +468,7 @@ mod tests {
#[tokio::test]
async fn test_takes_into_account_the_hits_addend_param() {
let namespace = "test_namespace";
let limit = Limit::new(namespace, 10, 60, vec!["x == '1'"], vec!["y"])
let limit = Limit::new(namespace, 10, 60, vec!["x == '1'".to_string()], vec!["y"])
.expect("This must be a valid limit!");

let limiter = RateLimiter::new(10_000);
Expand Down Expand Up @@ -532,7 +538,7 @@ mod tests {
// "hits_addend" is optional according to the spec, and should default
// to 1, However, with the autogenerated structs it defaults to 0.
let namespace = "test_namespace";
let limit = Limit::new(namespace, 1, 60, vec!["x == '1'"], vec!["y"])
let limit = Limit::new(namespace, 1, 60, vec!["x == '1'".to_string()], vec!["y"])
.expect("This must be a valid limit!");

let limiter = RateLimiter::new(10_000);
Expand Down
5 changes: 2 additions & 3 deletions limitador-server/src/http_api/request_types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use limitador::counter::Counter as LimitadorCounter;
use limitador::errors::LimitadorError;
use limitador::limit::Limit as LimitadorLimit;
use limitador::limit::{Limit as LimitadorLimit, ParseError};
use paperclip::actix::Apiv2Schema;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
Expand Down Expand Up @@ -42,7 +41,7 @@ impl From<&LimitadorLimit> for Limit {
}

impl TryFrom<Limit> for LimitadorLimit {
type Error = LimitadorError;
type Error = ParseError;

fn try_from(limit: Limit) -> Result<Self, Self::Error> {
let mut limitador_limit = if let Some(id) = limit.id {
Expand Down
11 changes: 6 additions & 5 deletions limitador/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::limit::ConditionParsingError;
use crate::limit::cel::EvaluationError;
use crate::limit::ParseError;
use crate::storage::StorageErr;
use std::convert::Infallible;
use std::error::Error;
Expand All @@ -7,7 +8,7 @@ use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub enum LimitadorError {
StorageError(StorageErr),
InterpreterError(ConditionParsingError),
InterpreterError(EvaluationError),
}

impl Display for LimitadorError {
Expand Down Expand Up @@ -38,13 +39,13 @@ impl From<StorageErr> for LimitadorError {
}
}

impl From<ConditionParsingError> for LimitadorError {
fn from(err: ConditionParsingError) -> Self {
impl From<EvaluationError> for LimitadorError {
fn from(err: EvaluationError) -> Self {
LimitadorError::InterpreterError(err)
}
}

impl From<Infallible> for LimitadorError {
impl From<Infallible> for ParseError {
fn from(value: Infallible) -> Self {
unreachable!("unexpected infallible value: {:?}", value)
}
Expand Down
Loading

0 comments on commit 80c994d

Please sign in to comment.