Build a function signature string.
(&self, node: &Node, source: &str)
| 385 | |
| 386 | /// Build a function signature string. |
| 387 | fn build_function_signature(&self, node: &Node, source: &str) -> Option<String> { |
| 388 | let name_node = node.child_by_field_name("name")?; |
| 389 | let name = self.node_text(&name_node, source); |
| 390 | |
| 391 | let params = node |
| 392 | .child_by_field_name("parameters") |
| 393 | .map(|n| self.node_text(&n, source)) |
| 394 | .unwrap_or_else(|| "()".to_string()); |
| 395 | |
| 396 | let return_type = node |
| 397 | .child_by_field_name("return_type") |
| 398 | .map(|n| format!(" -> {}", self.node_text(&n, source))); |
| 399 | |
| 400 | // Check for visibility, async, unsafe |
| 401 | let mut qualifiers = Vec::new(); |
| 402 | let mut cursor = node.walk(); |
| 403 | let children: Vec<_> = node.children(&mut cursor).collect(); |
| 404 | for child in &children { |
| 405 | match child.kind() { |
| 406 | "visibility_modifier" => qualifiers.push(self.node_text(child, source)), |
| 407 | "async" => qualifiers.push("async".to_string()), |
| 408 | "unsafe" => qualifiers.push("unsafe".to_string()), |
| 409 | _ => {} |
| 410 | } |
| 411 | // Stop once we hit the name |
| 412 | if child.id() == name_node.id() { |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | qualifiers.push("fn".to_string()); |
| 418 | |
| 419 | Some(format!( |
| 420 | "{} {}{}{}", |
| 421 | qualifiers.join(" "), |
| 422 | name, |
| 423 | params, |
| 424 | return_type.unwrap_or_default() |
| 425 | )) |
| 426 | } |
| 427 | |
| 428 | /// Build a struct signature string. |
| 429 | fn build_struct_signature(&self, node: &Node, source: &str) -> Option<String> { |