codama_nodes/
instruction_account_node.rs

1use crate::{CamelCaseString, Docs, HasName, 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
37impl HasName for InstructionAccountNode {
38    fn name(&self) -> &CamelCaseString {
39        &self.name
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use crate::AccountValueNode;
47
48    #[test]
49    fn new() {
50        let node = InstructionAccountNode::new("my_account", false, true);
51        assert_eq!(node.name, CamelCaseString::new("myAccount"));
52        assert!(!node.is_writable);
53        assert_eq!(node.is_signer, IsAccountSigner::True);
54        assert!(!node.is_optional);
55        assert_eq!(node.docs, Docs::default());
56        assert_eq!(node.default_value, None);
57    }
58
59    #[test]
60    fn direct_instantiation() {
61        let node = InstructionAccountNode {
62            name: "myAccount".into(),
63            is_writable: false,
64            is_signer: IsAccountSigner::Either,
65            is_optional: true,
66            docs: vec!["Hello".to_string()].into(),
67            default_value: Some(AccountValueNode::new("myOtherAccount").into()),
68        };
69        assert_eq!(node.name, CamelCaseString::new("myAccount"));
70        assert!(!node.is_writable);
71        assert_eq!(node.is_signer, IsAccountSigner::Either);
72        assert!(node.is_optional);
73        assert_eq!(*node.docs, vec!["Hello".to_string()]);
74        assert_eq!(
75            node.default_value,
76            Some(InstructionInputValueNode::Account(AccountValueNode::new(
77                "myOtherAccount"
78            )))
79        );
80    }
81
82    #[test]
83    fn to_json() {
84        let node = InstructionAccountNode::new("myAccount", false, true);
85        let json = serde_json::to_string(&node).unwrap();
86        assert_eq!(
87            json,
88            r#"{"kind":"instructionAccountNode","name":"myAccount","isWritable":false,"isSigner":true}"#
89        );
90    }
91
92    #[test]
93    fn from_json() {
94        let json = r#"{"kind":"instructionAccountNode","name":"myAccount","isWritable":false,"isSigner":true}"#;
95        let node: InstructionAccountNode = serde_json::from_str(json).unwrap();
96        assert_eq!(node, InstructionAccountNode::new("myAccount", false, true));
97    }
98}