pub mod errors;
pub mod registry_writer;
pub mod rust_writer;
pub mod types;
pub mod verifiers;
pub mod yaml_writer;
#[cfg(feature = "http-proxy")]
pub mod http_proxy_writer;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Error, PartialEq)]
pub enum InvalidFormatError {
#[error("Unknown output format: {0}")]
Unknown(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OutputFormat {
Yaml,
Registry,
#[cfg(feature = "http-proxy")]
HTTPProxy,
}
pub fn get_writer(format: &str) -> Result<OutputFormat, InvalidFormatError> {
match format {
"yaml" => return Ok(OutputFormat::Yaml),
"registry" => return Ok(OutputFormat::Registry),
_ => {}
}
#[cfg(feature = "http-proxy")]
{
if matches!(
format.to_ascii_lowercase().as_str(),
"http_proxy" | "http-proxy" | "httpproxy"
) {
return Ok(OutputFormat::HTTPProxy);
}
}
Err(InvalidFormatError::Unknown(format.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_writer_yaml() {
assert_eq!(get_writer("yaml"), Ok(OutputFormat::Yaml));
}
#[test]
fn test_get_writer_registry() {
assert_eq!(get_writer("registry"), Ok(OutputFormat::Registry));
}
#[cfg(feature = "http-proxy")]
#[test]
fn test_get_writer_http_proxy_variants() {
assert_eq!(get_writer("http_proxy"), Ok(OutputFormat::HTTPProxy));
assert_eq!(get_writer("http-proxy"), Ok(OutputFormat::HTTPProxy));
assert_eq!(get_writer("httpproxy"), Ok(OutputFormat::HTTPProxy));
}
#[test]
fn test_get_writer_canonical_formats_are_case_sensitive() {
assert!(get_writer("YAML").is_err());
assert!(get_writer("Yaml").is_err());
assert!(get_writer("Registry").is_err());
assert!(get_writer("REGISTRY").is_err());
}
#[cfg(feature = "http-proxy")]
#[test]
fn test_get_writer_case_insensitive_http_proxy() {
assert_eq!(get_writer("HTTP_PROXY"), Ok(OutputFormat::HTTPProxy));
assert_eq!(get_writer("Http-Proxy"), Ok(OutputFormat::HTTPProxy));
assert_eq!(get_writer("HTTPPROXY"), Ok(OutputFormat::HTTPProxy));
}
#[test]
fn test_get_writer_unknown() {
assert!(get_writer("xml").is_err());
assert!(get_writer("").is_err());
assert!(get_writer("xml")
.unwrap_err()
.to_string()
.contains("Unknown output format"));
}
#[test]
fn test_output_format_serde_roundtrip() {
let fmt = OutputFormat::Yaml;
let json = serde_json::to_string(&fmt).unwrap();
let deserialized: OutputFormat = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, fmt);
}
}