Skip to main content

codama_nodes/link_nodes/
instruction_link_node.rs

1use crate::{CamelCaseString, InstructionLinkNode, ProgramLinkNode};
2
3impl InstructionLinkNode {
4    pub fn new<T>(name: T) -> Self
5    where
6        T: Into<CamelCaseString>,
7    {
8        Self {
9            name: name.into(),
10            program: None,
11        }
12    }
13
14    pub fn new_from_program<T>(name: T, program: ProgramLinkNode) -> Self
15    where
16        T: Into<CamelCaseString>,
17    {
18        Self {
19            name: name.into(),
20            program: Some(program),
21        }
22    }
23}
24
25impl From<String> for InstructionLinkNode {
26    fn from(name: String) -> Self {
27        Self::new(name)
28    }
29}
30
31impl From<&str> for InstructionLinkNode {
32    fn from(name: &str) -> Self {
33        Self::new(name)
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn new() {
43        let node = InstructionLinkNode::new("my_instruction");
44        assert_eq!(node.name, CamelCaseString::new("myInstruction"));
45    }
46
47    #[test]
48    fn new_from_program() {
49        let node = InstructionLinkNode::new_from_program(
50            "my_instruction",
51            ProgramLinkNode::new("my_program"),
52        );
53        assert_eq!(node.name, CamelCaseString::new("myInstruction"));
54        assert_eq!(node.program, Some(ProgramLinkNode::new("myProgram")));
55    }
56
57    #[test]
58    fn from_string() {
59        let node: InstructionLinkNode = String::from("my_instruction").into();
60        assert_eq!(node.name, CamelCaseString::new("myInstruction"));
61        assert_eq!(node.program, None);
62    }
63
64    #[test]
65    fn from_str() {
66        let node: InstructionLinkNode = "my_instruction".into();
67        assert_eq!(node.name, CamelCaseString::new("myInstruction"));
68        assert_eq!(node.program, None);
69    }
70
71    #[test]
72    fn to_json() {
73        let node = InstructionLinkNode::new("myInstruction");
74        let json = serde_json::to_string(&node).unwrap();
75        assert_eq!(
76            json,
77            r#"{"kind":"instructionLinkNode","name":"myInstruction"}"#
78        );
79    }
80
81    #[test]
82    fn from_json() {
83        let json = r#"{"kind":"instructionLinkNode","name":"myInstruction"}"#;
84        let node: InstructionLinkNode = serde_json::from_str(json).unwrap();
85        assert_eq!(node, InstructionLinkNode::new("myInstruction"));
86    }
87}