codama_nodes/link_nodes/
instruction_link_node.rs1use crate::{CamelCaseString, ProgramLinkNode};
2use codama_nodes_derive::node;
3
4#[node]
5pub struct InstructionLinkNode {
6 pub name: CamelCaseString,
8
9 #[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
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn new() {
60 let node = InstructionLinkNode::new("my_instruction");
61 assert_eq!(node.name, CamelCaseString::new("myInstruction"));
62 }
63
64 #[test]
65 fn new_from_program() {
66 let node = InstructionLinkNode::new_from_program(
67 "my_instruction",
68 ProgramLinkNode::new("my_program"),
69 );
70 assert_eq!(node.name, CamelCaseString::new("myInstruction"));
71 assert_eq!(node.program, Some(ProgramLinkNode::new("myProgram")));
72 }
73
74 #[test]
75 fn from_string() {
76 let node: InstructionLinkNode = String::from("my_instruction").into();
77 assert_eq!(node.name, CamelCaseString::new("myInstruction"));
78 assert_eq!(node.program, None);
79 }
80
81 #[test]
82 fn from_str() {
83 let node: InstructionLinkNode = "my_instruction".into();
84 assert_eq!(node.name, CamelCaseString::new("myInstruction"));
85 assert_eq!(node.program, None);
86 }
87
88 #[test]
89 fn to_json() {
90 let node = InstructionLinkNode::new("myInstruction");
91 let json = serde_json::to_string(&node).unwrap();
92 assert_eq!(
93 json,
94 r#"{"kind":"instructionLinkNode","name":"myInstruction"}"#
95 );
96 }
97
98 #[test]
99 fn from_json() {
100 let json = r#"{"kind":"instructionLinkNode","name":"myInstruction"}"#;
101 let node: InstructionLinkNode = serde_json::from_str(json).unwrap();
102 assert_eq!(node, InstructionLinkNode::new("myInstruction"));
103 }
104}