use std::fmt;
use freeswitch_types::VARIABLE_PREFIX;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VarName(String);
impl VarName {
pub fn new(bare: impl Into<String>) -> Self {
VarName(bare.into())
}
pub fn from_prefixed(name: &str) -> Self {
VarName(
name.strip_prefix(VARIABLE_PREFIX)
.unwrap_or(name)
.to_string(),
)
}
pub fn bare(&self) -> &str {
&self.0
}
pub fn to_prefixed(&self) -> String {
freeswitch_types::variable_key(&self.0)
}
}
impl fmt::Display for VarName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{VARIABLE_PREFIX}{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_prefixed_strips_once() {
assert_eq!(
VarName::from_prefixed("variable_sip_call_id").bare(),
"sip_call_id"
);
assert_eq!(VarName::from_prefixed("sip_call_id").bare(), "sip_call_id");
assert_eq!(
VarName::from_prefixed("variable_variable_odd").bare(),
"variable_odd"
);
}
#[test]
fn renders_the_prefixed_spelling() {
let name = VarName::new("sip_call_id");
assert_eq!(name.to_string(), "variable_sip_call_id");
assert_eq!(name.to_prefixed(), "variable_sip_call_id");
}
}