Skip to content

Commit

Permalink
Slightly rearranged/cleaned deps
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 7742644 commit db5cea2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
2 changes: 1 addition & 1 deletion limitador/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::limit::cel::EvaluationError;
use crate::limit::EvaluationError;
use crate::limit::ParseError;
use crate::storage::StorageErr;
use std::convert::Infallible;
Expand Down
50 changes: 42 additions & 8 deletions limitador/src/limit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use cel::Context;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt::Debug;
use std::hash::{Hash, Hasher};

mod cel;

pub use cel::{EvaluationError, ParseError};
pub use cel::{Expression, Predicate};

#[derive(Debug, Hash, Eq, PartialEq, Clone, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Namespace(String);

Expand Down Expand Up @@ -38,7 +44,7 @@ pub struct Limit {

// Need to sort to generate the same object when using the JSON as a key or
// value in Redis.
conditions: BTreeSet<cel::Predicate>,
conditions: BTreeSet<Predicate>,
variables: BTreeSet<String>,
}

Expand Down Expand Up @@ -202,13 +208,6 @@ impl PartialEq for Limit {
}
}

use crate::limit::cel::{Context, Predicate};
pub use cel::Expression as CelExpression;
pub use cel::ParseError;
pub use cel::Predicate as CelPredicate;

pub(super) mod cel;

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -352,4 +351,39 @@ mod tests {
assert_eq!(limit1.partial_cmp(&limit2), Some(Equal));
assert_eq!(limit1, limit2);
}

#[test]
fn conditions_have_limit_info() {
let mut limit = Limit::new(
"ns",
42,
10,
vec!["limit.name == 'named_limit'"],
Vec::<String>::default(),
)
.expect("failed to create");
assert!(!limit.applies(&HashMap::default()));
limit.set_name("named_limit".to_string());
assert!(limit.applies(&HashMap::default()));
let limit = Limit::with_id(
"my_id",
"ns",
42,
10,
vec!["limit.id == 'my_id'"],
Vec::<String>::default(),
)
.expect("failed to create");
assert!(limit.applies(&HashMap::default()));
let limit = Limit::with_id(
"my_id",
"ns",
42,
10,
vec!["limit.id == 'other_id'"],
Vec::<String>::default(),
)
.expect("failed to create");
assert!(!limit.applies(&HashMap::default()));
}
}
43 changes: 3 additions & 40 deletions limitador/src/limit/cel.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::limit::Limit;
use cel_interpreter::{ExecutionError, Value};
pub use errors::ParseError;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::sync::Arc;

pub use errors::{EvaluationError, ParseError};

pub(super) mod errors {
use cel_interpreter::ExecutionError;
use std::error::Error;
Expand Down Expand Up @@ -72,8 +73,6 @@ pub(super) mod errors {
}
}

pub use errors::EvaluationError;

pub struct Context<'a> {
variables: HashSet<String>,
ctx: cel_interpreter::Context<'a>,
Expand Down Expand Up @@ -294,8 +293,7 @@ impl From<Predicate> for String {
#[cfg(test)]
mod tests {
use super::{Context, Expression, Predicate};
use crate::limit::Limit;
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;

#[test]
fn expression() {
Expand Down Expand Up @@ -336,41 +334,6 @@ mod tests {
);
}

#[test]
fn context_has_limit_info() {
let mut limit = Limit::new(
"ns",
42,
10,
vec!["limit.name == 'named_limit'"],
Vec::<String>::default(),
)
.expect("failed to create");
assert!(!limit.applies(&HashMap::default()));
limit.set_name("named_limit".to_string());
assert!(limit.applies(&HashMap::default()));
let limit = Limit::with_id(
"my_id",
"ns",
42,
10,
vec!["limit.id == 'my_id'"],
Vec::<String>::default(),
)
.expect("failed to create");
assert!(limit.applies(&HashMap::default()));
let limit = Limit::with_id(
"my_id",
"ns",
42,
10,
vec!["limit.id == 'other_id'"],
Vec::<String>::default(),
)
.expect("failed to create");
assert!(!limit.applies(&HashMap::default()));
}

fn ctx<'a>() -> Context<'a> {
Context {
variables: HashSet::default(),
Expand Down

0 comments on commit db5cea2

Please sign in to comment.