Skip to content

Commit 61555f0

Browse files
committed
Refactor structs processing
This commit refactors structs processing to pave the way for collecting schemas from their usages.
1 parent e13cfe1 commit 61555f0

File tree

12 files changed

+366
-369
lines changed

12 files changed

+366
-369
lines changed

utoipa-gen/src/component.rs

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,8 @@ pub struct Container<'c> {
541541
pub struct ComponentSchemaProps<'c> {
542542
pub container: &'c Container<'c>,
543543
pub type_tree: &'c TypeTree<'c>,
544-
pub features: Option<Vec<Feature>>,
544+
pub features: Vec<Feature>,
545545
pub description: Option<&'c ComponentDescription<'c>>,
546-
pub deprecated: Option<&'c Deprecated>,
547546
}
548547

549548
#[cfg_attr(feature = "debug", derive(Debug))]
@@ -579,19 +578,16 @@ pub struct ComponentSchema {
579578
pub name_tokens: TokenStream,
580579
}
581580

582-
impl<'c> ComponentSchema {
581+
impl ComponentSchema {
583582
pub fn new(
584583
ComponentSchemaProps {
585584
container,
586585
type_tree,
587-
features,
586+
mut features,
588587
description,
589-
deprecated,
590588
}: ComponentSchemaProps,
591589
) -> Result<Self, Diagnostics> {
592590
let mut tokens = TokenStream::new();
593-
let mut features = features.unwrap_or(Vec::new());
594-
let deprecated_stream = ComponentSchema::get_deprecated(deprecated);
595591
let mut name_tokens = TokenStream::new();
596592

597593
match type_tree.generic_type {
@@ -601,7 +597,6 @@ impl<'c> ComponentSchema {
601597
features,
602598
type_tree,
603599
description,
604-
deprecated_stream,
605600
)?,
606601
Some(GenericType::Vec | GenericType::LinkedList | GenericType::Set) => {
607602
ComponentSchema::vec_to_tokens(
@@ -610,7 +605,6 @@ impl<'c> ComponentSchema {
610605
features,
611606
type_tree,
612607
description,
613-
deprecated_stream,
614608
)?
615609
}
616610
#[cfg(feature = "smallvec")]
@@ -620,7 +614,6 @@ impl<'c> ComponentSchema {
620614
features,
621615
type_tree,
622616
description,
623-
deprecated_stream,
624617
)?,
625618
Some(GenericType::Option) => {
626619
// Add nullable feature if not already exists. Option is always nullable
@@ -640,9 +633,8 @@ impl<'c> ComponentSchema {
640633
.iter()
641634
.next()
642635
.expect("ComponentSchema generic container type should have 1 child"),
643-
features: Some(features),
636+
features,
644637
description,
645-
deprecated,
646638
})?
647639
.to_tokens(&mut tokens)?;
648640
}
@@ -656,9 +648,8 @@ impl<'c> ComponentSchema {
656648
.iter()
657649
.next()
658650
.expect("ComponentSchema generic container type should have 1 child"),
659-
features: Some(features),
651+
features,
660652
description,
661-
deprecated,
662653
})?
663654
.to_tokens(&mut tokens)?;
664655
}
@@ -673,9 +664,8 @@ impl<'c> ComponentSchema {
673664
.iter()
674665
.next()
675666
.expect("ComponentSchema rc generic container type should have 1 child"),
676-
features: Some(features),
667+
features,
677668
description,
678-
deprecated,
679669
})?
680670
.to_tokens(&mut tokens)?;
681671
}
@@ -686,7 +676,6 @@ impl<'c> ComponentSchema {
686676
features,
687677
type_tree,
688678
description,
689-
deprecated_stream,
690679
)?,
691680
};
692681

@@ -723,14 +712,14 @@ impl<'c> ComponentSchema {
723712
mut features: Vec<Feature>,
724713
type_tree: &TypeTree,
725714
description_stream: Option<&ComponentDescription<'_>>,
726-
deprecated_stream: Option<TokenStream>,
727715
) -> Result<(), Diagnostics> {
728716
let example = features.pop_by(|feature| matches!(feature, Feature::Example(_)));
729717
let additional_properties = pop_feature!(features => Feature::AdditionalProperties(_));
730718
let nullable: Option<Nullable> =
731719
pop_feature!(features => Feature::Nullable(_)).into_inner();
732720
let default = pop_feature!(features => Feature::Default(_));
733721
let default_tokens = as_tokens_or_diagnostics!(&default);
722+
let deprecated = pop_feature!(features => Feature::Deprecated(_)).try_to_token_stream()?;
734723

735724
let additional_properties = additional_properties
736725
.as_ref()
@@ -748,9 +737,8 @@ impl<'c> ComponentSchema {
748737
.expect("ComponentSchema Map type should have children")
749738
.get(1)
750739
.expect("ComponentSchema Map type should have 2 child"),
751-
features: Some(features),
740+
features,
752741
description: None,
753-
deprecated: None,
754742
})?;
755743
let schema_tokens = as_tokens_or_diagnostics!(&schema_property);
756744

@@ -767,7 +755,7 @@ impl<'c> ComponentSchema {
767755
#schema_type
768756
#additional_properties
769757
#description_stream
770-
#deprecated_stream
758+
#deprecated
771759
#default_tokens
772760
});
773761

@@ -780,7 +768,6 @@ impl<'c> ComponentSchema {
780768
mut features: Vec<Feature>,
781769
type_tree: &TypeTree,
782770
description_stream: Option<&ComponentDescription<'_>>,
783-
deprecated_stream: Option<TokenStream>,
784771
) -> Result<(), Diagnostics> {
785772
let example = pop_feature!(features => Feature::Example(_));
786773
let xml = features.extract_vec_xml_feature(type_tree)?;
@@ -789,6 +776,7 @@ impl<'c> ComponentSchema {
789776
let nullable: Option<Nullable> =
790777
pop_feature!(features => Feature::Nullable(_)).into_inner();
791778
let default = pop_feature!(features => Feature::Default(_));
779+
let deprecated = pop_feature!(features => Feature::Deprecated(_)).try_to_token_stream()?;
792780

793781
let child = type_tree
794782
.children
@@ -816,9 +804,8 @@ impl<'c> ComponentSchema {
816804
let component_schema = ComponentSchema::new(ComponentSchemaProps {
817805
container,
818806
type_tree: child,
819-
features: Some(features),
807+
features,
820808
description: None,
821-
deprecated: None,
822809
})?;
823810
let component_schema_tokens = as_tokens_or_diagnostics!(&component_schema);
824811

@@ -851,7 +838,7 @@ impl<'c> ComponentSchema {
851838

852839
tokens.extend(quote! {
853840
#schema
854-
#deprecated_stream
841+
#deprecated
855842
#description_stream
856843
});
857844

@@ -882,13 +869,13 @@ impl<'c> ComponentSchema {
882869
mut features: Vec<Feature>,
883870
type_tree: &TypeTree,
884871
description_stream: Option<&ComponentDescription<'_>>,
885-
deprecated_stream: Option<TokenStream>,
886872
) -> Result<(), Diagnostics> {
887873
let nullable_feat: Option<Nullable> =
888874
pop_feature!(features => Feature::Nullable(_)).into_inner();
889875
let nullable = nullable_feat
890876
.map(|nullable| nullable.value())
891877
.unwrap_or_default();
878+
let deprecated = pop_feature!(features => Feature::Deprecated(_)).try_to_token_stream()?;
892879

893880
match type_tree.value_type {
894881
ValueType::Primitive => {
@@ -921,7 +908,7 @@ impl<'c> ComponentSchema {
921908
}
922909

923910
description_stream.to_tokens(tokens);
924-
tokens.extend(deprecated_stream);
911+
tokens.extend(deprecated);
925912
for feature in features.iter().filter(|feature| feature.is_validatable()) {
926913
feature.validate(&schema_type, type_tree);
927914
}
@@ -934,7 +921,7 @@ impl<'c> ComponentSchema {
934921
tokens.extend(quote! {
935922
utoipa::openapi::ObjectBuilder::new()
936923
.schema_type(utoipa::openapi::schema::SchemaType::AnyValue)
937-
#description_stream #deprecated_stream
924+
#description_stream #deprecated
938925
})
939926
}
940927
}
@@ -949,7 +936,7 @@ impl<'c> ComponentSchema {
949936
tokens.extend(quote! {
950937
utoipa::openapi::ObjectBuilder::new()
951938
#nullable_schema_type
952-
#description_stream #deprecated_stream
939+
#description_stream #deprecated
953940
})
954941
} else {
955942
fn nullable_all_of_item(nullable: bool) -> Option<TokenStream> {
@@ -1108,17 +1095,16 @@ impl<'c> ComponentSchema {
11081095
.iter()
11091096
.map(|child| {
11101097
let features = if child.is_option() {
1111-
Some(vec![Feature::Nullable(Nullable::new())])
1098+
vec![Feature::Nullable(Nullable::new())]
11121099
} else {
1113-
None
1100+
Vec::new()
11141101
};
11151102

11161103
match ComponentSchema::new(ComponentSchemaProps {
11171104
container,
11181105
type_tree: child,
11191106
features,
11201107
description: None,
1121-
deprecated: None,
11221108
}) {
11231109
Ok(child) => Ok(as_tokens_or_diagnostics!(&child)),
11241110
Err(diagnostics) => Err(diagnostics),
@@ -1144,7 +1130,7 @@ impl<'c> ComponentSchema {
11441130
#nullable_schema_type
11451131
.items(#all_of)
11461132
#description_stream
1147-
#deprecated_stream
1133+
#deprecated
11481134
})
11491135
})?
11501136
.unwrap_or_else(|| quote!(utoipa::openapi::schema::empty())) // TODO should
@@ -1155,10 +1141,6 @@ impl<'c> ComponentSchema {
11551141
}
11561142
Ok(())
11571143
}
1158-
1159-
fn get_deprecated(deprecated: Option<&'c Deprecated>) -> Option<TokenStream> {
1160-
deprecated.map(|deprecated| quote! { .deprecated(Some(#deprecated)) })
1161-
}
11621144
}
11631145

11641146
impl ToTokensDiagnostics for ComponentSchema {
@@ -1178,14 +1160,12 @@ impl FlattenedMapSchema {
11781160
ComponentSchemaProps {
11791161
container,
11801162
type_tree,
1181-
features,
1163+
mut features,
11821164
description,
1183-
deprecated,
11841165
}: ComponentSchemaProps,
11851166
) -> Result<Self, Diagnostics> {
11861167
let mut tokens = TokenStream::new();
1187-
let mut features = features.unwrap_or(Vec::new());
1188-
let deprecated_stream = ComponentSchema::get_deprecated(deprecated);
1168+
let deprecated = pop_feature!(features => Feature::Deprecated(_)).try_to_token_stream()?;
11891169

11901170
let example = features.pop_by(|feature| matches!(feature, Feature::Example(_)));
11911171
let nullable = pop_feature!(features => Feature::Nullable(_));
@@ -1204,16 +1184,15 @@ impl FlattenedMapSchema {
12041184
.expect("ComponentSchema Map type should have children")
12051185
.get(1)
12061186
.expect("ComponentSchema Map type should have 2 child"),
1207-
features: Some(features),
1187+
features,
12081188
description: None,
1209-
deprecated: None,
12101189
})?;
12111190
let schema_tokens = as_tokens_or_diagnostics!(&schema_property);
12121191

12131192
tokens.extend(quote! {
12141193
#schema_tokens
12151194
#description
1216-
#deprecated_stream
1195+
#deprecated
12171196
#default_tokens
12181197
});
12191198

utoipa-gen/src/component/into_params.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,8 @@ impl ToTokensDiagnostics for Param<'_> {
457457

458458
let schema = ComponentSchema::new(component::ComponentSchemaProps {
459459
type_tree: &component,
460-
features: Some(schema_features),
460+
features: schema_features,
461461
description: None,
462-
deprecated: None,
463462
container: &Container {
464463
generics: self.generics,
465464
},

0 commit comments

Comments
 (0)