@@ -7,11 +7,13 @@ use syn::spanned::Spanned;
77
88pub struct SchemaExpr {
99 /// Definitions for types or functions that may be used within the creator or mutators
10- definitions : Vec < TokenStream > ,
10+ pub definitions : Vec < TokenStream > ,
1111 /// An expression that produces a `Schema`
12- creator : TokenStream ,
12+ pub creator : TokenStream ,
1313 /// Statements (including terminating semicolon) that mutate a var `schema` of type `Schema`
14- mutators : Vec < TokenStream > ,
14+ pub mutators : Vec < TokenStream > ,
15+ /// Same as `mutators`, but always applied last
16+ pub post_mutators : Vec < TokenStream > ,
1517}
1618
1719impl From < TokenStream > for SchemaExpr {
@@ -20,6 +22,7 @@ impl From<TokenStream> for SchemaExpr {
2022 definitions : Vec :: new ( ) ,
2123 creator,
2224 mutators : Vec :: new ( ) ,
25+ post_mutators : Vec :: new ( ) ,
2326 }
2427 }
2528}
@@ -30,14 +33,16 @@ impl ToTokens for SchemaExpr {
3033 definitions,
3134 creator,
3235 mutators,
36+ post_mutators,
3337 } = self ;
3438
35- tokens. extend ( if mutators. is_empty ( ) {
39+ tokens. extend ( if mutators. is_empty ( ) && post_mutators . is_empty ( ) {
3640 quote ! ( {
3741 #( #definitions) *
3842 #creator
3943 } )
4044 } else {
45+ let mutators = mutators. iter ( ) . chain ( post_mutators. iter ( ) ) ;
4146 quote ! ( {
4247 #( #definitions) *
4348 let mut #SCHEMA = #creator;
@@ -105,7 +110,7 @@ pub fn expr_for_container(cont: &Container) -> SchemaExpr {
105110 }
106111 } ;
107112
108- cont. add_mutators ( & mut schema_expr. mutators ) ;
113+ cont. add_mutators ( & mut schema_expr) ;
109114
110115 schema_expr
111116}
@@ -152,7 +157,7 @@ pub fn expr_for_repr(cont: &Container) -> Result<SchemaExpr, syn::Error> {
152157 schemars:: Schema :: from( map)
153158 } ) ) ;
154159
155- cont. add_mutators ( & mut schema_expr. mutators ) ;
160+ cont. add_mutators ( & mut schema_expr) ;
156161
157162 Ok ( schema_expr)
158163}
@@ -193,7 +198,7 @@ fn expr_for_field(
193198 let mut schema_expr = SchemaExpr :: from ( schema_expr) ;
194199
195200 schema_expr. definitions . extend ( type_def) ;
196- field. add_mutators ( & mut schema_expr. mutators ) ;
201+ field. add_mutators ( & mut schema_expr) ;
197202
198203 schema_expr
199204}
@@ -331,7 +336,7 @@ fn expr_for_external_tagged_enum<'a>(
331336 }
332337 } ) ;
333338
334- variant. add_mutators ( & mut schema_expr. mutators ) ;
339+ variant. add_mutators ( & mut schema_expr) ;
335340
336341 ( Some ( variant) , schema_expr)
337342 } ) ) ;
@@ -358,7 +363,7 @@ fn expr_for_internal_tagged_enum<'a>(
358363 schemars:: _private:: apply_internal_enum_variant_tag( & mut #SCHEMA , #tag_name, #name, #deny_unknown_fields) ;
359364 ) ) ;
360365
361- variant. add_mutators ( & mut schema_expr. mutators ) ;
366+ variant. add_mutators ( & mut schema_expr) ;
362367
363368 ( Some ( variant) , schema_expr)
364369 } )
@@ -453,7 +458,7 @@ fn expr_for_adjacent_tagged_enum<'a>(
453458 #set_additional_properties
454459 } ) ) ) ;
455460
456- variant. add_mutators ( & mut outer_schema. mutators ) ;
461+ variant. add_mutators ( & mut outer_schema) ;
457462
458463 ( Some ( variant) , outer_schema)
459464 } )
@@ -592,7 +597,7 @@ fn expr_for_untagged_enum_variant(
592597 } ) ;
593598 }
594599
595- variant. add_mutators ( & mut schema_expr. mutators ) ;
600+ variant. add_mutators ( & mut schema_expr) ;
596601 }
597602
598603 schema_expr
@@ -760,6 +765,7 @@ fn expr_for_struct(
760765 #set_additional_properties
761766 } ) ) ,
762767 mutators : properties,
768+ post_mutators : Vec :: new ( ) ,
763769 }
764770}
765771
0 commit comments