use serde_json::Value;
use std::collections::BTreeSet;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RustType {
Scalar(&'static str),
Ref(String),
Vec(Box<RustType>),
Map(Box<RustType>),
Json,
}
impl RustType {
#[must_use]
pub fn render(&self) -> String {
match self {
RustType::Scalar(s) => (*s).to_string(),
RustType::Ref(name) => name.clone(),
RustType::Vec(inner) => format!("Vec<{}>", inner.render()),
RustType::Map(inner) => {
format!("std::collections::BTreeMap<String, {}>", inner.render())
}
RustType::Json => "serde_json::Value".to_string(),
}
}
#[must_use]
pub fn has_ref(&self) -> bool {
match self {
RustType::Ref(_) => true,
RustType::Vec(inner) | RustType::Map(inner) => inner.has_ref(),
RustType::Scalar(_) | RustType::Json => false,
}
}
}
#[must_use]
pub fn type_name_from_ref(ref_str: &str) -> String {
let key = ref_str.rsplit('/').next().unwrap_or(ref_str);
key.rsplit('.').next().unwrap_or(key).to_string()
}
#[must_use]
pub fn map_schema(schema: &Value, refs: &mut BTreeSet<String>) -> RustType {
if let Some(r) = schema.get("$ref").and_then(Value::as_str) {
return ref_type(r, refs);
}
if let Some(all_of) = schema.get("allOf").and_then(Value::as_array)
&& let Some(r) = all_of
.iter()
.find_map(|m| m.get("$ref").and_then(Value::as_str))
{
return ref_type(r, refs);
}
match schema.get("type").and_then(Value::as_str) {
Some("string") => RustType::Scalar("String"),
Some("boolean") => RustType::Scalar("bool"),
Some("number") => RustType::Scalar("f64"),
Some("integer") => match schema.get("format").and_then(Value::as_str) {
Some("int32") => RustType::Scalar("i32"),
_ => RustType::Scalar("i64"),
},
Some("array") => {
let inner = schema
.get("items")
.map_or(RustType::Json, |items| map_schema(items, refs));
RustType::Vec(Box::new(inner))
}
Some("object") => match schema.get("additionalProperties") {
Some(ap) if ap.is_object() => RustType::Map(Box::new(map_schema(ap, refs))),
_ => RustType::Json,
},
_ => RustType::Json,
}
}
fn ref_type(ref_str: &str, refs: &mut BTreeSet<String>) -> RustType {
let key = ref_str.rsplit('/').next().unwrap_or(ref_str).to_string();
refs.insert(key);
RustType::Ref(type_name_from_ref(ref_str))
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[allow(clippy::needless_pass_by_value)]
fn map(v: Value) -> (RustType, BTreeSet<String>) {
let mut refs = BTreeSet::new();
let t = map_schema(&v, &mut refs);
(t, refs)
}
#[test]
fn scalars() {
assert_eq!(map(json!({"type": "string"})).0, RustType::Scalar("String"));
assert_eq!(map(json!({"type": "boolean"})).0, RustType::Scalar("bool"));
assert_eq!(map(json!({"type": "number"})).0, RustType::Scalar("f64"));
assert_eq!(map(json!({"type": "integer"})).0, RustType::Scalar("i64"));
assert_eq!(
map(json!({"type": "integer", "format": "int32"})).0,
RustType::Scalar("i32")
);
}
#[test]
fn direct_ref_records_key_and_names_type() {
let (t, refs) = map(json!({"$ref": "#/components/schemas/io.k8s.api.core.v1.PodSpec"}));
assert_eq!(t, RustType::Ref("PodSpec".into()));
assert!(refs.contains("io.k8s.api.core.v1.PodSpec"));
assert_eq!(t.render(), "PodSpec");
assert!(t.has_ref());
}
#[test]
fn allof_single_ref_unwraps() {
let (t, refs) = map(json!({
"allOf": [{"$ref": "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"}],
"default": {},
"description": "Standard object metadata."
}));
assert_eq!(t, RustType::Ref("ObjectMeta".into()));
assert!(refs.contains("io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"));
}
#[test]
fn array_of_ref_is_vec_and_collects_inner_ref() {
let (t, refs) = map(json!({
"type": "array",
"items": {"$ref": "#/components/schemas/io.k8s.api.core.v1.LocalObjectReference"}
}));
assert_eq!(
t,
RustType::Vec(Box::new(RustType::Ref("LocalObjectReference".into())))
);
assert_eq!(t.render(), "Vec<LocalObjectReference>");
assert!(refs.contains("io.k8s.api.core.v1.LocalObjectReference"));
assert!(t.has_ref());
}
#[test]
fn array_of_scalar_is_vec_no_ref() {
let (t, refs) = map(json!({"type": "array", "items": {"type": "string"}}));
assert_eq!(t, RustType::Vec(Box::new(RustType::Scalar("String"))));
assert_eq!(t.render(), "Vec<String>");
assert!(refs.is_empty());
assert!(!t.has_ref());
}
#[test]
fn object_with_typed_additional_properties_is_map() {
let (t, _) = map(json!({"type": "object", "additionalProperties": {"type": "string"}}));
assert_eq!(t, RustType::Map(Box::new(RustType::Scalar("String"))));
assert_eq!(t.render(), "std::collections::BTreeMap<String, String>");
}
#[test]
fn int_or_string_and_freeform_fall_back_to_json() {
assert_eq!(
map(json!({"x-kubernetes-int-or-string": true})).0,
RustType::Json
);
assert_eq!(
map(json!({"type": "object", "additionalProperties": true})).0,
RustType::Json
);
assert_eq!(map(json!({})).0, RustType::Json);
assert_eq!(RustType::Json.render(), "serde_json::Value");
assert!(!RustType::Json.has_ref());
}
#[test]
fn nested_array_of_map_renders_and_propagates_ref() {
let (t, refs) = map(json!({
"type": "array",
"items": {
"type": "object",
"additionalProperties": {"$ref": "#/components/schemas/io.k8s.api.core.v1.ResourceClaim"}
}
}));
assert_eq!(
t.render(),
"Vec<std::collections::BTreeMap<String, ResourceClaim>>"
);
assert!(t.has_ref());
assert!(refs.contains("io.k8s.api.core.v1.ResourceClaim"));
}
#[test]
fn type_name_from_ref_takes_last_segment() {
assert_eq!(
type_name_from_ref("#/components/schemas/io.k8s.api.core.v1.PodSpec"),
"PodSpec"
);
assert_eq!(
type_name_from_ref("io.k8s.api.apps.v1.DeploymentStatus"),
"DeploymentStatus"
);
}
}