1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-02 10:10:31 +00:00
hls_m3u8/src/types/value.rs

127 lines
3.4 KiB
Rust
Raw Normal View History

use std::borrow::Cow;
use std::convert::TryFrom;
2019-10-06 14:37:14 +00:00
use std::fmt;
2020-02-24 15:45:32 +00:00
use crate::types::Float;
2019-10-06 14:37:14 +00:00
use crate::utils::{quote, unquote};
use crate::Error;
2020-02-24 15:45:32 +00:00
/// A `Value`.
2020-02-10 12:21:48 +00:00
#[non_exhaustive]
2020-02-24 15:45:32 +00:00
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum Value<'a> {
2020-02-24 15:45:32 +00:00
/// A `String`.
String(Cow<'a, str>),
2019-10-06 14:37:14 +00:00
/// A sequence of bytes.
Hex(Vec<u8>),
2020-02-24 15:45:32 +00:00
/// A floating point number, that's neither NaN nor infinite.
Float(Float),
2019-10-06 14:37:14 +00:00
}
impl<'a> Value<'a> {
/// Makes the struct independent of its lifetime, by taking ownership of all
/// internal [`Cow`]s.
///
/// # Note
///
/// This is a relatively expensive operation.
#[must_use]
pub fn into_owned(self) -> Value<'static> {
match self {
Self::String(value) => Value::String(Cow::Owned(value.into_owned())),
Self::Hex(value) => Value::Hex(value),
Self::Float(value) => Value::Float(value),
}
}
}
impl<'a> fmt::Display for Value<'a> {
2020-04-09 06:43:13 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-10-06 14:37:14 +00:00
match &self {
Self::String(value) => write!(f, "{}", quote(value)),
Self::Hex(value) => write!(f, "0x{}", hex::encode_upper(value)),
Self::Float(value) => write!(f, "{}", value),
}
}
}
impl<'a> TryFrom<&'a str> for Value<'a> {
type Error = Error;
2019-10-06 14:37:14 +00:00
fn try_from(input: &'a str) -> Result<Self, Self::Error> {
2019-10-06 14:37:14 +00:00
if input.starts_with("0x") || input.starts_with("0X") {
Ok(Self::Hex(
hex::decode(input.trim_start_matches("0x").trim_start_matches("0X"))
.map_err(Error::hex)?,
))
2019-10-06 14:37:14 +00:00
} else {
match input.parse() {
Ok(value) => Ok(Self::Float(value)),
Err(_) => Ok(Self::String(unquote(input))),
}
}
}
}
impl<T: Into<Float>> From<T> for Value<'static> {
2020-02-24 15:45:32 +00:00
fn from(value: T) -> Self { Self::Float(value.into()) }
2019-10-06 14:37:14 +00:00
}
impl From<Vec<u8>> for Value<'static> {
2019-10-06 14:37:14 +00:00
fn from(value: Vec<u8>) -> Self { Self::Hex(value) }
}
impl From<String> for Value<'static> {
fn from(value: String) -> Self { Self::String(Cow::Owned(unquote(&value).into_owned())) }
2019-10-06 14:37:14 +00:00
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
2019-10-06 14:37:14 +00:00
#[test]
fn test_display() {
2020-02-24 15:45:32 +00:00
assert_eq!(Value::Float(Float::new(1.1)).to_string(), "1.1".to_string());
2019-10-06 14:37:14 +00:00
assert_eq!(
Value::String("&str".into()).to_string(),
2019-10-06 14:37:14 +00:00
"\"&str\"".to_string()
);
assert_eq!(
Value::Hex(vec![1, 2, 3]).to_string(),
"0x010203".to_string()
);
}
#[test]
fn test_parser() {
assert_eq!(
Value::Float(Float::new(1.1)),
Value::try_from("1.1").unwrap()
);
assert_eq!(
Value::String("&str".into()),
Value::try_from("\"&str\"").unwrap()
);
assert_eq!(
Value::Hex(vec![1, 2, 3]),
Value::try_from("0x010203").unwrap()
);
assert_eq!(
Value::Hex(vec![1, 2, 3]),
Value::try_from("0X010203").unwrap()
2019-10-06 14:37:14 +00:00
);
assert!(Value::try_from("0x010203Z").is_err());
2019-10-06 14:37:14 +00:00
}
#[test]
fn test_from() {
2020-02-24 15:45:32 +00:00
assert_eq!(Value::from(1_u8), Value::Float(Float::new(1.0)));
2019-10-06 14:37:14 +00:00
assert_eq!(
Value::from("&str".to_string()),
Value::String("&str".into())
2019-10-06 14:37:14 +00:00
);
assert_eq!(Value::from(vec![1, 2, 3]), Value::Hex(vec![1, 2, 3]));
}
}