-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proper impl Ord for Limit
#389
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,7 @@ impl From<String> for Namespace { | |
} | ||
} | ||
|
||
#[derive(Eq, Debug, Clone, PartialOrd, Ord, Serialize, Deserialize)] | ||
#[derive(Eq, Debug, Clone, Serialize, Deserialize)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It uses all the fields in the struct to compare... so it did consider |
||
pub struct Limit { | ||
#[serde(skip_serializing, default)] | ||
id: Option<String>, | ||
|
@@ -426,6 +426,27 @@ impl Hash for Limit { | |
} | ||
} | ||
|
||
impl PartialOrd for Limit { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for Limit { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
match self.namespace.cmp(&other.namespace) { | ||
Ordering::Equal => match self.seconds.cmp(&other.seconds) { | ||
Ordering::Equal => match self.conditions.cmp(&other.conditions) { | ||
Ordering::Equal => self.variables.cmp(&other.variables), | ||
cmp => cmp, | ||
}, | ||
cmp => cmp, | ||
}, | ||
cmp => cmp, | ||
} | ||
} | ||
} | ||
|
||
impl PartialEq for Limit { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.namespace == other.namespace | ||
|
@@ -833,6 +854,7 @@ mod conditions { | |
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::cmp::Ordering::Equal; | ||
|
||
#[test] | ||
fn limit_can_have_an_optional_name() { | ||
|
@@ -1027,4 +1049,28 @@ mod tests { | |
|
||
assert_eq!(limit.id(), Some("test_id")) | ||
} | ||
|
||
#[test] | ||
fn partial_equality() { | ||
let limit1 = Limit::with_id( | ||
"test_id", | ||
"test_namespace", | ||
42, | ||
60, | ||
vec!["req.method == 'GET'"], | ||
vec!["app_id"], | ||
); | ||
|
||
let mut limit2 = Limit::new( | ||
limit1.namespace.clone(), | ||
limit1.max_value + 10, | ||
limit1.seconds, | ||
limit1.conditions.clone(), | ||
limit1.variables.clone(), | ||
); | ||
limit2.set_name("Who cares?".to_string()); | ||
|
||
assert_eq!(limit1.partial_cmp(&limit2), Some(Equal)); | ||
assert_eq!(limit1, limit2); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was "just" to reproduce the
None.unwrap()
, thought I'd leave it in nonetheless