codama_nodes/
instruction_account_node.rs

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