1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-22 12:42:28 +00:00

more enum stuff

[skip ci]
This commit is contained in:
Dominic 2021-03-08 17:33:49 +01:00
parent 43d3a1cd89
commit 5f60599c41
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
2 changed files with 85 additions and 2 deletions

View file

@ -77,3 +77,68 @@ test_type!(EnumWithOneField = {
},
"required": ["Success"]
});
#[derive(OpenapiType)]
enum EnumWithFields {
Success { value: isize },
Error { msg: String }
}
test_type!(EnumWithFields = {
"title": "EnumWithFields",
"oneOf": [{
"type": "object",
"properties": {
"Success": {
"type": "object",
"properties": {
"value": {
"type": "integer"
}
},
"required": ["value"]
}
},
"required": ["Success"]
}, {
"type": "object",
"properties": {
"Error": {
"type": "object",
"properties": {
"msg": {
"type": "string"
}
},
"required": ["msg"]
}
},
"required": ["Error"]
}]
});
#[derive(OpenapiType)]
enum EnumExternallyTagged {
Success { value: isize },
Error
}
test_type!(EnumExternallyTagged = {
"title": "EnumExternallyTagged",
"oneOf": [{
"type": "object",
"properties": {
"Success": {
"type": "object",
"properties": {
"value": {
"type": "integer"
}
},
"required": ["value"]
}
},
"required": ["Success"]
}, {
"type": "string",
"enum": ["Error"]
}]
});

View file

@ -8,8 +8,8 @@ impl ParseData {
match self {
Self::Struct(fields) => gen_struct(fields),
Self::Enum(variants) => gen_enum(variants),
Self::Unit => gen_unit(),
_ => unimplemented!()
Self::Alternatives(alt) => gen_alt(alt),
Self::Unit => gen_unit()
}
}
}
@ -108,6 +108,24 @@ fn gen_enum(variants: &[LitStr]) -> TokenStream {
}
}
fn gen_alt(alt: &[ParseData]) -> TokenStream {
let openapi = path!(::openapi_type::openapi);
let schema = alt.iter().map(|data| data.gen_schema());
quote! {
{
let mut alternatives = <::std::vec::Vec<
#openapi::ReferenceOr<#openapi::Schema>
>>::new();
#(alternatives.push(#openapi::ReferenceOr::Item(
::openapi_type::OpenapiSchema::new(#schema).into_schema()
));)*
#openapi::SchemaKind::OneOf {
one_of: alternatives
}
}
}
}
fn gen_unit() -> TokenStream {
let openapi = path!(::openapi_type::openapi);
quote! {