pub mod errors;
pub mod registry_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 OutputFormatError {
#[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, OutputFormatError> {
match format.to_ascii_lowercase().as_str() {
"yaml" => Ok(OutputFormat::Yaml),
"registry" => Ok(OutputFormat::Registry),
#[cfg(feature = "http-proxy")]
"http_proxy" | "http-proxy" | "httpproxy" => Ok(OutputFormat::HttpProxy),
_ => Err(OutputFormatError::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_case_insensitive() {
assert_eq!(get_writer("YAML"), Ok(OutputFormat::Yaml));
assert_eq!(get_writer("Registry"), Ok(OutputFormat::Registry));
}
#[cfg(feature = "http-proxy")]
#[test]
fn test_get_writer_case_insensitive_http_proxy() {
assert_eq!(get_writer("HTTP_PROXY"), 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);
}
}