Build a human-readable label for a node in DOT output.
(node: &KgNode)
| 567 | |
| 568 | /// Build a human-readable label for a node in DOT output. |
| 569 | fn node_label(node: &KgNode) -> String { |
| 570 | let kind = compact(node.primary_type()); |
| 571 | match kind.as_str() { |
| 572 | "module" => { |
| 573 | // Last path component + trailing slash |
| 574 | let last = node |
| 575 | .label |
| 576 | .rsplit('/') |
| 577 | .find(|s| !s.is_empty()) |
| 578 | .unwrap_or(&node.label); |
| 579 | format!("{}/", last) |
| 580 | } |
| 581 | "atomic:File" => { |
| 582 | // Filename, optionally with match count |
| 583 | let filename = node.label.rsplit('/').next().unwrap_or(&node.label); |
| 584 | if let Some(ref meta) = node.metadata { |
| 585 | if let Some(matches) = meta.get("content_matches").and_then(|v| v.as_u64()) { |
| 586 | return format!("{}\n{} matches", filename, matches); |
| 587 | } |
| 588 | } |
| 589 | filename.to_string() |
| 590 | } |
| 591 | "entity" => { |
| 592 | // Label + kind/line info from metadata |
| 593 | let mut label = node.label.clone(); |
| 594 | if let Some(ref meta) = node.metadata { |
| 595 | let ent_kind = meta.get("kind").and_then(|v| v.as_str()); |
| 596 | let line = meta.get("line").and_then(|v| v.as_u64()); |
| 597 | let end_line = meta.get("end_line").and_then(|v| v.as_u64()); |
| 598 | if let (Some(k), Some(l)) = (ent_kind, line) { |
| 599 | if let Some(el) = end_line { |
| 600 | label = format!("{}\n{} L{}-{}", label, k, l, el); |
| 601 | } else { |
| 602 | label = format!("{}\n{} L{}", label, k, l); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | label |
| 607 | } |
| 608 | "atomic:Change" => { |
| 609 | // Short hash + optional summary excerpt |
| 610 | let mut label = node.label.clone(); |
| 611 | if let Some(ref summary) = node.summary { |
| 612 | let excerpt: String = summary.chars().take(30).collect(); |
| 613 | label = format!("{}\n{}", label, excerpt); |
| 614 | } |
| 615 | label |
| 616 | } |
| 617 | _ => { |
| 618 | // Everything else: label truncated to 40 chars |
| 619 | if node.label.len() > 40 { |
| 620 | let truncated: String = node.label.chars().take(37).collect(); |
| 621 | format!("{}...", truncated) |
| 622 | } else { |
| 623 | node.label.clone() |
| 624 | } |
| 625 | } |
| 626 | } |