#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InputFormat {
Protobuf,
Json,
Jsonl,
Auto,
}
impl InputFormat {
pub fn from_content_type(content_type: Option<&str>) -> Self {
let content_type = content_type.map(|v| v.trim().to_ascii_lowercase());
match content_type.as_deref() {
Some("application/x-ndjson") | Some("application/jsonl") => InputFormat::Jsonl,
Some("application/json") | Some("application/otlp+json") => InputFormat::Json,
Some("application/x-protobuf")
| Some("application/protobuf")
| Some("application/otlp") => InputFormat::Protobuf,
_ => InputFormat::Auto,
}
}
pub fn content_type(&self) -> &'static str {
match self {
InputFormat::Protobuf => "application/x-protobuf",
InputFormat::Json => "application/json",
InputFormat::Jsonl => "application/x-ndjson",
InputFormat::Auto => "application/x-protobuf",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn input_format_from_content_type_jsonl() {
assert_eq!(
InputFormat::from_content_type(Some("application/x-ndjson")),
InputFormat::Jsonl
);
assert_eq!(
InputFormat::from_content_type(Some("application/jsonl")),
InputFormat::Jsonl
);
}
#[test]
fn input_format_content_type() {
assert_eq!(
InputFormat::Protobuf.content_type(),
"application/x-protobuf"
);
assert_eq!(InputFormat::Json.content_type(), "application/json");
assert_eq!(InputFormat::Jsonl.content_type(), "application/x-ndjson");
}
}