1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-07 07:59:21 +00:00
hls_m3u8/src/types/signed_decimal_floating_point.rs

56 lines
1.4 KiB
Rust
Raw Normal View History

2019-09-06 11:20:40 +00:00
use std::fmt;
2019-09-15 17:09:48 +00:00
use std::str::FromStr;
2019-09-13 14:06:52 +00:00
use crate::Error;
2019-09-06 11:20:40 +00:00
/// Signed decimal floating-point number.
///
/// See: [4.2. Attribute Lists]
///
/// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(crate) struct SignedDecimalFloatingPoint(f64);
2019-09-06 11:20:40 +00:00
impl SignedDecimalFloatingPoint {
/// Makes a new `SignedDecimalFloatingPoint` instance.
///
/// # Errors
///
/// The given value must be finite,
/// otherwise this function will return an error that has the kind `ErrorKind::InvalidInput`.
2019-09-13 14:06:52 +00:00
pub fn new(n: f64) -> crate::Result<Self> {
if !n.is_finite() {
Err(Error::invalid_input())
} else {
Ok(SignedDecimalFloatingPoint(n))
}
2019-09-06 11:20:40 +00:00
}
/// Converts `DecimalFloatingPoint` to `f64`.
2019-09-08 10:23:33 +00:00
pub const fn as_f64(self) -> f64 {
2019-09-06 11:20:40 +00:00
self.0
}
}
impl From<i32> for SignedDecimalFloatingPoint {
fn from(f: i32) -> Self {
SignedDecimalFloatingPoint(f64::from(f))
}
}
impl Eq for SignedDecimalFloatingPoint {}
impl fmt::Display for SignedDecimalFloatingPoint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for SignedDecimalFloatingPoint {
type Err = Error;
2019-09-13 14:06:52 +00:00
fn from_str(input: &str) -> Result<Self, Self::Err> {
SignedDecimalFloatingPoint::new(input.parse().map_err(Error::parse_float_error)?)
2019-09-06 11:20:40 +00:00
}
}