mod value;
mod value_range;
mod value_sequence;
use std::ops::RangeInclusive;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub use self::{
value::MediaTrackValueCapability, value_range::MediaTrackValueRangeCapability,
value_sequence::MediaTrackValueSequenceCapability,
};
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
pub enum MediaTrackCapability {
BoolSequence(MediaTrackValueSequenceCapability<bool>),
Bool(MediaTrackValueCapability<bool>),
IntegerRange(MediaTrackValueRangeCapability<u64>),
FloatRange(MediaTrackValueRangeCapability<f64>),
StringSequence(MediaTrackValueSequenceCapability<String>),
String(MediaTrackValueCapability<String>),
}
impl From<bool> for MediaTrackCapability {
fn from(capability: bool) -> Self {
Self::Bool(capability.into())
}
}
impl From<Vec<bool>> for MediaTrackCapability {
fn from(capability: Vec<bool>) -> Self {
Self::BoolSequence(capability.into())
}
}
impl From<RangeInclusive<u64>> for MediaTrackCapability {
fn from(capability: RangeInclusive<u64>) -> Self {
Self::IntegerRange(capability.into())
}
}
impl From<RangeInclusive<f64>> for MediaTrackCapability {
fn from(capability: RangeInclusive<f64>) -> Self {
Self::FloatRange(capability.into())
}
}
impl From<String> for MediaTrackCapability {
fn from(capability: String) -> Self {
Self::String(capability.into())
}
}
impl<'a> From<&'a str> for MediaTrackCapability {
fn from(capability: &'a str) -> Self {
let capability: String = capability.to_owned();
Self::from(capability)
}
}
impl From<Vec<String>> for MediaTrackCapability {
fn from(capability: Vec<String>) -> Self {
Self::StringSequence(capability.into())
}
}
impl From<Vec<&str>> for MediaTrackCapability {
fn from(capability: Vec<&str>) -> Self {
let capability: Vec<String> = capability.into_iter().map(|c| c.to_owned()).collect();
Self::from(capability)
}
}
#[cfg(test)]
mod tests {
use super::*;
type Subject = MediaTrackCapability;
mod from {
use super::*;
#[test]
fn bool_sequence() {
let actual = Subject::from(vec![false, true]);
let expected = Subject::BoolSequence(vec![false, true].into());
assert_eq!(actual, expected);
}
#[test]
fn bool() {
let actual = Subject::from(true);
let expected = Subject::Bool(true.into());
assert_eq!(actual, expected);
}
#[test]
fn integer_range() {
let actual = Subject::from(12..=34);
let expected = Subject::IntegerRange((12..=34).into());
assert_eq!(actual, expected);
}
#[test]
fn float() {
let actual = Subject::from(1.2..=3.4);
let expected = Subject::FloatRange((1.2..=3.4).into());
assert_eq!(actual, expected);
}
#[test]
fn string_sequence() {
let actual = Subject::from(vec!["foo".to_owned(), "bar".to_owned()]);
let expected = Subject::StringSequence(vec!["foo".to_owned(), "bar".to_owned()].into());
assert_eq!(actual, expected);
}
#[test]
fn string() {
let actual = Subject::from("foo".to_owned());
let expected = Subject::String("foo".to_owned().into());
assert_eq!(actual, expected);
}
}
}
#[cfg(feature = "serde")]
#[cfg(test)]
mod serde_tests {
use crate::macros::test_serde_symmetry;
use super::*;
type Subject = MediaTrackCapability;
#[test]
fn bool_sequence() {
let subject = Subject::BoolSequence(vec![false, true].into());
let json = serde_json::json!([false, true]);
test_serde_symmetry!(subject: subject, json: json);
}
#[test]
fn bool() {
let subject = Subject::Bool(true.into());
let json = serde_json::json!(true);
test_serde_symmetry!(subject: subject, json: json);
}
#[test]
fn integer_range() {
let subject = Subject::IntegerRange((12..=34).into());
let json = serde_json::json!({
"min": 12,
"max": 34,
});
test_serde_symmetry!(subject: subject, json: json);
}
#[test]
fn float() {
let subject = Subject::FloatRange((1.2..=3.4).into());
let json = serde_json::json!({
"min": 1.2,
"max": 3.4,
});
test_serde_symmetry!(subject: subject, json: json);
}
#[test]
fn string_sequence() {
let subject = Subject::StringSequence(vec!["foo".to_owned(), "bar".to_owned()].into());
let json = serde_json::json!(["foo", "bar"]);
test_serde_symmetry!(subject: subject, json: json);
}
#[test]
fn string() {
let subject = Subject::String("foo".to_owned().into());
let json = serde_json::json!("foo");
test_serde_symmetry!(subject: subject, json: json);
}
}