Skip to main content

freeswitch_types/variables/
mod.rs

1//! Channel variable types: format parsers (`ARRAY::`, SIP multipart) and typed
2//! variable name enums.
3
4mod conference;
5mod core;
6mod core_media;
7mod esl_array;
8#[cfg(feature = "esl")]
9mod esl_headers;
10mod loopback;
11mod sip_multipart;
12mod sip_passthrough;
13mod sofia;
14
15pub use self::core::{ChannelVariable, ParseChannelVariableError};
16pub use conference::{ConferenceVariable, ParseConferenceVariableError};
17pub use core_media::{CoreMediaVariable, ParseCoreMediaVariableError, RtpStatUnit};
18pub use esl_array::{EslArray, EslArrayError, MAX_ARRAY_ITEMS};
19#[cfg(feature = "esl")]
20pub use esl_headers::EslHeaders;
21pub use loopback::{
22    LoopbackChannelName, LoopbackHangupCause, LoopbackLeg, LoopbackResignation, LoopbackVariable,
23    ParseLoopbackHangupCauseError, ParseLoopbackLegError, ParseLoopbackVariableError,
24};
25pub use sip_multipart::{MultipartBody, MultipartBodyError, MultipartItem};
26pub use sip_passthrough::{
27    InvalidHeaderName, ParseSipPassthroughError, SipHeaderPrefix, SipPassthroughHeader,
28};
29pub use sofia::{CarriedHeader, ParseSofiaVariableError, SofiaVariable};
30
31/// Trait for typed channel variable name enums.
32///
33/// Implement this on variable name enums to use them with
34/// [`HeaderLookup::variable()`](crate::HeaderLookup::variable) and
35/// [`variable_str()`](crate::HeaderLookup::variable_str).
36/// For variables not covered by any typed enum, use `variable_str()`.
37pub trait VariableName {
38    /// Wire-format variable name (e.g. `"sip_call_id"`).
39    fn as_str(&self) -> &str;
40
41    /// Full event header name, prefixed (e.g. `"variable_sip_call_id"`).
42    ///
43    /// For callers naming a variable with no event in hand -- a config entry, a
44    /// filter, a request field. Reading one out of a store is
45    /// [`HeaderLookup::variable()`](crate::HeaderLookup::variable), which takes
46    /// the bare name.
47    fn header_name(&self) -> String {
48        crate::lookup::variable_key(self.as_str())
49    }
50}
51
52/// Forwards to the inherent `as_str()` a `define_header_enum!` enum already has.
53macro_rules! impl_variable_name {
54    ($($enum:ident),+ $(,)?) => {
55        $(
56            impl VariableName for $enum {
57                fn as_str(&self) -> &str {
58                    $enum::as_str(self)
59                }
60            }
61        )+
62    };
63}
64
65impl_variable_name!(
66    ChannelVariable,
67    SofiaVariable,
68    CoreMediaVariable,
69    LoopbackVariable,
70    ConferenceVariable,
71);
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76    use crate::{HeaderLookup, VARIABLE_PREFIX};
77    use sip_header::SipHeader;
78    use std::collections::HashMap;
79
80    #[test]
81    fn header_name_composes_the_wire_key() {
82        assert_eq!(
83            ChannelVariable::ReadCodec.header_name(),
84            "variable_read_codec"
85        );
86        assert_eq!(
87            LoopbackVariable::LoopbackLeg.header_name(),
88            "variable_loopback_leg"
89        );
90        assert!(ChannelVariable::ReadCodec
91            .header_name()
92            .starts_with(VARIABLE_PREFIX));
93    }
94
95    // SipPassthroughHeader's as_str borrows from self, so the provided method
96    // must stay &self-borrowing to cover it.
97    #[test]
98    fn header_name_covers_a_borrowed_as_str() {
99        let h = SipPassthroughHeader::invite(SipHeader::CallInfo);
100        assert_eq!(h.header_name(), "variable_sip_i_call_info");
101    }
102
103    // What a downstream crate defining its own variable enum gets for free.
104    #[test]
105    fn an_external_implementor_supplies_only_as_str() {
106        struct TenantVariable;
107
108        impl VariableName for TenantVariable {
109            fn as_str(&self) -> &str {
110                "tenant_id"
111            }
112        }
113
114        assert_eq!(TenantVariable.header_name(), "variable_tenant_id");
115    }
116
117    #[test]
118    fn header_name_is_the_key_a_header_store_holds() {
119        let mut map: HashMap<String, String> = HashMap::new();
120        map.insert(ChannelVariable::ReadCodec.header_name(), "PCMU".into());
121        assert_eq!(map.variable(ChannelVariable::ReadCodec), Some("PCMU"));
122    }
123}