codama_nodes/
instruction_account_node.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use crate::{CamelCaseString, Docs, InstructionInputValueNode, IsAccountSigner};
use codama_nodes_derive::node;

#[node]
pub struct InstructionAccountNode {
    // Data.
    pub name: CamelCaseString,
    pub is_writable: bool,
    pub is_signer: IsAccountSigner,
    #[serde(default)]
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub is_optional: bool,
    #[serde(default)]
    #[serde(skip_serializing_if = "Docs::is_empty")]
    pub docs: Docs,

    // Children.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_value: Option<InstructionInputValueNode>,
}

impl InstructionAccountNode {
    pub fn new<T, U>(name: T, is_writable: bool, is_signer: U) -> Self
    where
        T: Into<CamelCaseString>,
        U: Into<IsAccountSigner>,
    {
        Self {
            name: name.into(),
            is_writable,
            is_signer: is_signer.into(),
            is_optional: false,
            docs: Docs::default(),
            default_value: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::AccountValueNode;

    #[test]
    fn new() {
        let node = InstructionAccountNode::new("my_account", false, true);
        assert_eq!(node.name, CamelCaseString::new("myAccount"));
        assert!(!node.is_writable);
        assert_eq!(node.is_signer, IsAccountSigner::True);
        assert!(!node.is_optional);
        assert_eq!(node.docs, Docs::default());
        assert_eq!(node.default_value, None);
    }

    #[test]
    fn direct_instantiation() {
        let node = InstructionAccountNode {
            name: "myAccount".into(),
            is_writable: false,
            is_signer: IsAccountSigner::Either,
            is_optional: true,
            docs: vec!["Hello".to_string()].into(),
            default_value: Some(AccountValueNode::new("myOtherAccount").into()),
        };
        assert_eq!(node.name, CamelCaseString::new("myAccount"));
        assert!(!node.is_writable);
        assert_eq!(node.is_signer, IsAccountSigner::Either);
        assert!(node.is_optional);
        assert_eq!(*node.docs, vec!["Hello".to_string()]);
        assert_eq!(
            node.default_value,
            Some(InstructionInputValueNode::Account(AccountValueNode::new(
                "myOtherAccount"
            )))
        );
    }

    #[test]
    fn to_json() {
        let node = InstructionAccountNode::new("myAccount", false, true);
        let json = serde_json::to_string(&node).unwrap();
        assert_eq!(
            json,
            r#"{"kind":"instructionAccountNode","name":"myAccount","isWritable":false,"isSigner":true}"#
        );
    }

    #[test]
    fn from_json() {
        let json = r#"{"kind":"instructionAccountNode","name":"myAccount","isWritable":false,"isSigner":true}"#;
        let node: InstructionAccountNode = serde_json::from_str(json).unwrap();
        assert_eq!(node, InstructionAccountNode::new("myAccount", false, true));
    }
}