Skip to content

Commit 9a3a833

Browse files
authored
Feat: improve docs and standardize generics (#180)
1 parent 95f4f65 commit 9a3a833

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

example_programs/faction_enlistment/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,11 @@ pub struct PlayerFactionData {
161161
pub enlisted_at_timestamp: i64,
162162
pub faction_id: FactionId,
163163
pub bump: u8,
164+
/// Some docs!
164165
pub counter: CounterAccountData,
165166
pub _padding: [u64; 5],
166167
#[unsized_start]
168+
/// Docs for Some fields!!
167169
some_fields: SomeFields,
168170
}
169171

star_frame_proc/src/idl/instruction_to_idl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::idl::{derive_type_to_idl_inner, TypeToIdlArgs};
2-
use crate::util::{ignore_cfg_module, reject_attributes, reject_generics, Paths};
2+
use crate::util::{ignore_cfg_module, new_generic, reject_attributes, reject_generics, Paths};
33
use easy_proc::{find_attr, ArgumentList};
4-
use proc_macro2::{Ident, TokenStream};
5-
use quote::{format_ident, quote};
4+
use proc_macro2::TokenStream;
5+
use quote::quote;
66
use syn::{parse_quote, DeriveInput};
77

88
pub fn derive_instruction_to_idl(input: &DeriveInput) -> TokenStream {
@@ -28,7 +28,7 @@ pub fn derive_instruction_to_idl(input: &DeriveInput) -> TokenStream {
2828
let mut generics = input.generics.clone();
2929
let where_clause = generics.make_where_clause();
3030

31-
let generic_arg: Ident = format_ident!("__A");
31+
let generic_arg = new_generic(&input.generics, None);
3232

3333
where_clause.predicates.push(
3434
parse_quote!(<Self as #prelude::StarFrameInstruction>::Accounts<'b, 'c>: #prelude::AccountSetToIdl<#generic_arg>),

star_frame_proc/src/unsize/struct_impl.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use proc_macro2::Ident;
1111
use proc_macro2::TokenStream;
1212
use proc_macro_error2::abort;
1313
use quote::{format_ident, quote};
14-
use syn::{parse_quote, Field, Generics, ItemStruct, Lifetime, Type, Visibility};
14+
use syn::{parse_quote, Attribute, Field, Generics, ItemStruct, Lifetime, Type, Visibility};
1515

1616
#[allow(non_snake_case)]
1717
macro_rules! UnsizedStructContext {
@@ -120,6 +120,7 @@ pub struct UnsizedStructContext {
120120
sized_field_types: Vec<Type>,
121121
unsized_field_idents: Vec<Ident>,
122122
unsized_field_types: Vec<Type>,
123+
with_sized_docs: Vec<Vec<Attribute>>,
123124
with_sized_idents: Vec<Ident>,
124125
with_sized_types: Vec<Type>,
125126
with_sized_vis: Vec<Visibility>,
@@ -212,14 +213,29 @@ impl UnsizedStructContext {
212213

213214
let sized_field_ident = sized_ident
214215
.as_ref()
215-
.map(|_| new_ident("__sized", get_field_idents(&all_fields), true));
216+
.map(|_| new_ident("_sized", get_field_idents(&all_fields), true));
216217

217218
let sized_field_idents = get_field_idents(&sized_fields).cloned().collect_vec();
218219
let sized_field_types = get_field_types(&sized_fields).cloned().collect_vec();
219220
let unsized_field_idents = get_field_idents(&unsized_fields).cloned().collect_vec();
220221
let unsized_field_types = get_field_types(&unsized_fields).cloned().collect_vec();
221222
let unsized_field_vis = get_field_vis(&unsized_fields).cloned().collect_vec();
222223

224+
let with_sized_docs = sized_field_ident
225+
.as_ref()
226+
.map(|_ident| {
227+
vec![parse_quote! {
228+
#[doc = "Sized portion of the Unsized Type"]
229+
}]
230+
})
231+
.into_iter()
232+
.chain(
233+
unsized_fields
234+
.iter()
235+
.map(|field| get_doc_attributes(&field.attrs)),
236+
)
237+
.collect_vec();
238+
223239
let with_sized_idents = sized_field_ident
224240
.iter()
225241
.chain(unsized_field_idents.iter())
@@ -267,6 +283,7 @@ impl UnsizedStructContext {
267283
sized_field_types,
268284
unsized_field_idents,
269285
unsized_field_types,
286+
with_sized_docs,
270287
with_sized_idents,
271288
with_sized_types,
272289
with_sized_vis,
@@ -308,15 +325,19 @@ impl UnsizedStructContext {
308325

309326
fn ref_struct(&self) -> TokenStream {
310327
Paths!(prelude);
311-
UnsizedStructContext!(self => vis, ref_ident, with_sized_vis, with_sized_idents, with_sized_types, top_lt);
328+
UnsizedStructContext!(self => vis, ref_ident, with_sized_vis, with_sized_idents, with_sized_types, top_lt, with_sized_docs);
312329
let (generics, wc) = self.split_for_declaration(true);
313330
let transparent = (with_sized_idents.len() == 1).then(|| quote!(#[repr(transparent)]));
331+
332+
let doc = format!("Ref type for [`{}`]", self.struct_ident);
314333
quote! {
334+
#[doc = #doc]
315335
#[derive(#prelude::DeriveWhere)]
316336
#[derive_where(Debug; #(<#with_sized_types as #prelude::UnsizedType>::Ref<#top_lt>,)*)]
317337
#transparent
318338
#vis struct #ref_ident #generics #wc {
319339
#(
340+
#(#with_sized_docs)*
320341
#with_sized_vis #with_sized_idents: <#with_sized_types as #prelude::UnsizedType>::Ref<#top_lt>,
321342
)*
322343
}
@@ -325,16 +346,19 @@ impl UnsizedStructContext {
325346

326347
fn mut_struct(&self) -> TokenStream {
327348
Paths!(prelude);
328-
UnsizedStructContext!(self => vis, mut_ident, top_lt, with_sized_vis, with_sized_idents, with_sized_types);
349+
UnsizedStructContext!(self => vis, mut_ident, top_lt, with_sized_vis, with_sized_idents, with_sized_types, with_sized_docs);
329350
let (generics, wc) = self.split_for_declaration(true);
330351
let transparent = (with_sized_idents.len() == 1).then(|| quote!(#[repr(transparent)]));
331352

353+
let doc = format!("Mut type for [`{}`]", self.struct_ident);
332354
quote! {
355+
#[doc = #doc]
333356
#[derive(#prelude::DeriveWhere)]
334357
#[derive_where(Debug; #(<#with_sized_types as #prelude::UnsizedType>::Mut<#top_lt>,)*)]
335358
#transparent
336359
#vis struct #mut_ident #generics #wc {
337360
#(
361+
#(#with_sized_docs)*
338362
#with_sized_vis #with_sized_idents: <#with_sized_types as #prelude::UnsizedType>::Mut<#top_lt>,
339363
)*
340364
}
@@ -351,8 +375,10 @@ impl UnsizedStructContext {
351375
let owned_types = get_field_types(owned_fields).collect_vec();
352376
let lt = new_lifetime(&self.generics, None);
353377

378+
let doc = format!("Owned type for [`{}`]", self.struct_ident);
354379
quote! {
355380
#(#[#additional_attributes])*
381+
#[doc = #doc]
356382
#[derive(#prelude::DeriveWhere)]
357383
#[derive_where(Debug, Copy, Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd; #(for<#lt> #owned_types,)*)]
358384
#vis struct #owned_ident #gen #where_clause {
@@ -388,8 +414,10 @@ impl UnsizedStructContext {
388414
#program_account
389415
)
390416
});
417+
let doc = format!("Sized portion of [`{}`]", self.struct_ident);
391418
let sized_struct = quote! {
392419
#(#[#additional_attributes])*
420+
#[doc = #doc]
393421
#[derive(#prelude::Align1, #sized_bytemuck_derives)]
394422
#sized_attributes
395423
#[repr(C, packed)]

star_frame_proc/src/util/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub fn reject_generics(item: &impl GetGenerics, error: Option<&str>) {
195195
}
196196

197197
pub fn phantom_generics_ident() -> Ident {
198-
format_ident!("__generics")
198+
format_ident!("_generics")
199199
}
200200

201201
pub fn phantom_generics_type(item: &impl GetGenerics) -> Option<Type> {

0 commit comments

Comments
 (0)