codama_nodes/contextual_value_nodes/
conditional_value_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
use crate::{
    AccountValueNode, ArgumentValueNode, InstructionInputValueNode, ResolverValueNode, ValueNode,
};
use codama_nodes_derive::{node, node_union};

#[node]
pub struct ConditionalValueNode {
    // Children.
    pub condition: ConditionNode,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<ValueNode>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub if_true: Box<Option<InstructionInputValueNode>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub if_false: Box<Option<InstructionInputValueNode>>,
}

impl From<ConditionalValueNode> for crate::Node {
    fn from(val: ConditionalValueNode) -> Self {
        crate::Node::ContextualValue(val.into())
    }
}

#[node_union]
pub enum ConditionNode {
    Account(AccountValueNode),
    Argument(ArgumentValueNode),
    Resolver(ResolverValueNode),
}

#[cfg(test)]
mod tests {
    use crate::NumberValueNode;

    use super::*;

    #[test]
    fn direct_instantiation() {
        let node = ConditionalValueNode {
            condition: ArgumentValueNode::new("myArgument").into(),
            value: Some(NumberValueNode::new(42).into()),
            if_true: Box::new(Some(AccountValueNode::new("myOtherAccount").into())),
            if_false: Box::new(None),
        };
        assert_eq!(
            node.condition,
            ConditionNode::Argument(ArgumentValueNode::new("myArgument"))
        );
        assert_eq!(
            node.value,
            Some(ValueNode::Number(NumberValueNode::new(42)))
        );
        assert_eq!(
            *node.if_true,
            Some(InstructionInputValueNode::Account(AccountValueNode::new(
                "myOtherAccount"
            )))
        );
        assert_eq!(*node.if_false, None);
    }

    #[test]
    fn to_json() {
        let node = ConditionalValueNode {
            condition: ArgumentValueNode::new("myArgument").into(),
            value: Some(NumberValueNode::new(42).into()),
            if_true: Box::new(Some(AccountValueNode::new("myOtherAccount").into())),
            if_false: Box::new(None),
        };
        let json = serde_json::to_string(&node).unwrap();
        assert_eq!(
            json,
            r#"{"kind":"conditionalValueNode","condition":{"kind":"argumentValueNode","name":"myArgument"},"value":{"kind":"numberValueNode","number":42},"ifTrue":{"kind":"accountValueNode","name":"myOtherAccount"}}"#
        );
    }

    #[test]
    fn from_json() {
        let json = r#"{"kind":"conditionalValueNode","condition":{"kind":"argumentValueNode","name":"myArgument"},"value":{"kind":"numberValueNode","number":42},"ifTrue":{"kind":"accountValueNode","name":"myOtherAccount"}}"#;
        let node: ConditionalValueNode = serde_json::from_str(json).unwrap();
        assert_eq!(
            node,
            ConditionalValueNode {
                condition: ArgumentValueNode::new("myArgument").into(),
                value: Some(NumberValueNode::new(42u32).into()),
                if_true: Box::new(Some(AccountValueNode::new("myOtherAccount").into())),
                if_false: Box::new(None),
            }
        );
    }
}