Skip to content

Commit

Permalink
feat(headers): Implement PartialOrd for QualityItem
Browse files Browse the repository at this point in the history
PartialOrd is useful to sort Collections of QualityItems by quality.
So you can find the preferred match easier.

Closes #314
  • Loading branch information
pyfisch committed Feb 15, 2015
1 parent f554c09 commit 2859d7e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/header/shared/quality_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use std::fmt;
use std::str;
use std::cmp;
#[cfg(test)] use super::encoding::*;

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

impl<T: PartialEq> cmp::PartialOrd for QualityItem<T> {
fn partial_cmp(&self, other: &QualityItem<T>) -> Option<cmp::Ordering> {
self.quality.partial_cmp(&other.quality)
}
}

impl<T: fmt::Display> fmt::Display for QualityItem<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.quality == 1.0 {
Expand Down Expand Up @@ -127,3 +134,10 @@ fn test_quality_item_from_str5() {
let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.2739999".parse();
assert_eq!(x, Err(()));
}
#[test]
fn test_quality_item_ordering() {
let x: QualityItem<Encoding> = "gzip; q=0.5".parse().ok().unwrap();
let y: QualityItem<Encoding> = "gzip; q=0.273".parse().ok().unwrap();
let comparision_result: bool = x.gt(&y);
assert_eq!(comparision_result, true)
}

0 comments on commit 2859d7e

Please sign in to comment.