Skip to content

Commit

Permalink
feat: carry over field docs
Browse files Browse the repository at this point in the history
  • Loading branch information
NexRX committed Dec 23, 2023
1 parent 04c0298 commit eb4613f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ fn extract_oai_f_attributes(attrs: &[syn::Attribute]) -> Vec<&syn::Attribute> {
.collect()
}

fn is_doc(v: &&Attribute) -> bool {
v.meta.require_name_value().map_or(false, |v| {
v.path.segments.first().map_or(false, |v| v.ident == "doc")
})
}

fn extract_docs(attrs: &[Attribute]) -> proc_macro2::TokenStream {
let docs: Vec<_> = attrs.iter().filter(is_doc).collect();
quote!(#(#docs)*)
}

/// Aborts on unexpected args to show that they arent valid
fn abort_unexpected_args(names: Vec<&str>, args: &[TokenTree]) {
for tk in args.iter() {
Expand Down
19 changes: 14 additions & 5 deletions src/patch.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::*;

use proc_macro2::{Group, Ident, TokenStream, TokenTree};
use proc_macro_error::abort;
use quote::quote;
use syn::{Attribute, DeriveInput, Type};
use proc_macro_error::abort;

struct PatchModelArgs {
name: Ident,
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn impl_patch_model(
}

// Add
let field_ty = &field.ty;
let docs = extract_docs(&field.attrs);
let oai_f_attributes: Vec<_> = field_attributes
.iter()
.filter(|attr| {
Expand All @@ -51,13 +51,15 @@ pub fn impl_patch_model(
.map_or(false, |seg| seg.ident == "oai")
})
.collect();
let field_ty = &field.ty;
let option_ty = extract_type_from_option(field_ty);

fields_and_is_option.push((field_name, option_ty.is_some()));
fields.push(impl_struct_fields(
field_name,
field_ty,
option_ty,
&docs,
&oai_f_attributes,
));
}),
Expand Down Expand Up @@ -216,19 +218,22 @@ fn impl_struct_fields(
field_name: &Ident,
field_ty: &Type,
#[allow(unused_variables)] option_ty: Option<&Type>,
docs: &TokenStream,
oai_f_attr: &Vec<&Attribute>,
) -> TokenStream {
#[cfg(feature = "openapi")]
{
match option_ty {
Some(t) => {
quote! {
#(#oai_f_attr)*
pub #field_name: ::poem_openapi::types::MaybeUndefined<#t>
#docs
#(#oai_f_attr)*
pub #field_name: ::poem_openapi::types::MaybeUndefined<#t>
}
}
_ => {
quote! {
#docs
#(#oai_f_attr)*
pub #field_name: core::option::Option<#field_ty>
}
Expand All @@ -238,6 +243,7 @@ fn impl_struct_fields(
#[cfg(not(feature = "openapi"))]
{
quote! {
#docs
#(#oai_f_attr)*
pub #field_name: core::option::Option<#field_ty>
}
Expand All @@ -257,7 +263,10 @@ fn parse_patch_arg(attr: &Attribute) -> PatchModelArgs {
let name = match &tks[0] {
TokenTree::Ident(v) => v.clone(),
x => {
abort!(x, "First argument must be an identifier (name) of the struct for the view")
abort!(
x,
"First argument must be an identifier (name) of the struct for the view"
)
}
};

Expand Down
10 changes: 7 additions & 3 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ pub fn impl_view_model(
let field_attr: &Vec<syn::Attribute> = &field.attrs;

let vis = &field.vis;
let docs = extract_docs(&field.attrs);
let oai_f_attributes: Vec<_> = extract_oai_f_attributes(field_attr);
let field_name = &field.ident.as_ref().unwrap();
let field_ty = &field.ty;
let oai_f_attributes: Vec<_> = extract_oai_f_attributes(field_attr);

field_from_mapping.push(quote!(#field_name: value.#field_name));
quote! {
#(#oai_f_attributes)*
#vis #field_name: #field_ty
#docs
#(#oai_f_attributes)*
#vis #field_name: #field_ty
}
})
.collect(),
Expand All @@ -58,6 +60,7 @@ pub fn impl_view_model(
let mut field_without_attrs = field.clone();
field_without_attrs.attrs = vec![];

let docs = extract_docs(&field.attrs);
let field_name = &field.ident;

match &field.fields {
Expand Down Expand Up @@ -99,6 +102,7 @@ pub fn impl_view_model(
};

quote! {
#docs
#(#oai_f_attr)*
#field_without_attrs
}
Expand Down
4 changes: 4 additions & 0 deletions tests/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use restructed::Models;
#[derive(Models)]
#[view(UserProfile, fields(display_name, bio))]
struct User {
/// This should be omitted
id: i32,
/// This shouldn't be omitted
display_name: String,
bio: String,
password: String,
Expand Down Expand Up @@ -53,6 +55,8 @@ fn only_fields() {
#[derive(Debug, Clone, Models)]
#[view(ApiErrorReads, fields(NotFound, Unauthorized, InternalServerError))]
pub enum ApiError {
/// This "NotFound" rustdoc will carry over <br/>
/// Even for multiple lines
NotFound(String),
ConflictX(String),
ConflictY(u64),
Expand Down

0 comments on commit eb4613f

Please sign in to comment.