Skip to content

Commit

Permalink
Adding attributes to Params and Fields #521 (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
ambiguousname authored Aug 19, 2024
1 parent ec09003 commit 3eaf5a4
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 94 deletions.
2 changes: 1 addition & 1 deletion core/src/ast/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl RenameAttr {
}

/// Whether this rename is empty and will perform no changes
fn is_empty(&self) -> bool {
pub(crate) fn is_empty(&self) -> bool {
self.pattern.is_none()
}

Expand Down
9 changes: 6 additions & 3 deletions core/src/ast/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use quote::{quote, ToTokens};
use serde::{Deserialize, Serialize};
use std::fmt;

use super::{Docs, Ident, Param, SelfParam, TypeName};
use super::{Attrs, Docs, Ident, Param, SelfParam, TypeName};

/// A named lifetime, e.g. `'a`.
///
Expand Down Expand Up @@ -126,10 +126,13 @@ impl LifetimeEnv {
/// Returns a [`LifetimeEnv`] for a struct, accounding for lifetimes and bounds
/// defined in the struct generics, as well as implicit lifetime bounds in
/// the struct's fields. For example, the field `&'a Foo<'b>` implies `'b: 'a`.
pub fn from_struct_item(strct: &syn::ItemStruct, fields: &[(Ident, TypeName, Docs)]) -> Self {
pub fn from_struct_item(
strct: &syn::ItemStruct,
fields: &[(Ident, TypeName, Docs, Attrs)],
) -> Self {
let mut this = LifetimeEnv::new();
this.extend_generics(&strct.generics);
for (_, typ, _) in fields {
for (_, typ, _, _) in fields {
this.extend_implicit_lifetime_bounds(typ, None);
}
this
Expand Down
10 changes: 10 additions & 0 deletions core/src/ast/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ pub struct SelfParam {
/// The type of the parameter, which will be a named reference to
/// the associated struct,
pub path_type: PathType,

/// Associated attributes with this self parameter. Used in Demo Generation, mostly.
pub attrs: Attrs,
}

impl SelfParam {
Expand All @@ -247,6 +250,7 @@ impl SelfParam {
.as_ref()
.map(|(_, lt)| (lt.into(), Mutability::from_syn(&rec.mutability))),
path_type,
attrs: Attrs::from_attrs(&rec.attrs),
}
}
}
Expand All @@ -260,6 +264,9 @@ pub struct Param {

/// The type of the parameter.
pub ty: TypeName,

/// Parameter attributes (like #[diplomat::demo(label = "Out")])
pub attrs: Attrs,
}

impl Param {
Expand All @@ -277,9 +284,12 @@ impl Param {
_ => panic!("Unexpected param type"),
};

let attrs = Attrs::from_attrs(&t.attrs);

Param {
name: (&ident.ident).into(),
ty: TypeName::from_syn(&t.ty, Some(self_path_type)),
attrs,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/src/ast/methods.rs
expression: "Method::from_syn(&syn::parse_quote! {\n /// Some docs.\n #[diplomat :: rust_link(foo :: Bar :: batz, FnInStruct)]\n #[cfg(any(feature = \"foo\", not(feature = \"bar\")))] fn\n foo(x : u64, y : MyCustomStruct) {}\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
expression: "Method::from_syn(&syn::parse_quote! {\n #[doc = r\" Some docs.\"]\n #[diplomat::rust_link(foo::Bar::batz, FnInStruct)]\n #[cfg(any(feature = \"foo\", not(feature = \"bar\")))] fn\n foo(x: u64, y: MyCustomStruct) {}\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
---
name: foo
docs:
Expand All @@ -18,16 +18,17 @@ params:
- name: x
ty:
Primitive: u64
attrs: {}
- name: y
ty:
Named:
path:
elements:
- MyCustomStruct
lifetimes: []
attrs: {}
return_type: ~
lifetime_env: {}
attrs:
cfg:
- "# [cfg (any (feature = \"foo\" , not (feature = \"bar\")))]"

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/src/ast/methods.rs
expression: "Method::from_syn(&syn::parse_quote! {\n #[diplomat :: rust_link(foo :: Bar :: batz, FnInStruct)] fn\n foo(& mut self, x : u64, y : MyCustomStruct) -> u64 { x }\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
expression: "Method::from_syn(&syn::parse_quote! {\n #[diplomat::rust_link(foo::Bar::batz, FnInStruct)] fn\n foo(&mut self, x: u64, y: MyCustomStruct) -> u64 { x }\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
---
name: foo
docs:
Expand All @@ -22,19 +22,21 @@ self_param:
elements:
- MyStructContainingMethod
lifetimes: []
attrs: {}
params:
- name: x
ty:
Primitive: u64
attrs: {}
- name: y
ty:
Named:
path:
elements:
- MyCustomStruct
lifetimes: []
attrs: {}
return_type:
Primitive: u64
lifetime_env: {}
attrs: {}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/src/ast/methods.rs
expression: "Method::from_syn(&syn::parse_quote! {\n fn foo(& self, x : u64, y : MyCustomStruct) {}\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
expression: "Method::from_syn(&syn::parse_quote! {\n fn foo(&self, x: u64, y: MyCustomStruct) {}\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
---
name: foo
docs:
Expand All @@ -16,18 +16,20 @@ self_param:
elements:
- MyStructContainingMethod
lifetimes: []
attrs: {}
params:
- name: x
ty:
Primitive: u64
attrs: {}
- name: y
ty:
Named:
path:
elements:
- MyCustomStruct
lifetimes: []
attrs: {}
return_type: ~
lifetime_env: {}
attrs: {}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/src/ast/methods.rs
expression: "Method::from_syn(&syn::parse_quote! {\n /// Some docs.\n /// Some more docs.\n ///\n /// Even more docs.\n #[diplomat :: rust_link(foo :: Bar :: batz, FnInEnum)] fn\n foo(x : u64, y : MyCustomStruct) -> u64 { x }\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
expression: "Method::from_syn(&syn::parse_quote! {\n #[doc = r\" Some docs.\"] #[doc = r\" Some more docs.\"]\n #[doc = r\"\"] #[doc = r\" Even more docs.\"]\n #[diplomat::rust_link(foo::Bar::batz, FnInEnum)] fn\n foo(x: u64, y: MyCustomStruct) -> u64 { x }\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
---
name: foo
docs:
Expand All @@ -18,15 +18,16 @@ params:
- name: x
ty:
Primitive: u64
attrs: {}
- name: y
ty:
Named:
path:
elements:
- MyCustomStruct
lifetimes: []
attrs: {}
return_type:
Primitive: u64
lifetime_env: {}
attrs: {}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/src/ast/methods.rs
expression: "Method::from_syn(&syn::parse_quote! {\n /// Some docs.\n #[diplomat :: rust_link(foo :: Bar :: batz, FnInStruct)] fn\n foo(x : u64, y : MyCustomStruct) {}\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
expression: "Method::from_syn(&syn::parse_quote! {\n #[doc = r\" Some docs.\"]\n #[diplomat::rust_link(foo::Bar::batz, FnInStruct)] fn\n foo(x: u64, y: MyCustomStruct) {}\n },\n PathType::new(Path::empty().sub_path(Ident::from(\"MyStructContainingMethod\"))),\n None, &Attrs::default())"
---
name: foo
docs:
Expand All @@ -18,14 +18,15 @@ params:
- name: x
ty:
Primitive: u64
attrs: {}
- name: y
ty:
Named:
path:
elements:
- MyCustomStruct
lifetimes: []
attrs: {}
return_type: ~
lifetime_env: {}
attrs: {}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declared_types:
- Primitive: i32
- - ""
- []
- {}
- - b
- Box:
Named:
Expand All @@ -26,6 +27,7 @@ declared_types:
lifetimes: []
- - ""
- []
- {}
methods:
- name: new
docs:
Expand All @@ -37,6 +39,7 @@ declared_types:
- name: x
ty:
Primitive: i32
attrs: {}
return_type:
Named:
path:
Expand All @@ -59,10 +62,12 @@ declared_types:
elements:
- NonOpaqueStruct
lifetimes: []
attrs: {}
params:
- name: new_a
ty:
Primitive: i32
attrs: {}
return_type: ~
lifetime_env: {}
attrs: {}
Expand Down Expand Up @@ -106,6 +111,7 @@ declared_types:
elements:
- OpaqueStruct
lifetimes: []
attrs: {}
params: []
return_type:
Named:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: core/src/ast/structs.rs
expression: "Struct::new(&syn::parse_quote! {\n /// Some docs.\n #[diplomat :: rust_link(foo :: Bar, Struct)] struct\n MyLocalStruct { a : i32, b : Box < MyLocalStruct > }\n }, true, &Default::default())"
expression: "Struct::new(&syn::parse_quote! {\n #[doc = r\" Some docs.\"]\n #[diplomat::rust_link(foo::Bar, Struct)] struct MyLocalStruct\n { a: i32, b: Box<MyLocalStruct> }\n }, true, &Default::default())"
---
name: MyLocalStruct
docs:
Expand All @@ -17,6 +17,7 @@ fields:
- Primitive: i32
- - ""
- []
- {}
- - b
- Box:
Named:
Expand All @@ -26,7 +27,7 @@ fields:
lifetimes: []
- - ""
- []
- {}
methods: []
output_only: true
attrs: {}

4 changes: 2 additions & 2 deletions core/src/ast/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Struct {
pub name: Ident,
pub docs: Docs,
pub lifetimes: LifetimeEnv,
pub fields: Vec<(Ident, TypeName, Docs)>,
pub fields: Vec<(Ident, TypeName, Docs, Attrs)>,
pub methods: Vec<Method>,
pub output_only: bool,
pub attrs: Attrs,
Expand All @@ -33,7 +33,7 @@ impl Struct {
let type_name = TypeName::from_syn(&field.ty, Some(self_path_type.clone()));
let docs = Docs::from_attrs(&field.attrs);

(name, type_name, docs)
(name, type_name, docs, Attrs::from_attrs(&field.attrs))
})
.collect();

Expand Down
Loading

0 comments on commit 3eaf5a4

Please sign in to comment.