Skip to main content

codama_nodes/link_nodes/
instruction_account_link_node.rs

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