use std::fmt;
wire_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MuteAction {
Mute => "mute",
Unmute => "unmute",
}
error ParseMuteActionError("mute action");
tests: mute_action_tests;
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConferenceMute {
pub name: String,
pub action: MuteAction,
pub member: String,
}
impl ConferenceMute {
pub fn new(name: impl Into<String>, action: MuteAction, member: impl Into<String>) -> Self {
Self {
name: name.into(),
action,
member: member.into(),
}
}
}
impl fmt::Display for ConferenceMute {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"conference {} {} {}",
self.name, self.action, self.member
)
}
}
wire_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HoldAction {
Hold => "hold",
Unhold => "unhold",
}
error ParseHoldActionError("hold action");
tests: hold_action_tests;
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConferenceHold {
pub name: String,
pub action: HoldAction,
pub member: String,
pub stream: Option<String>,
}
impl ConferenceHold {
pub fn new(name: impl Into<String>, action: HoldAction, member: impl Into<String>) -> Self {
Self {
name: name.into(),
action,
member: member.into(),
stream: None,
}
}
pub fn with_stream(mut self, stream: impl Into<String>) -> Self {
self.stream = Some(stream.into());
self
}
}
impl fmt::Display for ConferenceHold {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"conference {} {} {}",
self.name, self.action, self.member
)?;
if let Some(ref stream) = self.stream {
write!(f, " {}", stream)?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ConferenceDtmf {
pub name: String,
pub member: String,
pub dtmf: String,
}
impl ConferenceDtmf {
pub fn new(
name: impl Into<String>,
member: impl Into<String>,
dtmf: impl Into<String>,
) -> Self {
Self {
name: name.into(),
member: member.into(),
dtmf: dtmf.into(),
}
}
}
impl fmt::Display for ConferenceDtmf {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"conference {} dtmf {} {}",
self.name, self.member, self.dtmf
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn conference_mute_renders_both_actions() {
for (action, wire) in [
(MuteAction::Mute, "conference conf1 mute 5"),
(MuteAction::Unmute, "conference conf1 unmute 5"),
] {
assert_eq!(ConferenceMute::new("conf1", action, "5").to_string(), wire);
}
}
#[test]
fn conference_hold_renders_both_actions_and_the_stream() {
assert_eq!(
ConferenceHold::new("conf1", HoldAction::Hold, "all").to_string(),
"conference conf1 hold all"
);
assert_eq!(
ConferenceHold::new("conf1", HoldAction::Unhold, "all").to_string(),
"conference conf1 unhold all"
);
assert_eq!(
ConferenceHold::new("conf1", HoldAction::Hold, "all")
.with_stream("local_stream://moh")
.to_string(),
"conference conf1 hold all local_stream://moh"
);
}
#[test]
fn conference_dtmf_renders() {
assert_eq!(
ConferenceDtmf::new("conf1", "all", "1234").to_string(),
"conference conf1 dtmf all 1234"
);
}
}