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() {
let cmd = ConferenceMute {
name: "conf1".into(),
action: MuteAction::Mute,
member: "5".into(),
};
assert_eq!(cmd.to_string(), "conference conf1 mute 5");
}
#[test]
fn conference_unmute() {
let cmd = ConferenceMute {
name: "conf1".into(),
action: MuteAction::Unmute,
member: "5".into(),
};
assert_eq!(cmd.to_string(), "conference conf1 unmute 5");
}
#[test]
fn conference_hold_all() {
let cmd = ConferenceHold {
name: "conf1".into(),
action: HoldAction::Hold,
member: "all".into(),
stream: None,
};
assert_eq!(cmd.to_string(), "conference conf1 hold all");
}
#[test]
fn conference_hold_with_stream() {
let cmd = ConferenceHold {
name: "conf1".into(),
action: HoldAction::Hold,
member: "all".into(),
stream: Some("local_stream://moh".into()),
};
assert_eq!(
cmd.to_string(),
"conference conf1 hold all local_stream://moh"
);
}
#[test]
fn conference_unhold() {
let cmd = ConferenceHold {
name: "conf1".into(),
action: HoldAction::Unhold,
member: "all".into(),
stream: None,
};
assert_eq!(cmd.to_string(), "conference conf1 unhold all");
}
#[test]
fn conference_dtmf() {
let cmd = ConferenceDtmf {
name: "conf1".into(),
member: "all".into(),
dtmf: "1234".into(),
};
assert_eq!(cmd.to_string(), "conference conf1 dtmf all 1234");
}
#[test]
fn mute_action_from_str() {
assert_eq!(
"mute"
.parse::<MuteAction>()
.unwrap(),
MuteAction::Mute
);
assert_eq!(
"unmute"
.parse::<MuteAction>()
.unwrap(),
MuteAction::Unmute
);
assert!("Mute"
.parse::<MuteAction>()
.is_err());
assert!("MUTE"
.parse::<MuteAction>()
.is_err());
}
#[test]
fn hold_action_from_str() {
assert_eq!(
"hold"
.parse::<HoldAction>()
.unwrap(),
HoldAction::Hold
);
assert_eq!(
"unhold"
.parse::<HoldAction>()
.unwrap(),
HoldAction::Unhold
);
assert!("Hold"
.parse::<HoldAction>()
.is_err());
assert!("HOLD"
.parse::<HoldAction>()
.is_err());
}
}