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