use serde::Serialize;
pub type YamlError = serde_saphyr::ser_error::Error;
pub trait ToYaml: Serialize + Sized {
fn to_yaml(&self) -> Result<String, YamlError> {
serde_saphyr::to_string(self)
}
}
impl<T: Serialize + Sized> ToYaml for T {}
#[cfg(test)]
mod tests {
use super::*;
use insta::assert_snapshot;
use utoipa::openapi::{InfoBuilder, OpenApiBuilder};
#[test]
fn should_serialize_openapi_to_yaml() {
let spec = OpenApiBuilder::new()
.info(
InfoBuilder::new()
.title("Test API")
.version("1.0.0")
.build(),
)
.build();
let yaml = spec.to_yaml().expect("should serialize to YAML");
assert_snapshot!(yaml, @r"
openapi: 3.1.0
info:
title: Test API
version: 1.0.0
paths: {}
");
}
#[test]
fn should_serialize_simple_struct_to_yaml() {
#[derive(Serialize)]
struct Config {
name: String,
version: u32,
}
let config = Config {
name: "test".to_string(),
version: 1,
};
let yaml = config.to_yaml().expect("should serialize to YAML");
assert_snapshot!(yaml, @r"
name: test
version: 1
");
}
}