codama_nodes/link_nodes/
instruction_link_node.rs

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