Skip to main content

codama_nodes/
instruction_account_node.rs

1use crate::{CamelCaseString, Docs, InstructionAccountNode, IsSigner};
2
3impl InstructionAccountNode {
4    pub fn new<T, U>(name: T, is_writable: bool, is_signer: U) -> Self
5    where
6        T: Into<CamelCaseString>,
7        U: Into<IsSigner>,
8    {
9        Self {
10            name: name.into(),
11            is_writable,
12            is_signer: is_signer.into(),
13            is_optional: None,
14            docs: Docs::default(),
15            default_value: Box::new(None),
16            account_link: None,
17            display: None,
18        }
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25    use crate::{AccountValueNode, InstructionInputValueNode};
26
27    #[test]
28    fn new() {
29        let node = InstructionAccountNode::new("my_account", false, true);
30        assert_eq!(node.name, CamelCaseString::new("myAccount"));
31        assert!(!node.is_writable);
32        assert_eq!(node.is_signer, IsSigner::True);
33        assert_eq!(node.is_optional, None);
34        assert_eq!(node.docs, Docs::default());
35        assert_eq!(*node.default_value, None);
36    }
37
38    #[test]
39    fn direct_instantiation() {
40        let node = InstructionAccountNode {
41            name: "myAccount".into(),
42            is_writable: false,
43            is_signer: IsSigner::Either,
44            is_optional: Some(true),
45            docs: vec!["Hello".to_string()].into(),
46            default_value: Box::new(Some(AccountValueNode::new("myOtherAccount").into())),
47            account_link: None,
48            display: None,
49        };
50        assert_eq!(node.name, CamelCaseString::new("myAccount"));
51        assert!(!node.is_writable);
52        assert_eq!(node.is_signer, IsSigner::Either);
53        assert_eq!(node.is_optional, Some(true));
54        assert_eq!(*node.docs, vec!["Hello".to_string()]);
55        assert_eq!(
56            *node.default_value,
57            Some(InstructionInputValueNode::AccountValue(
58                AccountValueNode::new("myOtherAccount")
59            ))
60        );
61    }
62
63    #[test]
64    fn to_json() {
65        let node = InstructionAccountNode::new("myAccount", false, true);
66        let json = serde_json::to_string(&node).unwrap();
67        assert_eq!(
68            json,
69            r#"{"kind":"instructionAccountNode","name":"myAccount","isWritable":false,"isSigner":true}"#
70        );
71    }
72
73    #[test]
74    fn from_json() {
75        let json = r#"{"kind":"instructionAccountNode","name":"myAccount","isWritable":false,"isSigner":true}"#;
76        let node: InstructionAccountNode = serde_json::from_str(json).unwrap();
77        assert_eq!(node, InstructionAccountNode::new("myAccount", false, true));
78    }
79}