Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit b902429

Browse files
author
Hendrik van Antwerpen
committed
Use predefined constants for NodeID methods
1 parent 4dabbe5 commit b902429

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

stack-graphs/src/graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ pub struct NodeID {
429429
local_id: u32,
430430
}
431431

432-
const ROOT_NODE_ID: u32 = 1;
433-
const JUMP_TO_NODE_ID: u32 = 2;
432+
pub(crate) const ROOT_NODE_ID: u32 = 1;
433+
pub(crate) const JUMP_TO_NODE_ID: u32 = 2;
434434

435435
impl NodeID {
436436
/// Returns the ID of the singleton _root node_.

stack-graphs/src/serde/graph.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl StackGraph {
7676
for node in &self.nodes.data {
7777
let handle = match node {
7878
Node::DropScopes { id, .. } => {
79-
let node_id = id.into_node_id(graph)?;
79+
let node_id = id.to_node_id(graph)?;
8080
graph.add_drop_scopes_node(node_id)
8181
}
8282
Node::PopScopedSymbol {
@@ -85,7 +85,7 @@ impl StackGraph {
8585
is_definition,
8686
..
8787
} => {
88-
let node_id = id.into_node_id(graph)?;
88+
let node_id = id.to_node_id(graph)?;
8989
let symbol_handle = graph.add_symbol(&symbol);
9090
graph.add_pop_scoped_symbol_node(node_id, symbol_handle, *is_definition)
9191
}
@@ -95,7 +95,7 @@ impl StackGraph {
9595
is_definition,
9696
..
9797
} => {
98-
let node_id = id.into_node_id(graph)?;
98+
let node_id = id.to_node_id(graph)?;
9999
let symbol_handle = graph.add_symbol(&symbol);
100100
graph.add_pop_symbol_node(node_id, symbol_handle, *is_definition)
101101
}
@@ -106,8 +106,8 @@ impl StackGraph {
106106
is_reference,
107107
..
108108
} => {
109-
let node_id = id.into_node_id(graph)?;
110-
let scope_id = scope.into_node_id(graph)?;
109+
let node_id = id.to_node_id(graph)?;
110+
let scope_id = scope.to_node_id(graph)?;
111111
let symbol_handle = graph.add_symbol(&symbol);
112112
graph.add_push_scoped_symbol_node(
113113
node_id,
@@ -122,14 +122,14 @@ impl StackGraph {
122122
is_reference,
123123
..
124124
} => {
125-
let node_id = id.into_node_id(graph)?;
125+
let node_id = id.to_node_id(graph)?;
126126
let symbol_handle = graph.add_symbol(&symbol);
127127
graph.add_push_symbol_node(node_id, symbol_handle, *is_reference)
128128
}
129129
Node::Scope {
130130
id, is_exported, ..
131131
} => {
132-
let node_id = id.into_node_id(graph)?;
132+
let node_id = id.to_node_id(graph)?;
133133
graph.add_scope_node(node_id, *is_exported)
134134
}
135135
Node::JumpToScope { .. } | Node::Root { .. } => None,
@@ -173,8 +173,8 @@ impl StackGraph {
173173
precedence,
174174
} in &self.edges.data
175175
{
176-
let source_id = source.into_node_id(graph)?;
177-
let sink_id = sink.into_node_id(graph)?;
176+
let source_id = source.to_node_id(graph)?;
177+
let sink_id = sink.to_node_id(graph)?;
178178

179179
let source_handle = graph
180180
.node_for_id(source_id)
@@ -318,35 +318,32 @@ pub struct NodeID {
318318
}
319319

320320
impl NodeID {
321-
fn into_node_id(
321+
pub fn from_node_id(graph: &crate::graph::StackGraph, value: crate::graph::NodeID) -> Self {
322+
Self {
323+
file: value.file().map(|f| graph[f].to_string()),
324+
local_id: value.local_id(),
325+
}
326+
}
327+
328+
pub fn to_node_id(
322329
&self,
323330
graph: &crate::graph::StackGraph,
324331
) -> Result<crate::graph::NodeID, Error> {
325-
if let Some(file) = self.file.as_ref() {
326-
let handle = graph
327-
.get_file(&file)
328-
.ok_or(Error::FileNotFound(file.to_owned()))?;
329-
Ok(crate::graph::NodeID::new_in_file(handle, self.local_id))
330-
} else if self.is_root() {
331-
Ok(crate::graph::NodeID::root())
332-
} else if self.is_jump_to() {
332+
if let Some(file) = &self.file {
333+
let file = graph
334+
.get_file(file)
335+
.ok_or_else(|| Error::FileNotFound(file.clone()))?;
336+
Ok(crate::graph::NodeID::new_in_file(file, self.local_id))
337+
} else if self.local_id == crate::graph::JUMP_TO_NODE_ID {
333338
Ok(crate::graph::NodeID::jump_to())
339+
} else if self.local_id == crate::graph::ROOT_NODE_ID {
340+
Ok(crate::graph::NodeID::root())
334341
} else {
335342
Err(Error::InvalidGlobalNodeID(self.local_id))
336343
}
337344
}
338345
}
339346

340-
impl NodeID {
341-
fn is_root(&self) -> bool {
342-
self.local_id == crate::graph::NodeID::root().local_id()
343-
}
344-
345-
fn is_jump_to(&self) -> bool {
346-
self.local_id == crate::graph::NodeID::jump_to().local_id()
347-
}
348-
}
349-
350347
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
351348
#[serde(transparent)]
352349
pub struct Edges {

0 commit comments

Comments
 (0)