ffizz-macros 0.5.0

FFI helper macros (typically not depended on directly)
Documentation
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
use syn::parse::{Error, Result};

/// The default order for a header item.
const DEFAULT_ORDER: usize = 100;

/// HeaderItem is a proc-macro-execution-time version of the HeaderItem object these macros will
/// insert into the Rust code.
#[derive(Debug, PartialEq)]
pub(crate) struct HeaderItem {
    pub(crate) order: usize,
    pub(crate) name: String,
    pub(crate) content: String,
}

impl HeaderItem {
    /// Create a HeaderItem, given a name and a vec of its attributes.  All ffizz_header-specific
    /// attributes are removed from attrs, and all docstrings are parsed into C header content.
    pub(crate) fn from_attrs(name: String, attrs: &mut Vec<syn::Attribute>) -> Result<Self> {
        let (doc, override_name, override_order) = Self::parse_attrs(attrs)?;
        let content = Self::parse_content(doc);
        Ok(Self {
            name: override_name.unwrap_or(name),
            order: override_order.unwrap_or(DEFAULT_ORDER),
            content,
        })
    }

    /// Parse a vec of attributes, extracting docstrings and ffizz attributes (name and header).
    /// Any ffizz attributes are removed from the given vector.
    ///
    /// Returns the docstrings, the name property (if found), and the order (if found)
    pub(crate) fn parse_attrs(
        attrs: &mut Vec<syn::Attribute>,
    ) -> Result<(Vec<String>, Option<String>, Option<usize>)> {
        let mut order = None;
        let mut name = None;

        let mut doc: Vec<String> = vec![];
        let mut kept_attrs = vec![];
        for attr in attrs.drain(..) {
            let mut keep_attr = true;
            match attr.parse_meta() {
                // docstrings are represented as #[doc = r"..."]
                Ok(syn::Meta::NameValue(nv)) => {
                    if nv.path.is_ident("doc") {
                        if let syn::Lit::Str(s) = nv.lit {
                            let s = s.value();
                            doc.extend(Self::parse_docstring_attr(s));
                        }
                    }
                }
                Ok(syn::Meta::List(metalist)) => {
                    if metalist.path.is_ident("ffizz") {
                        keep_attr = false;
                        for elt in metalist.nested {
                            let mut ok = false;
                            if let syn::NestedMeta::Meta(syn::Meta::NameValue(nv)) = elt {
                                if nv.path.is_ident("name") {
                                    if let syn::Lit::Str(s) = nv.lit {
                                        name = Some(s.value());
                                        ok = true;
                                    }
                                } else if nv.path.is_ident("order") {
                                    if let syn::Lit::Int(i) = nv.lit {
                                        if let Ok(i) = i.base10_parse::<usize>() {
                                            order = Some(i);
                                            ok = true;
                                        }
                                    }
                                }
                            }
                            if !ok {
                                return Err(Error::new_spanned(
                                    attr,
                                    "Valid #[fizz(..)] attribute properties here are name=\"..\" and order=.."
                                ));
                            }
                        }
                    }
                }
                _ => {
                    // ignore (and keep) any other attributes
                }
            }
            if keep_attr {
                kept_attrs.push(attr);
            }
        }
        *attrs = kept_attrs;

        Ok((doc, name, order))
    }

    /// Parse a docstring attribute value into an array of docstring lines, accounting for
    /// the peculiar ways we receive these from the parser.  The goal here is to capture
    /// the user's intended text, without any indentation or `*` prefixes.
    fn parse_docstring_attr(s: String) -> Vec<String> {
        // We get everything but the comment characters, including whitespace.
        //  - For `/// foo`, we will get " foo".
        //  - For `/** \n    * foo */`, we will get " \n    * foo ".

        // For simplicity, we assume the docstring is either using `///` or
        // included in one big `/** .. */` comment.
        if s.contains('\n') {
            // /** ... */ - style

            let mut lines: Vec<_> = s.split('\n').collect();

            fn is_boring(line: Option<&&str>) -> bool {
                line.map(|line| line.chars().all(|c| c.is_whitespace() || c == '*'))
                    .unwrap_or(false)
            }

            // if the first line is boring, we can drop it.  This is the case when the `/**`
            // is alone on the first line.
            if is_boring(lines.first()) {
                lines.remove(0);
            }

            // if the last line is boring, it will mess up the prefix determination, so remove it.
            // A non-boring last line occurs when the comment is terminated with `*/` on the same
            // line.
            if is_boring(lines.last()) {
                lines.pop();
            }

            // now any remaining lines after the first probably have a common
            // prefix of whitespace and `*`.  Guess that from the last line.
            let prefix = lines
                .last()
                .map(|first_line| {
                    let offset = first_line
                        .find(|c: char| !(c.is_whitespace() || c == '*'))
                        .unwrap_or(first_line.len());
                    first_line[..offset].to_string()
                })
                .unwrap_or_else(String::new);

            // and remove it from all lines where it appears
            let lines: Vec<String> = lines
                .iter()
                .map(|line| {
                    if line.starts_with(&prefix) {
                        line[prefix.len()..].to_string()
                    } else {
                        line.to_string()
                    }
                })
                .collect();

            lines
        } else {
            // /// - style

            // strip a single leading space, if it exists.
            if let Some(stripped) = s.strip_prefix(' ') {
                vec![stripped.to_string()]
            } else {
                vec![s]
            }
        }
    }

    /// Parse a docstring, presented as a vec of lines, to extract C declarations and comments.
    pub(crate) fn parse_content(doc: Vec<String>) -> String {
        let mut content = vec![];
        let mut in_decl = false;
        let mut strip_new_blank_comments = true;

        /// strip trailing blank comment lines
        fn strip_trailing_blank_comments(lines: &mut Vec<String>) {
            while let Some(line) = lines.last() {
                if line == "//" {
                    lines.pop();
                } else {
                    break;
                }
            }
        }

        for line in doc {
            if in_decl {
                if line.trim() == "```" {
                    in_decl = false;
                    strip_new_blank_comments = true;
                    continue;
                }
                content.push(line);
            } else {
                if strip_new_blank_comments && line.is_empty() {
                    continue;
                }
                if line.trim() == "```c" {
                    in_decl = true;
                    strip_trailing_blank_comments(&mut content);
                    continue;
                }
                if !line.is_empty() {
                    content.push(format!("// {line}"));
                } else {
                    content.push("//".to_string());
                }
                strip_new_blank_comments = false;
            }
        }

        strip_trailing_blank_comments(&mut content);

        itertools::join(content, "\n")
    }

    /// Write the content of this HeaderItem into a TokenStream such that the resulting binary will
    /// include the HeaderItem in its `::ffizz_header::FFIZZ_HEADER_ITEMS` array.
    pub(crate) fn to_tokens(&self, tokens: &mut TokenStream2) {
        let HeaderItem {
            order,
            name,
            content,
        } = self;
        let item_name = syn::Ident::new(&format!("FFIZZ_HDR__{name}"), Span::call_site());

        // insert an invocation of linkme::distributed_slice to add this header item to
        // the FFIZZ_HEADER_ITEMS slice.
        tokens.extend(quote! {
            #[::ffizz_header::linkme::distributed_slice(::ffizz_header::FFIZZ_HEADER_ITEMS)]
            #[linkme(crate=::ffizz_header::linkme)]
            #[allow(non_upper_case_globals)]
            static #item_name: ::ffizz_header::HeaderItem = ::ffizz_header::HeaderItem {
                order: #order,
                name: #name,
                content: #content,
            };
        });
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use syn::parse::{Parse, ParseStream};
    use syn::parse_quote;

    struct Attrs(Vec<syn::Attribute>);

    impl Parse for Attrs {
        fn parse(input: ParseStream) -> Result<Self> {
            let attrs = input.call(syn::Attribute::parse_outer)?;
            Ok(Attrs(attrs))
        }
    }

    #[test]
    fn parse_attrs_simple() {
        let mut attrs: Attrs = parse_quote! {
            /// aaa
            /// bbb
        };
        let (doc, name, order) = HeaderItem::parse_attrs(&mut attrs.0).unwrap();
        assert_eq!(order, None);
        assert_eq!(name, None);
        assert_eq!(doc, vec!["aaa", "bbb"]);
    }

    #[test]
    fn parse_attrs_multiline() {
        let mut attrs: Attrs = parse_quote! {
            /**
             * aaa
             * bbb
             */
        };
        let (doc, name, order) = HeaderItem::parse_attrs(&mut attrs.0).unwrap();
        assert_eq!(order, None);
        assert_eq!(name, None);
        assert_eq!(doc, vec!["aaa", "bbb"]);
    }

    #[test]
    fn parse_attrs_single_override_attr() {
        let mut attrs: Attrs = parse_quote! {
            /// aaa
            #[ffizz(name="override")]
            /// bbb
        };
        let (doc, name, order) = HeaderItem::parse_attrs(&mut attrs.0).unwrap();
        assert_eq!(order, None);
        assert_eq!(name, Some(String::from("override")));
        assert_eq!(doc, vec!["aaa", "bbb"]);
        // check that the #[ffizz(..)] attributes were stripped
        assert_eq!(attrs.0.len(), 2);
    }

    #[test]
    fn parse_attrs_multi_override_attr() {
        let mut attrs: Attrs = parse_quote! {
            #[ffizz(name="not seen")]
            /// aaa
            #[ffizz(name="override")]
            #[ffizz(order=13)]
            /// bbb
        };
        let (doc, name, order) = HeaderItem::parse_attrs(&mut attrs.0).unwrap();
        assert_eq!(order, Some(13));
        assert_eq!(name, Some(String::from("override")));
        assert_eq!(doc, vec!["aaa", "bbb"]);
        // check that the #[ffizz(..)] attributes were stripped
        assert_eq!(attrs.0.len(), 2);
    }

    #[test]
    fn parse_attrs_name_order_same_attr() {
        let mut attrs: Attrs = parse_quote! {
            #[ffizz(name="override", order=13)]
            /// aaa
            /// bbb
        };
        let (doc, name, order) = HeaderItem::parse_attrs(&mut attrs.0).unwrap();
        assert_eq!(order, Some(13));
        assert_eq!(name, Some(String::from("override")));
        assert_eq!(doc, vec!["aaa", "bbb"]);
        // check that the #[ffizz(..)] attributes were stripped
        assert_eq!(attrs.0.len(), 2);
    }

    #[test]
    fn parse_attrs_invalid_ffizz_attr() {
        let mut attrs: Attrs = parse_quote! {
            #[ffizz(blergh="uhoh", snars=13)]
            /// aaa
            /// bbb
        };
        assert!(HeaderItem::parse_attrs(&mut attrs.0).is_err());
    }

    fn multiline(s: &'static str) -> String {
        // strip `/**` and `*/`.
        s[3..s.len() - 2].to_string()
    }

    #[test]
    fn parse_doc_attr_multiline_1() {
        assert_eq!(
            HeaderItem::parse_docstring_attr(multiline(
                "/**
                  * hello
                  */"
            )),
            vec!["hello".to_string()],
        )
    }

    #[test]
    fn parse_doc_attr_multiline_2() {
        assert_eq!(
            HeaderItem::parse_docstring_attr(multiline(
                "/** hello
                  */"
            )),
            vec!["hello".to_string()],
        )
    }

    #[test]
    fn parse_doc_attr_multiline_3() {
        assert_eq!(
            HeaderItem::parse_docstring_attr(multiline(
                "/**
                  */"
            )),
            Vec::<String>::new(),
        )
    }

    #[test]
    fn parse_doc_attr_multiline_4() {
        assert_eq!(
            HeaderItem::parse_docstring_attr(multiline(
                "/**
                  * two
                  * lines
                  */"
            )),
            vec!["two", "lines"],
        )
    }

    #[test]
    fn parse_doc_attr_multiline_5() {
        assert_eq!(
            HeaderItem::parse_docstring_attr(multiline(
                "/**
                  * three
                  *   indented
                  * lines
                  */"
            )),
            vec!["three", "  indented", "lines"],
        )
    }

    #[test]
    fn parse_doc_attr_single_line() {
        assert_eq!(HeaderItem::parse_docstring_attr(" foo".into()), vec!["foo"],)
    }

    #[test]
    fn parse_doc_attr_single_line_empty() {
        assert_eq!(HeaderItem::parse_docstring_attr("".into()), vec![""],)
    }

    #[test]
    fn parse_content_just_text() {
        assert_eq!(
            HeaderItem::parse_content(vec!["some".to_string(), "content".to_string()]),
            "// some\n// content".to_string()
        );
    }

    #[test]
    fn parse_content_single_decl() {
        assert_eq!(
            HeaderItem::parse_content(vec![
                "intro".to_string(),
                "```c".to_string(),
                "void foo(void);".to_string(),
                "```".to_string(),
                "suffix".to_string(),
            ]),
            "// intro\nvoid foo(void);\n// suffix".to_string()
        );
    }

    #[test]
    fn parse_content_empty_lines() {
        assert_eq!(
            HeaderItem::parse_content(vec![
                "".to_string(),
                "intro".to_string(),
                "".to_string(),
                "suffix".to_string(),
                "".to_string(),
            ]),
            "// intro\n//\n// suffix".to_string()
        );
    }

    #[test]
    fn parse_content_multi_decl() {
        assert_eq!(
            HeaderItem::parse_content(vec![
                "aaa".to_string(),
                "".to_string(),
                "```c".to_string(),
                "void foo(void);".to_string(),
                "```".to_string(),
                "".to_string(),
                "bbb".to_string(),
                "".to_string(),
                "```c".to_string(),
                "void bar(void);".to_string(),
                "```".to_string(),
                "".to_string(),
            ]),
            "// aaa\nvoid foo(void);\n// bbb\nvoid bar(void);".to_string()
        );
    }
}