Skip to main content

freeswitch_types/event/
format.rs

1use std::fmt;
2use std::str::FromStr;
3
4/// Event format types supported by FreeSWITCH ESL
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7#[non_exhaustive]
8pub enum EventFormat {
9    /// Plain text format (default)
10    Plain,
11    /// JSON format
12    Json,
13    /// XML format
14    Xml,
15}
16
17impl EventFormat {
18    /// Determine event format from a Content-Type header value.
19    ///
20    /// Returns `Err` for unrecognized content types to avoid silently
21    /// misparsing events if FreeSWITCH adds a new format.
22    pub fn from_content_type(ct: &str) -> Result<Self, ParseEventFormatError> {
23        match ct {
24            "text/event-json" => Ok(Self::Json),
25            "text/event-xml" => Ok(Self::Xml),
26            "text/event-plain" => Ok(Self::Plain),
27            _ => Err(ParseEventFormatError(ct.to_string())),
28        }
29    }
30}
31
32impl fmt::Display for EventFormat {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            EventFormat::Plain => write!(f, "plain"),
36            EventFormat::Json => write!(f, "json"),
37            EventFormat::Xml => write!(f, "xml"),
38        }
39    }
40}
41
42impl FromStr for EventFormat {
43    type Err = ParseEventFormatError;
44
45    fn from_str(s: &str) -> Result<Self, Self::Err> {
46        if s.eq_ignore_ascii_case("plain") {
47            Ok(Self::Plain)
48        } else if s.eq_ignore_ascii_case("json") {
49            Ok(Self::Json)
50        } else if s.eq_ignore_ascii_case("xml") {
51            Ok(Self::Xml)
52        } else {
53            Err(ParseEventFormatError(s.to_string()))
54        }
55    }
56}
57
58parse_error! { ParseEventFormatError("event format"); }
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_event_format_from_str() {
66        assert_eq!("plain".parse::<EventFormat>(), Ok(EventFormat::Plain));
67        assert_eq!("json".parse::<EventFormat>(), Ok(EventFormat::Json));
68        assert_eq!("xml".parse::<EventFormat>(), Ok(EventFormat::Xml));
69        assert!("foo"
70            .parse::<EventFormat>()
71            .is_err());
72    }
73
74    #[test]
75    fn test_event_format_from_str_case_insensitive() {
76        assert_eq!("PLAIN".parse::<EventFormat>(), Ok(EventFormat::Plain));
77        assert_eq!("Json".parse::<EventFormat>(), Ok(EventFormat::Json));
78        assert_eq!("XML".parse::<EventFormat>(), Ok(EventFormat::Xml));
79        assert_eq!("Xml".parse::<EventFormat>(), Ok(EventFormat::Xml));
80    }
81
82    #[test]
83    fn test_event_format_from_content_type() {
84        assert_eq!(
85            EventFormat::from_content_type("text/event-json"),
86            Ok(EventFormat::Json)
87        );
88        assert_eq!(
89            EventFormat::from_content_type("text/event-xml"),
90            Ok(EventFormat::Xml)
91        );
92        assert_eq!(
93            EventFormat::from_content_type("text/event-plain"),
94            Ok(EventFormat::Plain)
95        );
96        assert!(EventFormat::from_content_type("unknown").is_err());
97    }
98}