use crate::{bindgen, util::is_unsanitary};
use check_keyword::CheckKeyword;
use openapiv3::{ReferenceOr, Schema, SchemaKind, Type};
pub fn gen(name: &str, schema: &Schema, workaround_mode: bool) -> Option<String> {
let typ = match &schema.schema_kind {
SchemaKind::Type(x) => x,
_ => return None,
};
let obj = match &typ {
Type::Object(x) => x,
_ => return None,
};
let mut result =
"#[derive(Serialize, Deserialize, Debug, Default, Clone)]\npub struct ".to_owned();
result += name;
result += " {\n";
for (prop_name, prop) in &obj.properties {
let p = prop.clone().unbox();
let type_name = bindgen::type_to_string(&p);
if let ReferenceOr::Item(item) = &p {
if let Some(desc) = &item.schema_data.description {
result += bindgen::make_comment(Some(desc.clone()), 1).as_str();
}
}
result += "\t";
result += &format!("pub {}", &prop_name.clone().into_safe());
result += ": ";
if workaround_mode {
if is_unsanitary(name)
&& !type_name.contains("Option<")
&& !result.ends_with("\tpub id:")
{
result += &format!("Option<{}>", type_name);
}
} else {
result += &type_name;
}
result += ",\n";
}
result += "}\n";
Some(result)
}