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
This commit is contained in:
		| @@ -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<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 { | ||||
| @@ -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) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user