Skip to content

Commit

Permalink
feat(headers): add q function to ease creating Quality values
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Mar 4, 2015
1 parent 9e07708 commit d68773c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use unicase::UniCase;
use self::cell::OptCell;
use {http, HttpResult, HttpError};

pub use self::shared::{Encoding, EntityTag, Quality, QualityItem, qitem};
pub use self::shared::{Encoding, EntityTag, Quality, QualityItem, qitem, q};
pub use self::common::*;

mod cell;
Expand Down
2 changes: 1 addition & 1 deletion src/header/shared/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use self::encoding::Encoding;
pub use self::entity::EntityTag;
pub use self::quality_item::{Quality, QualityItem, qitem};
pub use self::quality_item::{Quality, QualityItem, qitem, q};

mod encoding;
mod entity;
Expand Down
10 changes: 7 additions & 3 deletions src/header/shared/quality_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ use std::str;

/// Represents a quality used in quality values.
///
/// `Quality` should only be created using the `FromPrimitve` trait methods `from_f32` and
/// `from_f64`, they take a value between 0.0 and 1.0. To create a quality with the value 1.0, the
/// default you can use `let q: Quality = Default::default()`.
/// Can be created with the `q` function.
///
/// # Implementation notes
///
/// The quality value is defined as a number between 0 and 1 with three decimal places. This means
/// there are 1000 possible values. Since floating point numbers are not exact and the smallest
/// floating point data type (`f32`) consumes four bytes, hyper uses an `u16` value to store the
Expand Down Expand Up @@ -149,6 +148,11 @@ pub fn qitem<T>(item: T) -> QualityItem<T> {
QualityItem::new(item, Default::default())
}

/// Convenience function to create a `Quality` fromt a float.
pub fn q(f: f32) -> Quality {
FromPrimitive::from_f32(f).expect("q value must be between 0.0 and 1.0")
}

#[cfg(test)]
mod tests {
use std::num::FromPrimitive;
Expand Down

0 comments on commit d68773c

Please sign in to comment.