1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-16 23:43:00 +00:00
hls_m3u8/src/types/decimal_floating_point.rs

153 lines
3.9 KiB
Rust
Raw Normal View History

2019-09-22 18:33:40 +00:00
use core::ops::Deref;
use core::str::FromStr;
use derive_more::Display;
2019-09-13 14:06:52 +00:00
use crate::Error;
2019-09-06 11:20:40 +00:00
/// Non-negative decimal floating-point number.
///
/// See: [4.2. Attribute Lists]
///
2019-09-22 18:33:40 +00:00
/// [4.2. Attribute Lists]:
/// https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-05#section-4.2
#[derive(Default, Debug, Clone, Copy, PartialEq, PartialOrd, Display)]
2019-09-15 16:54:25 +00:00
pub(crate) struct DecimalFloatingPoint(f64);
2019-09-06 11:20:40 +00:00
impl DecimalFloatingPoint {
2019-09-22 18:33:40 +00:00
/// Makes a new [DecimalFloatingPoint] instance.
2019-09-06 11:20:40 +00:00
///
/// # Errors
///
/// The given value must have a positive sign and be finite,
/// otherwise this function will return an error that has the kind `ErrorKind::InvalidInput`.
2019-09-22 18:33:40 +00:00
pub fn new(value: f64) -> crate::Result<Self> {
if value.is_sign_negative() || value.is_infinite() {
2019-09-13 14:06:52 +00:00
return Err(Error::invalid_input());
}
2019-09-22 18:33:40 +00:00
Ok(Self(value))
2019-09-06 11:20:40 +00:00
}
2019-09-22 18:33:40 +00:00
pub(crate) const fn from_f64_unchecked(value: f64) -> Self {
Self(value)
2019-09-06 11:20:40 +00:00
}
2019-09-22 18:33:40 +00:00
/// Converts [DecimalFloatingPoint] to [f64].
pub const fn as_f64(self) -> f64 {
self.0
2019-09-06 11:20:40 +00:00
}
}
impl Eq for DecimalFloatingPoint {}
2019-09-22 18:33:40 +00:00
// this trait is implemented manually, so it doesn't construct a [DecimalFloatingPoint],
// with a negative value.
2019-09-06 11:20:40 +00:00
impl FromStr for DecimalFloatingPoint {
type Err = Error;
2019-09-13 14:06:52 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
2019-09-22 18:33:40 +00:00
Self::new(input.parse()?)
}
}
impl Deref for DecimalFloatingPoint {
type Target = f64;
fn deref(&self) -> &Self::Target {
&self.0
2019-09-06 11:20:40 +00:00
}
}
2019-09-06 11:46:21 +00:00
2019-09-22 08:57:28 +00:00
impl From<f64> for DecimalFloatingPoint {
fn from(value: f64) -> Self {
2019-09-22 18:33:40 +00:00
let mut result = value;
// guard against the unlikely case of an infinite value...
if result.is_infinite() {
result = 0.0;
}
Self(result.abs())
2019-09-22 08:57:28 +00:00
}
}
impl From<f32> for DecimalFloatingPoint {
fn from(value: f32) -> Self {
2019-09-22 18:33:40 +00:00
(value as f64).into()
2019-09-22 08:57:28 +00:00
}
}
2019-09-06 11:46:21 +00:00
#[cfg(test)]
mod tests {
use super::*;
2019-09-22 18:33:40 +00:00
macro_rules! test_from {
( $($input:expr),* ) => {
use ::core::convert::From;
#[test]
fn test_from() {
$(
assert_eq!(
DecimalFloatingPoint::from($input),
DecimalFloatingPoint::new(1.0).unwrap(),
);
)*
}
}
}
test_from![1u8, 1u16, 1u32, 1.0f32, -1.0f32, 1.0f64, -1.0f64];
2019-09-06 11:46:21 +00:00
#[test]
pub fn test_display() {
let decimal_floating_point = DecimalFloatingPoint::new(22.0).unwrap();
assert_eq!(decimal_floating_point.to_string(), "22".to_string());
let decimal_floating_point = DecimalFloatingPoint::new(4.1).unwrap();
assert_eq!(decimal_floating_point.to_string(), "4.1".to_string());
}
#[test]
2019-09-21 11:24:05 +00:00
pub fn test_parser() {
2019-09-06 11:46:21 +00:00
let decimal_floating_point = DecimalFloatingPoint::new(22.0).unwrap();
assert_eq!(
decimal_floating_point,
"22".parse::<DecimalFloatingPoint>().unwrap()
);
let decimal_floating_point = DecimalFloatingPoint::new(4.1).unwrap();
assert_eq!(
decimal_floating_point,
"4.1".parse::<DecimalFloatingPoint>().unwrap()
);
2019-09-22 18:33:40 +00:00
assert!("1#".parse::<DecimalFloatingPoint>().is_err());
assert!("-1.0".parse::<DecimalFloatingPoint>().is_err());
}
#[test]
fn test_new() {
assert!(DecimalFloatingPoint::new(::std::f64::INFINITY).is_err());
assert!(DecimalFloatingPoint::new(-1.0).is_err());
}
#[test]
fn test_as_f64() {
assert_eq!(DecimalFloatingPoint::new(1.0).unwrap().as_f64(), 1.0);
}
#[test]
fn test_from_inf() {
assert_eq!(
DecimalFloatingPoint::from(::std::f64::INFINITY),
DecimalFloatingPoint::new(0.0).unwrap()
);
}
#[test]
fn test_deref() {
assert_eq!(DecimalFloatingPoint::from(0.1).floor(), 0.0);
2019-09-06 11:46:21 +00:00
}
}