1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-17 04:20:31 +00:00

improvements to Value

This commit is contained in:
Luro02 2020-02-24 16:45:32 +01:00
parent 6333a80507
commit a262c77c58
No known key found for this signature in database
GPG key ID: B66FD4F74501A9CF

View file

@ -1,21 +1,20 @@
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use hex; use crate::types::Float;
use crate::utils::{quote, unquote}; use crate::utils::{quote, unquote};
use crate::Error; use crate::Error;
/// A [`Value`]. /// A `Value`.
#[non_exhaustive] #[non_exhaustive]
#[derive(Debug, Clone, PartialEq, PartialOrd)] #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum Value { pub enum Value {
/// A [`String`]. /// A `String`.
String(String), String(String),
/// A sequence of bytes. /// A sequence of bytes.
Hex(Vec<u8>), Hex(Vec<u8>),
/// A floating point number, that's neither NaN nor infinite! /// A floating point number, that's neither NaN nor infinite.
Float(f64), Float(Float),
} }
impl fmt::Display for Value { impl fmt::Display for Value {
@ -46,8 +45,8 @@ impl FromStr for Value {
} }
} }
impl From<f64> for Value { impl<T: Into<Float>> From<T> for Value {
fn from(value: f64) -> Self { Self::Float(value) } fn from(value: T) -> Self { Self::Float(value.into()) }
} }
impl From<Vec<u8>> for Value { impl From<Vec<u8>> for Value {
@ -62,10 +61,6 @@ impl From<&str> for Value {
fn from(value: &str) -> Self { Self::String(unquote(value)) } fn from(value: &str) -> Self { Self::String(unquote(value)) }
} }
// impl<T: AsRef<[u8]>> From<T> for Value {
// fn from(value: T) -> Self { Self::Hex(value.as_ref().into()) }
// }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -73,7 +68,7 @@ mod tests {
#[test] #[test]
fn test_display() { fn test_display() {
assert_eq!(Value::Float(1.1).to_string(), "1.1".to_string()); assert_eq!(Value::Float(Float::new(1.1)).to_string(), "1.1".to_string());
assert_eq!( assert_eq!(
Value::String("&str".to_string()).to_string(), Value::String("&str".to_string()).to_string(),
"\"&str\"".to_string() "\"&str\"".to_string()
@ -86,7 +81,7 @@ mod tests {
#[test] #[test]
fn test_parser() { fn test_parser() {
assert_eq!(Value::Float(1.1), "1.1".parse().unwrap()); assert_eq!(Value::Float(Float::new(1.1)), "1.1".parse().unwrap());
assert_eq!( assert_eq!(
Value::String("&str".to_string()), Value::String("&str".to_string()),
"\"&str\"".parse().unwrap() "\"&str\"".parse().unwrap()
@ -98,7 +93,7 @@ mod tests {
#[test] #[test]
fn test_from() { fn test_from() {
assert_eq!(Value::from(1.0_f64), Value::Float(1.0)); assert_eq!(Value::from(1_u8), Value::Float(Float::new(1.0)));
assert_eq!(Value::from("\"&str\""), Value::String("&str".to_string())); assert_eq!(Value::from("\"&str\""), Value::String("&str".to_string()));
assert_eq!( assert_eq!(
Value::from("&str".to_string()), Value::from("&str".to_string()),