Generate schemas for simple enums
This commit is contained in:
parent
48d6cda0b8
commit
d1a319c5f2
2 changed files with 23 additions and 3 deletions
|
@ -2,7 +2,7 @@ use schemars::MakeSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Result;
|
use serde_json::Result;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, MakeSchema)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
enum TodoStatus {
|
enum TodoStatus {
|
||||||
Backlog,
|
Backlog,
|
||||||
|
@ -17,7 +17,7 @@ struct Todo {
|
||||||
id: u64,
|
id: u64,
|
||||||
title: String,
|
title: String,
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
// status: TodoStatus,
|
status: TodoStatus,
|
||||||
assigned_to: Vec<User>,
|
assigned_to: Vec<User>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,8 @@ pub fn derive_make_schema(input: proc_macro::TokenStream) -> proc_macro::TokenSt
|
||||||
|
|
||||||
let fn_contents = match cont.data {
|
let fn_contents = match cont.data {
|
||||||
Data::Struct(Style::Struct, ref fields) => struct_implementation(fields),
|
Data::Struct(Style::Struct, ref fields) => struct_implementation(fields),
|
||||||
_ => unimplemented!("Only structs work so far!"),
|
Data::Enum(ref variants) => enum_implementation(variants),
|
||||||
|
_ => unimplemented!("work in progress!"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let impl_block = quote! {
|
let impl_block = quote! {
|
||||||
|
@ -49,6 +50,25 @@ fn compile_error(span: Span, message: String) -> TokenStream {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_unit_variant(v: &Variant) -> bool {
|
||||||
|
match v.style {
|
||||||
|
Style::Unit => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enum_implementation(variants: &[Variant]) -> TokenStream {
|
||||||
|
if variants.iter().all(is_unit_variant) {
|
||||||
|
let names = variants
|
||||||
|
.into_iter()
|
||||||
|
.map(|v| v.attrs.name().deserialize_name());
|
||||||
|
return quote! {
|
||||||
|
o.enum_values = Some(vec![#(#names.into()),*]);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
unimplemented!("work in progress!")
|
||||||
|
}
|
||||||
|
|
||||||
fn struct_implementation(fields: &[Field]) -> TokenStream {
|
fn struct_implementation(fields: &[Field]) -> TokenStream {
|
||||||
let recurse = fields.into_iter().map(|f| {
|
let recurse = fields.into_iter().map(|f| {
|
||||||
let name = f.attrs.name().deserialize_name();
|
let name = f.attrs.name().deserialize_name();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue