refactor(headers): Use u16 based newtype for quality value

Using floating point numbers is problematic because comparison is inexact.
They also take more space than integral numbers in this case.

Add `FromPrimitve`, `ToPrimitive` and `Default` traits to quality newtype.

Closes: #330

BREAKING_CHANGE: Replace f32 quality values in quality items with a
Quality(u16) newtype. Valid values are from 0 to 1000.
This commit is contained in:
Pyfisch
2015-02-22 16:51:53 +01:00
parent b1b8bf1db7
commit 8f6ce453de
6 changed files with 202 additions and 114 deletions

View File

@@ -33,7 +33,8 @@ impl_list_header!(Accept,
mod tests {
use mime::*;
use header::{Header, QualityItem, qitem};
use header::{Header, Quality, QualityItem, qitem};
use super::Accept;
#[test]
@@ -49,7 +50,7 @@ mod tests {
fn test_parse_header_with_quality() {
let a: Accept = Header::parse_header([b"text/plain; charset=utf-8; q=0.5".to_vec()].as_slice()).unwrap();
let b = Accept(vec![
QualityItem::new(Mime(TopLevel::Text, SubLevel::Plain, vec![(Attr::Charset, Value::Utf8)]), 0.5f32),
QualityItem::new(Mime(TopLevel::Text, SubLevel::Plain, vec![(Attr::Charset, Value::Utf8)]), Quality(500)),
]);
assert_eq!(a, b);
}