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