From 2859d7ef4ecadc3927fa46292ebbb225da597690 Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Sun, 15 Feb 2015 18:01:03 +0100 Subject: [PATCH] feat(headers): Implement PartialOrd for QualityItem PartialOrd is useful to sort Collections of QualityItems by quality. So you can find the preferred match easier. Closes #314 --- src/header/shared/quality_item.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/header/shared/quality_item.rs b/src/header/shared/quality_item.rs index 0e953f05d7..a83060d60f 100644 --- a/src/header/shared/quality_item.rs +++ b/src/header/shared/quality_item.rs @@ -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 @@ -26,6 +27,12 @@ impl QualityItem { } } +impl cmp::PartialOrd for QualityItem { + fn partial_cmp(&self, other: &QualityItem) -> Option { + self.quality.partial_cmp(&other.quality) + } +} + impl fmt::Display for QualityItem { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.quality == 1.0 { @@ -127,3 +134,10 @@ fn test_quality_item_from_str5() { let x: Result, ()> = "gzip; q=0.2739999".parse(); assert_eq!(x, Err(())); } +#[test] +fn test_quality_item_ordering() { + let x: QualityItem = "gzip; q=0.5".parse().ok().unwrap(); + let y: QualityItem = "gzip; q=0.273".parse().ok().unwrap(); + let comparision_result: bool = x.gt(&y); + assert_eq!(comparision_result, true) +}