Skip to main content

backhopper_xref_graph/
vertex.rs

1// Copyright (C) 2026 Michael S. Klishin and Contributors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// See LICENSE-APACHE and LICENSE-MIT for details.
4
5//! Graph vertices.
6//!
7//! Vertices are entities the cross-reference engine tracks. We keep them as
8//! a single enum over the existing `backhopper-core` newtypes so that a
9//! `BTreeSet<Vertex>` can mix levels (function, module, application,
10//! behaviour) when an analysis wants that.
11
12use std::fmt;
13
14use backhopper_core::{ApplicationName, BehaviourName, Mfa, ModuleName};
15use serde::{Deserialize, Serialize};
16
17#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
18#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
19pub enum Vertex {
20    Function(Mfa),
21    Module(ModuleName),
22    Application(ApplicationName),
23    Behaviour(BehaviourName),
24}
25
26impl Vertex {
27    pub fn as_module(&self) -> Option<&ModuleName> {
28        if let Vertex::Module(m) = self {
29            Some(m)
30        } else {
31            None
32        }
33    }
34
35    pub fn as_application(&self) -> Option<&ApplicationName> {
36        if let Vertex::Application(a) = self {
37            Some(a)
38        } else {
39            None
40        }
41    }
42
43    pub fn as_behaviour(&self) -> Option<&BehaviourName> {
44        if let Vertex::Behaviour(b) = self {
45            Some(b)
46        } else {
47            None
48        }
49    }
50}
51
52impl fmt::Display for Vertex {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            Vertex::Function(mfa) => write!(f, "{mfa}"),
56            Vertex::Module(m) => write!(f, "{m}"),
57            Vertex::Application(a) => write!(f, "app:{a}"),
58            Vertex::Behaviour(b) => write!(f, "behaviour:{b}"),
59        }
60    }
61}