use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Default, Debug, PartialEq, Eq, Copy, Clone, Serialize, Deserialize)]
pub enum RTCBundlePolicy {
#[default]
Unspecified = 0,
#[serde(rename = "balanced")]
Balanced = 1,
#[serde(rename = "max-compat")]
MaxCompat = 2,
#[serde(rename = "max-bundle")]
MaxBundle = 3,
}
const BUNDLE_POLICY_BALANCED_STR: &str = "balanced";
const BUNDLE_POLICY_MAX_COMPAT_STR: &str = "max-compat";
const BUNDLE_POLICY_MAX_BUNDLE_STR: &str = "max-bundle";
impl From<&str> for RTCBundlePolicy {
fn from(raw: &str) -> Self {
match raw {
BUNDLE_POLICY_BALANCED_STR => RTCBundlePolicy::Balanced,
BUNDLE_POLICY_MAX_COMPAT_STR => RTCBundlePolicy::MaxCompat,
BUNDLE_POLICY_MAX_BUNDLE_STR => RTCBundlePolicy::MaxBundle,
_ => RTCBundlePolicy::Unspecified,
}
}
}
impl fmt::Display for RTCBundlePolicy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
RTCBundlePolicy::Balanced => write!(f, "{BUNDLE_POLICY_BALANCED_STR}"),
RTCBundlePolicy::MaxCompat => write!(f, "{BUNDLE_POLICY_MAX_COMPAT_STR}"),
RTCBundlePolicy::MaxBundle => write!(f, "{BUNDLE_POLICY_MAX_BUNDLE_STR}"),
_ => write!(f, "{}", crate::UNSPECIFIED_STR),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_new_bundle_policy() {
let tests = vec![
("Unspecified", RTCBundlePolicy::Unspecified),
("balanced", RTCBundlePolicy::Balanced),
("max-compat", RTCBundlePolicy::MaxCompat),
("max-bundle", RTCBundlePolicy::MaxBundle),
];
for (policy_string, expected_policy) in tests {
assert_eq!(RTCBundlePolicy::from(policy_string), expected_policy);
}
}
#[test]
fn test_bundle_policy_string() {
let tests = vec![
(RTCBundlePolicy::Unspecified, "Unspecified"),
(RTCBundlePolicy::Balanced, "balanced"),
(RTCBundlePolicy::MaxCompat, "max-compat"),
(RTCBundlePolicy::MaxBundle, "max-bundle"),
];
for (policy, expected_string) in tests {
assert_eq!(policy.to_string(), expected_string);
}
}
}