1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use proc_macro2::{Ident, Span, TokenStream};
use syn::{
parse::{ParseStream, Parser},
spanned::Spanned,
};
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum ExportScope {
PubOnly,
Prefix(String),
All,
}
impl Default for ExportScope {
fn default() -> ExportScope {
ExportScope::PubOnly
}
}
pub trait ExportedParams: Sized {
fn parse_stream(args: ParseStream) -> syn::Result<Self>;
fn no_attrs() -> Self;
fn from_info(info: ExportInfo) -> syn::Result<Self>;
}
#[derive(Debug, Clone)]
pub struct AttrItem {
pub key: Ident,
pub value: Option<syn::LitStr>,
pub span: Span,
}
#[derive(Debug, Clone)]
pub struct ExportInfo {
pub item_span: Span,
pub items: Vec<AttrItem>,
}
pub fn parse_attr_items(args: ParseStream) -> syn::Result<ExportInfo> {
if args.is_empty() {
return Ok(ExportInfo {
item_span: args.span(),
items: Vec::new(),
});
}
let arg_list = args.call(syn::punctuated::Punctuated::parse_separated_nonempty)?;
parse_punctuated_items(arg_list)
}
pub fn parse_punctuated_items(
arg_list: syn::punctuated::Punctuated<syn::Expr, syn::Token![,]>,
) -> syn::Result<ExportInfo> {
let list_span = arg_list.span();
let mut attrs = Vec::new();
for arg in arg_list {
let arg_span = arg.span();
let (key, value) = match arg {
syn::Expr::Assign(syn::ExprAssign {
ref left,
ref right,
..
}) => {
let attr_name = match left.as_ref() {
syn::Expr::Path(syn::ExprPath {
path: attr_path, ..
}) => attr_path.get_ident().cloned().ok_or_else(|| {
syn::Error::new(attr_path.span(), "expecting attribute name")
})?,
x => return Err(syn::Error::new(x.span(), "expecting attribute name")),
};
let attr_value = match right.as_ref() {
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(string),
..
}) => string.clone(),
x => return Err(syn::Error::new(x.span(), "expecting string literal")),
};
(attr_name, Some(attr_value))
}
syn::Expr::Path(syn::ExprPath { path, .. }) => path
.get_ident()
.cloned()
.map(|a| (a, None))
.ok_or_else(|| syn::Error::new(path.span(), "expecting attribute name"))?,
x => return Err(syn::Error::new(x.span(), "expecting identifier")),
};
attrs.push(AttrItem {
key,
value,
span: arg_span,
});
}
Ok(ExportInfo {
item_span: list_span,
items: attrs,
})
}
pub fn outer_item_attributes<T: ExportedParams>(
args: TokenStream,
_attr_name: &str,
) -> syn::Result<T> {
if args.is_empty() {
return Ok(T::no_attrs());
}
let arg_list = syn::punctuated::Punctuated::parse_separated_nonempty.parse2(args)?;
T::from_info(parse_punctuated_items(arg_list)?)
}
pub fn inner_item_attributes<T: ExportedParams>(
attrs: &mut Vec<syn::Attribute>,
attr_name: &str,
) -> syn::Result<T> {
if let Some(index) = attrs
.iter()
.position(|a| a.path.get_ident().map_or(false, |i| *i == attr_name))
{
let rhai_fn_attr = attrs.remove(index);
if let Some(duplicate) = attrs
.iter()
.find(|a| a.path.get_ident().map_or(false, |i| *i == attr_name))
{
return Err(syn::Error::new(
duplicate.span(),
format!("duplicated attribute '{attr_name}'"),
));
}
rhai_fn_attr.parse_args_with(T::parse_stream)
} else {
Ok(T::no_attrs())
}
}
#[cfg(feature = "metadata")]
pub fn doc_attributes(attrs: &[syn::Attribute]) -> syn::Result<Vec<String>> {
let mut comments = Vec::new();
let mut buf = String::new();
for attr in attrs {
if let Some(i) = attr.path.get_ident() {
if *i == "doc" {
if let syn::Meta::NameValue(syn::MetaNameValue {
lit: syn::Lit::Str(s),
..
}) = attr.parse_meta()?
{
let mut line = s.value();
if line.contains('\n') {
if !buf.is_empty() {
comments.push(buf.clone());
buf.clear();
}
line.insert_str(0, "/**");
line.push_str("*/");
comments.push(line);
} else {
if !buf.is_empty() {
buf.push('\n');
}
buf.push_str("///");
buf.push_str(&line);
}
}
}
}
}
if !buf.is_empty() {
comments.push(buf);
}
Ok(comments)
}
pub fn collect_cfg_attr(attrs: &[syn::Attribute]) -> Vec<syn::Attribute> {
attrs
.iter()
.filter(|&a| a.path.get_ident().map_or(false, |i| *i == "cfg"))
.cloned()
.collect()
}