codama_nodes/
instruction_byte_delta_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::{AccountLinkNode, ArgumentValueNode, NumberValueNode, ResolverValueNode};
use codama_nodes_derive::{node, node_union};

#[node]
pub struct InstructionByteDeltaNode {
    // Data.
    pub with_header: bool,
    #[serde(default)]
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub subtract: bool,

    // Children.
    pub value: InstructionByteDeltaNodeValue,
}

impl InstructionByteDeltaNode {
    pub fn new<T>(value: T, with_header: bool) -> Self
    where
        T: Into<InstructionByteDeltaNodeValue>,
    {
        Self {
            value: value.into(),
            with_header,
            subtract: false,
        }
    }

    pub fn minus<T>(value: T, with_header: bool) -> Self
    where
        T: Into<InstructionByteDeltaNodeValue>,
    {
        Self {
            value: value.into(),
            with_header,
            subtract: true,
        }
    }
}

#[node_union]
pub enum InstructionByteDeltaNodeValue {
    Account(AccountLinkNode),
    Argument(ArgumentValueNode),
    Number(NumberValueNode),
    Resolver(ResolverValueNode),
}

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

    #[test]
    fn new() {
        let node = InstructionByteDeltaNode::new(ArgumentValueNode::new("myArgument"), true);
        assert_eq!(
            node.value,
            InstructionByteDeltaNodeValue::Argument(ArgumentValueNode::new("myArgument"))
        );
        assert!(node.with_header);
        assert!(!node.subtract);
    }

    #[test]
    fn minus() {
        let node = InstructionByteDeltaNode::minus(NumberValueNode::new(42), true);
        assert_eq!(
            node.value,
            InstructionByteDeltaNodeValue::Number(NumberValueNode::new(42))
        );
        assert!(node.with_header);
        assert!(node.subtract);
    }

    #[test]
    fn to_json() {
        let node = InstructionByteDeltaNode::new(ArgumentValueNode::new("myArgument"), true);
        let json = serde_json::to_string(&node).unwrap();
        assert_eq!(
            json,
            r#"{"kind":"instructionByteDeltaNode","withHeader":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#
        );
    }

    #[test]
    fn from_json() {
        let json = r#"{"kind":"instructionByteDeltaNode","withHeader":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#;
        let node: InstructionByteDeltaNode = serde_json::from_str(json).unwrap();
        assert_eq!(
            node,
            InstructionByteDeltaNode::new(ArgumentValueNode::new("myArgument"), true)
        );
    }

    #[test]
    fn to_json_minus() {
        let node = InstructionByteDeltaNode::minus(ArgumentValueNode::new("myArgument"), true);
        let json = serde_json::to_string(&node).unwrap();
        assert_eq!(
            json,
            r#"{"kind":"instructionByteDeltaNode","withHeader":true,"subtract":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#
        );
    }

    #[test]
    fn from_json_minus() {
        let json = r#"{"kind":"instructionByteDeltaNode","withHeader":true,"subtract":true,"value":{"kind":"argumentValueNode","name":"myArgument"}}"#;
        let node: InstructionByteDeltaNode = serde_json::from_str(json).unwrap();
        assert_eq!(
            node,
            InstructionByteDeltaNode::minus(ArgumentValueNode::new("myArgument"), true)
        );
    }
}