Skip to content

Commit 51bf112

Browse files
committed
Merge pull request #319 from pyfisch/partialordqualityitem
feat(headers): Implement PartialOrd for QualityItem
2 parents f8776f4 + 2859d7e commit 51bf112

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/header/shared/quality_item.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
use std::fmt;
77
use std::str;
8+
use std::cmp;
89
#[cfg(test)] use super::encoding::*;
910

1011
/// Represents an item with a quality value as defined in
@@ -26,6 +27,12 @@ impl<T> QualityItem<T> {
2627
}
2728
}
2829

30+
impl<T: PartialEq> cmp::PartialOrd for QualityItem<T> {
31+
fn partial_cmp(&self, other: &QualityItem<T>) -> Option<cmp::Ordering> {
32+
self.quality.partial_cmp(&other.quality)
33+
}
34+
}
35+
2936
impl<T: fmt::Display> fmt::Display for QualityItem<T> {
3037
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3138
if self.quality == 1.0 {
@@ -127,3 +134,10 @@ fn test_quality_item_from_str5() {
127134
let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.2739999".parse();
128135
assert_eq!(x, Err(()));
129136
}
137+
#[test]
138+
fn test_quality_item_ordering() {
139+
let x: QualityItem<Encoding> = "gzip; q=0.5".parse().ok().unwrap();
140+
let y: QualityItem<Encoding> = "gzip; q=0.273".parse().ok().unwrap();
141+
let comparision_result: bool = x.gt(&y);
142+
assert_eq!(comparision_result, true)
143+
}

0 commit comments

Comments
 (0)