workspacer_syntax/
generate_trait_signature.rs1crate::ix!();
3
4#[derive(Debug, Clone)]
5pub struct TraitSignatureGenerator(ast::Trait);
6
7impl GenerateSignature for ast::Trait {
8 fn generate_signature(&self) -> String {
9 trace!("generate_signature called on ast::Trait (no custom opts)");
11 self.generate_signature_with_opts(&SignatureOptions::default())
12 }
13
14 fn generate_signature_with_opts(&self, opts: &SignatureOptions) -> String {
15 trace!("generate_signature_with_opts called on ast::Trait; fully_expand = {}", opts.fully_expand());
16
17 let vis = if let Some(vis) = self.visibility() {
19 let txt = vis.syntax().text().to_string();
20 trace!("Trait visibility: {:?}", txt);
21 format!("{} ", txt)
22 } else {
23 "".to_string()
24 };
25
26 let name = self.name()
27 .map(|it| it.text().to_string())
28 .unwrap_or_else(|| "<anon_trait>".to_string());
29 trace!("Trait name: {:?}", name);
30
31 let generics = self.generic_param_list()
32 .map(|gp| gp.syntax().text().to_string())
33 .unwrap_or_default();
34 if !generics.is_empty() {
35 trace!("Trait generics: {:?}", generics);
36 }
37
38 if !*opts.fully_expand() {
40 trace!("Not fully expanding trait items; returning short signature");
41 return format!("{}trait {}{} {{ /* trait items here */ }}", vis, name, generics);
42 }
43
44
45 let mut lines = Vec::new();
47 if let Some(item_list) = self.assoc_item_list() {
48 for assoc in item_list.assoc_items() {
49 if let Some(fn_ast) = ast::Fn::cast(assoc.syntax().clone()) {
51 let mut opts = opts.clone();
52 opts.set_add_semicolon(true);
53 let method_sig = fn_ast.generate_signature_with_opts(&opts);
57 lines.push(method_sig);
58
59 } else if let Some(type_ast) = ast::TypeAlias::cast(assoc.syntax().clone()) {
61 let raw_txt = type_ast.syntax().text().to_string();
64 lines.push(raw_txt);
65
66 } else if let Some(const_ast) = ast::Const::cast(assoc.syntax().clone()) {
68 let raw_txt = const_ast.syntax().text().to_string();
69 lines.push(raw_txt);
70 }
71 }
72 }
73
74 let mut items_str = String::new();
76 for line in lines {
77 for sub_line in line.lines() {
78 items_str.push_str(" ");
79 items_str.push_str(sub_line);
80 items_str.push('\n');
81 }
82 }
83
84 if items_str.trim().is_empty() {
85 trace!("No associated items found in trait; returning empty braces");
86 format!("{}trait {}{} {{}}", vis, name, generics)
87 } else {
88 trace!("Returning fully expanded trait signature with items");
89 format!("{}trait {}{} {{\n{}}}", vis, name, generics, items_str)
90 }
91 }
92}