Skip to content

Commit

Permalink
Fix compile error propagation
Browse files Browse the repository at this point in the history
Fix compile error propagation via comprehensive use of
`ToTokensDiagnostics` trait. `ToTokensDiagnostics` was introduced in
PR #920 to allow error propagation up in the call stack for error
reporting. However this initial implementation was too limited which
resulted invalid compile errors in case of error. This implementation
aims to fix the shortcomings of the initial implementation.
  • Loading branch information
juhaku committed May 16, 2024
1 parent 48342ae commit 87b177d
Show file tree
Hide file tree
Showing 16 changed files with 478 additions and 464 deletions.
228 changes: 117 additions & 111 deletions utoipa-gen/src/component.rs

Large diffs are not rendered by default.

35 changes: 22 additions & 13 deletions utoipa-gen/src/component/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use quote::{quote, ToTokens};
use syn::{parenthesized, parse::ParseStream, LitFloat, LitInt, LitStr, TypePath};

use crate::{
impl_to_tokens_diagnostics, parse_utils,
as_tokens_or_diagnostics, parse_utils,
path::parameter::{self, ParameterStyle},
schema_type::{SchemaFormat, SchemaType},
AnyValue, Diagnostics, OptionExt,
AnyValue, Diagnostics, OptionExt, ToTokensDiagnostics,
};

use super::{schema, serde::RenameRule, GenericType, TypeTree};
Expand Down Expand Up @@ -168,8 +168,10 @@ impl Feature {
}
}
}
}

fn tokens_or_diagnostics(&self, tokens: &mut TokenStream) -> Result<(), Diagnostics> {
impl ToTokensDiagnostics for Feature {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) -> Result<(), Diagnostics> {
let feature = match &self {
Feature::Default(default) => quote! { .default(#default) },
Feature::Example(example) => quote! { .example(Some(#example)) },
Expand Down Expand Up @@ -242,10 +244,12 @@ impl Feature {
}
}

impl_to_tokens_diagnostics! {
impl ToTokensDiagnostics for Feature {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) -> Result<(), Diagnostics> {
self.tokens_or_diagnostics(tokens)
impl ToTokensDiagnostics for Option<Feature> {
fn to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostics> {
if let Some(this) = self {
this.to_tokens(tokens)
} else {
Ok(())
}
}
}
Expand Down Expand Up @@ -1673,15 +1677,20 @@ impl IsInline for Vec<Feature> {
}

pub trait ToTokensExt {
fn to_token_stream(&self) -> TokenStream;
fn to_token_stream(&self) -> Result<TokenStream, Diagnostics>;
}

impl ToTokensExt for Vec<Feature> {
fn to_token_stream(&self) -> TokenStream {
self.iter().fold(TokenStream::new(), |mut tokens, item| {
item.to_tokens(&mut tokens);
tokens
})
fn to_token_stream(&self) -> Result<TokenStream, Diagnostics> {
Ok(self
.iter()
.map(|feature| Ok(as_tokens_or_diagnostics!(feature)))
.collect::<Result<Vec<TokenStream>, Diagnostics>>()?
.into_iter()
.fold(TokenStream::new(), |mut tokens, item| {
item.to_tokens(&mut tokens);
tokens
}))
}
}

Expand Down
26 changes: 11 additions & 15 deletions utoipa-gen/src/component/into_params.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use quote::quote;
use syn::{
parse::Parse, punctuated::Punctuated, spanned::Spanned, token::Comma, Attribute, Data, Field,
Generics, Ident,
Expand All @@ -19,7 +19,7 @@ use crate::{
FieldRename,
},
doc_comment::CommentAttributes,
impl_to_tokens_diagnostics, Array, Diagnostics, OptionExt, Required, ToTokensDiagnostics,
Array, Diagnostics, OptionExt, Required, ToTokensDiagnostics,
};

use super::{
Expand Down Expand Up @@ -362,8 +362,10 @@ impl Param<'_> {
},
))
}
}

fn tokens_or_diagnostics(&self, tokens: &mut TokenStream) -> Result<(), Diagnostics> {
impl ToTokensDiagnostics for Param<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostics> {
let field = self.field;
let field_serde_params = &self.field_serde_params;
let ident = &field.ident;
Expand Down Expand Up @@ -408,7 +410,7 @@ impl Param<'_> {
});
tokens.extend(
if let Some(ref parameter_in) = self.container_attributes.parameter_in {
parameter_in.into_token_stream()
parameter_in.to_token_stream()
} else {
quote! {
.parameter_in(parameter_in_provider().unwrap_or_default())
Expand All @@ -422,6 +424,7 @@ impl Param<'_> {

let schema_with = pop_feature!(param_features => Feature::SchemaWith(_));
if let Some(schema_with) = schema_with {
let schema_with = crate::as_tokens_or_diagnostics!(&schema_with);
tokens.extend(quote! { .schema(Some(#schema_with)).build() });
} else {
let description =
Expand All @@ -448,27 +451,20 @@ impl Param<'_> {
tokens.extend(quote! {
.required(#required)
});
tokens.extend(param_features.to_token_stream());
tokens.extend(param_features.to_token_stream()?);

let schema = ComponentSchema::new(component::ComponentSchemaProps {
type_tree: &component,
features: Some(schema_features),
description: None,
deprecated: None,
object_name: "",
});
})?;
let schema_tokens = crate::as_tokens_or_diagnostics!(&schema);

tokens.extend(quote! { .schema(Some(#schema)).build() });
tokens.extend(quote! { .schema(Some(#schema_tokens)).build() });
}

Ok(())
}
}

impl_to_tokens_diagnostics! {
impl ToTokensDiagnostics for Param<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostics> {
self.tokens_or_diagnostics(tokens)
}
}
}
Loading

0 comments on commit 87b177d

Please sign in to comment.