hyper 0.1.11

A modern HTTP library.
//! Provides a struct for quality values.
//!
//! [RFC7231 Section 5.3.1](https://tools.ietf.org/html/rfc7231#section-5.3.1)
//! gives more information on quality values in HTTP header fields.

use std::fmt;
use std::str;
#[cfg(test)] use super::encoding::*;

/// Represents an item with a quality value as defined in
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-5.3.1).
#[derive(Clone, PartialEq, Debug)]
pub struct QualityItem<T> {
    /// The actual contents of the field.
    pub item: T,
    /// The quality (client or server preference) for the value.
    pub quality: f32,
}

impl<T> QualityItem<T> {
    /// Creates a new `QualityItem` from an item and a quality.
    /// The item can be of any type.
    /// The quality should be a value in the range [0, 1].
    pub fn new(item: T, quality: f32) -> QualityItem<T> {
        QualityItem{item: item, quality: quality}
    }
}

impl<T: fmt::Display> fmt::Display for QualityItem<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if self.quality == 1.0 {
            write!(f, "{}", self.item)
        } else {
            write!(f, "{}; q={}", self.item,
                   format!("{:.3}", self.quality).trim_right_matches(&['0', '.'][]))
        }
    }
}

impl<T: str::FromStr> str::FromStr for QualityItem<T> {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, ()> {
        // Set defaults used if parsing fails.
        let mut raw_item = s;
        let mut quality = 1f32;

        let parts: Vec<&str> = s.rsplitn(1, ';').map(|x| x.trim()).collect();
        if parts.len() == 2 {
            let start = &parts[0][0..2];
            if start == "q=" || start == "Q=" {
                let q_part = &parts[0][2..parts[0].len()];
                if q_part.len() > 5 {
                    return Err(());
                }
                let x: Result<f32, _> = q_part.parse();
                match x {
                    Ok(q_value) => {
                        if 0f32 <= q_value && q_value <= 1f32 {
                            quality = q_value;
                            raw_item = parts[1];
                            } else {
                                return Err(());
                            }
                        },
                    Err(_) => return Err(()),
                }
            }
        }
        let x: Result<T, _> = raw_item.parse();
        match x {
            Ok(item) => {
                Ok(QualityItem{ item: item, quality: quality, })
            },
            Err(_) => return Err(()),
        }
    }
}

/// Convinience function to wrap a value in a `QualityItem`
/// Sets `q` to the default 1.0
pub fn qitem<T>(item: T) -> QualityItem<T> {
    QualityItem::new(item, 1.0)
}

#[test]
fn test_quality_item_show1() {
    let x = qitem(Chunked);
    assert_eq!(format!("{}", x), "chunked");
}
#[test]
fn test_quality_item_show2() {
    let x = QualityItem::new(Chunked, 0.001);
    assert_eq!(format!("{}", x), "chunked; q=0.001");
}
#[test]
fn test_quality_item_show3() {
    // Custom value
    let x = QualityItem{
        item: EncodingExt("identity".to_string()),
        quality: 0.5f32,
    };
    assert_eq!(format!("{}", x), "identity; q=0.5");
}

#[test]
fn test_quality_item_from_str1() {
    let x: Result<QualityItem<Encoding>, ()> = "chunked".parse();
    assert_eq!(x.unwrap(), QualityItem{ item: Chunked, quality: 1f32, });
}
#[test]
fn test_quality_item_from_str2() {
    let x: Result<QualityItem<Encoding>, ()> = "chunked; q=1".parse();
    assert_eq!(x.unwrap(), QualityItem{ item: Chunked, quality: 1f32, });
}
#[test]
fn test_quality_item_from_str3() {
    let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.5".parse();
    assert_eq!(x.unwrap(), QualityItem{ item: Gzip, quality: 0.5f32, });
}
#[test]
fn test_quality_item_from_str4() {
    let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.273".parse();
    assert_eq!(x.unwrap(), QualityItem{ item: Gzip, quality: 0.273f32, });
}
#[test]
fn test_quality_item_from_str5() {
    let x: Result<QualityItem<Encoding>, ()> = "gzip; q=0.2739999".parse();
    assert_eq!(x, Err(()));
}