Skip to main content

meta_ast/graph/
naming.rs

1//! Canonical names of graph nodes.
2//!
3//! One authority for the writer side: a node kind or display name is spelled
4//! here, so the serialized graph output cannot drift from the node model by
5//! holding its own copy of the strings. Stable shard names are a separate
6//! concern and keep their own module.
7
8use crate::graph::node::NodeData;
9use crate::language::LangId;
10
11/// Kind name of a node: `file`, `symbol`, `external` or `data`.
12pub fn node_kind_name(node: &NodeData) -> &'static str {
13    node.kind_str()
14}
15
16/// Name a reader shows for a node, when the node has one.
17///
18/// Symbols carry their name and data nodes an optional name. Files and
19/// external dependencies are addressed by path instead.
20pub fn node_display_name(node: &NodeData) -> Option<&str> {
21    match node {
22        NodeData::Symbol(symbol) => Some(symbol.name.as_str()),
23        NodeData::Data(data) => data.name.as_deref(),
24        _ => None,
25    }
26}
27
28/// Language of a node, when the node has one.
29pub fn node_language(node: &NodeData) -> Option<LangId> {
30    match node {
31        NodeData::File(file) => Some(file.language),
32        NodeData::External(external) => Some(external.language),
33        _ => None,
34    }
35}