codama_nodes/link_nodes/
instruction_account_link_node.rs

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