1use crate::data_model::Function;
2
3use super::docstring_from_attrs;
4
5impl Function {
6 /// Fully qualified name of the variant
7 pub fn path_str(&self) -> String {
8 self.path.join("::")
9 }
10 pub fn parse(parent: &[&str], ast: &syn::ItemFn) -> Self {
11 let name = ast.sig.ident.to_string();
12 let path: Vec<&str> = parent.iter().copied().chain(Some(name.as_str())).collect();
13 let docstring = docstring_from_attrs(&ast.attrs);
14 Self {
15 path: path.iter().map(|s| s.to_string()).collect(),
16 docstring,
17 }
18 }
19}
20
21#[cfg(test)]
22mod tests {
23 use super::*;
24
25 use insta::assert_yaml_snapshot;
26
27 #[test]
28 fn test_function_parse() {
29 let item: syn::ItemFn = syn::parse_quote! {
30 /// This is a docstring
31 pub fn my_function() {}
32 };
33 let func = Function::parse(&["my_module"], &item);
34 assert_yaml_snapshot!(func, @r###"
35 ---
36 path:
37 - my_module
38 - my_function
39 docstring: This is a docstring
40 "###);
41 }
42}