Refactoring

This commit is contained in:
Graham Esau 2021-04-16 16:53:15 +01:00
parent 4be21bd811
commit 5f841f2e5c
4 changed files with 117 additions and 101 deletions

View file

@ -5,6 +5,7 @@ mod validation;
pub use schemars_to_serde::process_serde_attrs;
pub use validation::ValidationAttrs;
use crate::metadata::SchemaMetadata;
use proc_macro2::{Group, Span, TokenStream, TokenTree};
use quote::ToTokens;
use serde_derive_internals::Ctxt;
@ -53,6 +54,27 @@ impl Attrs {
result
}
pub fn as_metadata(&self) -> SchemaMetadata<'_> {
#[allow(clippy::ptr_arg)]
fn none_if_empty(s: &String) -> Option<&str> {
if s.is_empty() {
None
} else {
Some(s)
}
}
SchemaMetadata {
title: self.title.as_ref().and_then(none_if_empty),
description: self.description.as_ref().and_then(none_if_empty),
deprecated: self.deprecated,
examples: &self.examples,
read_only: false,
write_only: false,
default: None,
}
}
fn populate(
mut self,
attrs: &[syn::Attribute],

View file

@ -120,7 +120,7 @@ impl ValidationAttrs {
self
}
pub fn apply_to_schema(&self, schema_expr: TokenStream) -> TokenStream {
pub fn apply_to_schema(&self, schema_expr: &mut TokenStream) {
let mut array_validation = Vec::new();
let mut number_validation = Vec::new();
let mut object_validation = Vec::new();
@ -200,7 +200,7 @@ impl ValidationAttrs {
|| string_validation.is_some()
|| format.is_some()
{
quote! {
*schema_expr = quote! {
{
let mut schema = #schema_expr;
if let schemars::schema::Schema::Object(schema_object) = &mut schema
@ -214,8 +214,6 @@ impl ValidationAttrs {
schema
}
}
} else {
schema_expr
}
}
}