-
Notifications
You must be signed in to change notification settings - Fork 73
/
syn_parsing.rs
53 lines (45 loc) · 1.4 KB
/
syn_parsing.rs
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
// Copyright 2021 Oxide Computer Company
use syn::{
parse::{Parse, ParseStream},
Attribute, Signature, Visibility,
};
/// Represent an item without concern for its body which may (or may not)
/// contain syntax errors.
pub(crate) struct ItemFnForSignature {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub sig: Signature,
pub _block: proc_macro2::TokenStream,
}
impl Parse for ItemFnForSignature {
fn parse(input: ParseStream) -> syn::parse::Result<Self> {
let attrs = input.call(Attribute::parse_outer)?;
let vis: Visibility = input.parse()?;
let sig: Signature = input.parse()?;
let block = input.parse()?;
Ok(ItemFnForSignature { attrs, vis, sig, _block: block })
}
}
#[cfg(test)]
mod tests {
use quote::quote;
use syn::{Signature, Visibility};
use crate::syn_parsing::ItemFnForSignature;
#[test]
fn test_busted_function() {
let f = quote! {
fn foo(parameter: u32) -> u32 {
(void) printf("%s", "no language C here!");
return (0);
}
};
let ast: ItemFnForSignature = syn::parse2(f).unwrap();
let sig: Signature = syn::parse2(quote! {
fn foo(parameter: u32) -> u32
})
.unwrap();
assert!(ast.attrs.is_empty());
assert_eq!(ast.vis, Visibility::Inherited);
assert_eq!(ast.sig, sig);
}
}