use std::fmt;
use super::{extract_variables, write_variables};
use crate::commands::originate::{OriginateError, Variables};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct AudioEndpoint {
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub destination: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub variables: Option<Variables>,
}
impl AudioEndpoint {
pub fn new() -> Self {
Self::default()
}
pub fn with_destination(mut self, destination: impl Into<String>) -> Self {
self.destination = Some(destination.into());
self
}
pub fn with_variables(mut self, variables: Variables) -> Self {
self.variables = Some(variables);
self
}
pub fn fmt_with_prefix(&self, f: &mut fmt::Formatter<'_>, prefix: &str) -> fmt::Result {
write_variables(f, &self.variables)?;
match &self.destination {
Some(dest) => write!(f, "{}/{}", prefix, dest),
None => f.write_str(prefix),
}
}
pub fn parse_with_prefix(s: &str, prefix: &str) -> Result<Self, OriginateError> {
let (variables, uri) = extract_variables(s)?;
let rest = uri
.strip_prefix(prefix)
.ok_or_else(|| OriginateError::ParseError(format!("not a {} endpoint", prefix)))?;
let destination = rest
.strip_prefix('/')
.and_then(|d| {
if d.is_empty() {
None
} else {
Some(d.to_string())
}
});
Ok(Self {
destination,
variables,
})
}
}
impl fmt::Display for AudioEndpoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fmt_with_prefix(f, "audio")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_with_prefix_portaudio() {
let ep = AudioEndpoint::parse_with_prefix("portaudio/auto_answer", "portaudio").unwrap();
assert_eq!(
ep.destination
.as_deref(),
Some("auto_answer")
);
assert!(ep
.variables
.is_none());
}
#[test]
fn parse_with_prefix_bare() {
let ep = AudioEndpoint::parse_with_prefix("alsa", "alsa").unwrap();
assert!(ep
.destination
.is_none());
}
#[test]
fn parse_with_prefix_trailing_slash() {
let ep = AudioEndpoint::parse_with_prefix("pulseaudio/", "pulseaudio").unwrap();
assert!(ep
.destination
.is_none());
}
#[test]
fn parse_with_prefix_wrong_module() {
let result = AudioEndpoint::parse_with_prefix("portaudio/x", "alsa");
assert!(result.is_err());
}
}