codama_nodes/
instruction_byte_delta_node.rs

1use crate::{AccountLinkNode, ArgumentValueNode, NumberValueNode, ResolverValueNode};
2use codama_nodes_derive::{node, node_union};
3
4#[node]
5pub struct InstructionByteDeltaNode {
6    // Data.
7    pub with_header: bool,
8    #[serde(default, skip_serializing_if = "crate::is_default")]
9    pub subtract: bool,
10
11    // Children.
12    pub value: InstructionByteDeltaNodeValue,
13}
14
15impl InstructionByteDeltaNode {
16    pub fn new<T>(value: T, with_header: bool) -> Self
17    where
18        T: Into<InstructionByteDeltaNodeValue>,
19    {
20        Self {
21            value: value.into(),
22            with_header,
23            subtract: false,
24        }
25    }
26
27    pub fn minus<T>(value: T, with_header: bool) -> Self
28    where
29        T: Into<InstructionByteDeltaNodeValue>,
30    {
31        Self {
32            value: value.into(),
33            with_header,
34            subtract: true,
35        }
36    }
37}
38
39#[node_union]
40pub enum InstructionByteDeltaNodeValue {
41    Account(AccountLinkNode),
42    Argument(ArgumentValueNode),
43    Number(NumberValueNode),
44    Resolver(ResolverValueNode),
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn new() {
53        let node = InstructionByteDeltaNode::new(ArgumentValueNode::new("myArgument"), true);
54        assert_eq!(
55            node.value,
56            InstructionByteDeltaNodeValue::Argument(ArgumentValueNode::new("myArgument"))
57        );
58        assert!(node.with_header);
59        assert!(!node.subtract);
60    }
61
62    #[test]
63    fn minus() {
64        let node = InstructionByteDeltaNode::minus(NumberValueNode::new(42), true);
65        assert_eq!(
66            node.value,
67            InstructionByteDeltaNodeValue::Number(NumberValueNode::new(42))
68        );
69        assert!(node.with_header);
70        assert!(node.subtract);
71    }
72
73    #[test]
74    fn to_json() {
75        let node = InstructionByteDeltaNode::new(ArgumentValueNode::new("myArgument"), true);
76        let json = serde_json::to_string(&node).unwrap();
77        assert_eq!(
78            json,
79            r#"{"kind":"instructionByteDeltaNode","withHeader":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#
80        );
81    }
82
83    #[test]
84    fn from_json() {
85        let json = r#"{"kind":"instructionByteDeltaNode","withHeader":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#;
86        let node: InstructionByteDeltaNode = serde_json::from_str(json).unwrap();
87        assert_eq!(
88            node,
89            InstructionByteDeltaNode::new(ArgumentValueNode::new("myArgument"), true)
90        );
91    }
92
93    #[test]
94    fn to_json_minus() {
95        let node = InstructionByteDeltaNode::minus(ArgumentValueNode::new("myArgument"), true);
96        let json = serde_json::to_string(&node).unwrap();
97        assert_eq!(
98            json,
99            r#"{"kind":"instructionByteDeltaNode","withHeader":true,"subtract":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#
100        );
101    }
102
103    #[test]
104    fn from_json_minus() {
105        let json = r#"{"kind":"instructionByteDeltaNode","withHeader":true,"subtract":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#;
106        let node: InstructionByteDeltaNode = serde_json::from_str(json).unwrap();
107        assert_eq!(
108            node,
109            InstructionByteDeltaNode::minus(ArgumentValueNode::new("myArgument"), true)
110        );
111    }
112}