Skip to main content

helix_ast/
graph.rs

1use serde::{Deserialize, Serialize};
2
3/// Type alias for node IDs.
4pub type NodeId = u64;
5
6/// Type alias for edge IDs.
7pub type EdgeId = u64;
8/// A reference to nodes.
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum NodeRef {
12    /// All nodes.
13    All,
14    /// Concrete node IDs.
15    Ids(Vec<NodeId>),
16    /// Named variable.
17    Var(String),
18    /// Runtime parameter containing IDs.
19    Param(String),
20}
21
22impl NodeRef {
23    /// Reference all nodes.
24    pub fn all() -> Self {
25        Self::All
26    }
27
28    /// Reference one node.
29    pub fn id(id: NodeId) -> Self {
30        Self::Ids(vec![id])
31    }
32
33    /// Reference multiple nodes.
34    pub fn ids(ids: impl IntoIterator<Item = NodeId>) -> Self {
35        Self::Ids(ids.into_iter().collect())
36    }
37
38    /// Reference a variable.
39    pub fn var(name: impl Into<String>) -> Self {
40        Self::Var(name.into())
41    }
42
43    /// Reference a runtime parameter.
44    pub fn param(name: impl Into<String>) -> Self {
45        Self::Param(name.into())
46    }
47}
48
49impl From<NodeId> for NodeRef {
50    fn from(value: NodeId) -> Self {
51        Self::id(value)
52    }
53}
54
55impl From<Vec<NodeId>> for NodeRef {
56    fn from(value: Vec<NodeId>) -> Self {
57        Self::Ids(value)
58    }
59}
60
61impl<const N: usize> From<[NodeId; N]> for NodeRef {
62    fn from(value: [NodeId; N]) -> Self {
63        Self::Ids(value.to_vec())
64    }
65}
66
67impl From<&str> for NodeRef {
68    fn from(value: &str) -> Self {
69        Self::Var(value.to_string())
70    }
71}
72
73/// A reference to edges.
74#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
75#[serde(rename_all = "snake_case")]
76pub enum EdgeRef {
77    /// All edges.
78    All,
79    /// Concrete edge IDs.
80    Ids(Vec<EdgeId>),
81    /// Named variable.
82    Var(String),
83    /// Runtime parameter containing IDs.
84    Param(String),
85}
86
87impl EdgeRef {
88    /// Reference all edges.
89    pub fn all() -> Self {
90        Self::All
91    }
92
93    /// Reference one edge.
94    pub fn id(id: EdgeId) -> Self {
95        Self::Ids(vec![id])
96    }
97
98    /// Reference multiple edges.
99    pub fn ids(ids: impl IntoIterator<Item = EdgeId>) -> Self {
100        Self::Ids(ids.into_iter().collect())
101    }
102
103    /// Reference a variable.
104    pub fn var(name: impl Into<String>) -> Self {
105        Self::Var(name.into())
106    }
107
108    /// Reference a runtime parameter.
109    pub fn param(name: impl Into<String>) -> Self {
110        Self::Param(name.into())
111    }
112}
113
114impl From<EdgeId> for EdgeRef {
115    fn from(value: EdgeId) -> Self {
116        Self::id(value)
117    }
118}
119
120impl From<Vec<EdgeId>> for EdgeRef {
121    fn from(value: Vec<EdgeId>) -> Self {
122        Self::Ids(value)
123    }
124}
125
126impl<const N: usize> From<[EdgeId; N]> for EdgeRef {
127    fn from(value: [EdgeId; N]) -> Self {
128        Self::Ids(value.to_vec())
129    }
130}