Skip to main content

nexus_sdk/move_bindings/extensions/
runtime_vertex.rs

1//! Constructors and display helpers for generated runtime vertices.
2//!
3//! [`crate::move_bindings::interface::graph::RuntimeVertex`] is the Move representation of a DAG
4//! vertex during execution. This module gives Rust callers stable constructors for plain and
5//! iterator vertices, plus conversions to the generated
6//! [`crate::move_bindings::interface::graph::Vertex`] and
7//! [`crate::move_bindings::move_std::type_name::TypeName`] shapes used in BCS.
8
9use crate::move_bindings::{
10    interface::graph::{RuntimeVertex, Vertex},
11    move_std::type_name::TypeName,
12};
13
14impl RuntimeVertex {
15    pub fn plain(vertex: &str) -> Self {
16        Self::Plain {
17            vertex: vertex_from_str(vertex),
18        }
19    }
20
21    pub fn with_iterator(vertex: &str, iteration: u64, out_of: u64) -> Self {
22        Self::WithIterator {
23            vertex: vertex_from_str(vertex),
24            iteration,
25            out_of,
26        }
27    }
28
29    pub fn vertex(&self) -> &Vertex {
30        match self {
31            Self::Plain { vertex } | Self::WithIterator { vertex, .. } => vertex,
32        }
33    }
34
35    pub fn vertex_name(&self) -> &str {
36        self.vertex().as_str()
37    }
38
39    pub fn name(&self) -> TypeName {
40        TypeName::from(self.vertex_name())
41    }
42}
43
44impl std::fmt::Display for RuntimeVertex {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        match self {
47            Self::Plain { vertex } => write!(f, "Plain({})", vertex.name.as_str()),
48            Self::WithIterator {
49                vertex,
50                iteration,
51                out_of,
52            } => write!(
53                f,
54                "WithIterator({}:{}:{})",
55                vertex.name.as_str(),
56                iteration,
57                out_of
58            ),
59        }
60    }
61}
62
63fn vertex_from_str(vertex: &str) -> Vertex {
64    Vertex::new(vertex)
65}
66
67impl From<TypeName> for Vertex {
68    fn from(value: TypeName) -> Self {
69        Self { name: value.name }
70    }
71}
72
73impl From<&TypeName> for Vertex {
74    fn from(value: &TypeName) -> Self {
75        Self {
76            name: value.name.clone(),
77        }
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn runtime_vertex_helpers_cover_plain_and_iterator_variants() {
87        let plain = RuntimeVertex::plain("source");
88        assert_eq!(plain.name(), TypeName::new("source"));
89        assert_eq!(plain.to_string(), "Plain(source)");
90
91        let with_iterator = RuntimeVertex::with_iterator("worker", 2, 5);
92        assert_eq!(with_iterator.name(), TypeName::new("worker"));
93        assert_eq!(with_iterator.to_string(), "WithIterator(worker:2:5)");
94    }
95
96    #[test]
97    fn type_name_conversions_create_move_vertices() {
98        let owned_name = TypeName::new("owned");
99        let borrowed_name = TypeName::new("borrowed");
100
101        let owned: Vertex = owned_name.into();
102        let borrowed: Vertex = (&borrowed_name).into();
103
104        assert_eq!(owned.name.as_str(), "owned");
105        assert_eq!(borrowed.name.as_str(), "borrowed");
106    }
107}