wire_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EventFormat {
Plain => "plain",
Json => "json",
Xml => "xml",
}
error ParseEventFormatError("event format");
from_str: ignore_case;
}
impl EventFormat {
pub fn from_content_type(ct: &str) -> Result<Self, ParseEventFormatError> {
match ct {
"text/event-json" => Ok(Self::Json),
"text/event-xml" => Ok(Self::Xml),
"text/event-plain" => Ok(Self::Plain),
_ => Err(ParseEventFormatError(ct.to_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_event_format_from_str() {
assert_eq!("plain".parse::<EventFormat>(), Ok(EventFormat::Plain));
assert_eq!("json".parse::<EventFormat>(), Ok(EventFormat::Json));
assert_eq!("xml".parse::<EventFormat>(), Ok(EventFormat::Xml));
assert!("foo"
.parse::<EventFormat>()
.is_err());
}
#[test]
fn test_event_format_from_str_case_insensitive() {
assert_eq!("PLAIN".parse::<EventFormat>(), Ok(EventFormat::Plain));
assert_eq!("Json".parse::<EventFormat>(), Ok(EventFormat::Json));
assert_eq!("XML".parse::<EventFormat>(), Ok(EventFormat::Xml));
assert_eq!("Xml".parse::<EventFormat>(), Ok(EventFormat::Xml));
}
#[test]
fn test_event_format_from_content_type() {
assert_eq!(
EventFormat::from_content_type("text/event-json"),
Ok(EventFormat::Json)
);
assert_eq!(
EventFormat::from_content_type("text/event-xml"),
Ok(EventFormat::Xml)
);
assert_eq!(
EventFormat::from_content_type("text/event-plain"),
Ok(EventFormat::Plain)
);
assert!(EventFormat::from_content_type("unknown").is_err());
}
}