Refactoring
This commit is contained in:
parent
4be21bd811
commit
5f841f2e5c
4 changed files with 117 additions and 101 deletions
|
@ -1,5 +1,3 @@
|
|||
use crate::attr;
|
||||
use attr::Attrs;
|
||||
use proc_macro2::TokenStream;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -14,24 +12,10 @@ pub struct SchemaMetadata<'a> {
|
|||
}
|
||||
|
||||
impl<'a> SchemaMetadata<'a> {
|
||||
pub fn from_attrs(attrs: &'a Attrs) -> Self {
|
||||
SchemaMetadata {
|
||||
title: attrs.title.as_ref().and_then(none_if_empty),
|
||||
description: attrs.description.as_ref().and_then(none_if_empty),
|
||||
deprecated: attrs.deprecated,
|
||||
examples: &attrs.examples,
|
||||
read_only: false,
|
||||
write_only: false,
|
||||
default: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_to_schema(&self, schema_expr: TokenStream) -> TokenStream {
|
||||
pub fn apply_to_schema(&self, schema_expr: &mut TokenStream) {
|
||||
let setters = self.make_setters();
|
||||
if setters.is_empty() {
|
||||
schema_expr
|
||||
} else {
|
||||
quote! {
|
||||
if !setters.is_empty() {
|
||||
*schema_expr = quote! {
|
||||
schemars::_private::apply_metadata(#schema_expr, schemars::schema::Metadata {
|
||||
#(#setters)*
|
||||
..Default::default()
|
||||
|
@ -91,12 +75,3 @@ impl<'a> SchemaMetadata<'a> {
|
|||
setters
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::ptr_arg)]
|
||||
fn none_if_empty(s: &String) -> Option<&str> {
|
||||
if s.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(s)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue