codama_nodes/value_nodes/
constant_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
use crate::{BytesEncoding, BytesTypeNode, BytesValueNode, TypeNode, ValueNode};
use codama_nodes_derive::node;

#[node]
pub struct ConstantValueNode {
    // Children.
    pub r#type: Box<TypeNode>,
    pub value: Box<ValueNode>,
}

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

impl ConstantValueNode {
    pub fn new<T, U>(r#type: T, value: U) -> Self
    where
        T: Into<TypeNode>,
        U: Into<ValueNode>,
    {
        Self {
            r#type: Box::new(r#type.into()),
            value: Box::new(value.into()),
        }
    }

    pub fn bytes<T>(encoding: BytesEncoding, data: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            r#type: Box::new(BytesTypeNode::new().into()),
            value: Box::new(BytesValueNode::new(encoding, data).into()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Base16, NumberTypeNode, NumberValueNode, U64};

    #[test]
    fn new() {
        let node = ConstantValueNode::new(NumberTypeNode::le(U64), NumberValueNode::new(42u64));
        assert_eq!(*node.r#type, TypeNode::Number(NumberTypeNode::le(U64)));
        assert_eq!(*node.value, ValueNode::Number(NumberValueNode::new(42u64)));
    }

    #[test]
    fn bytes() {
        let node = ConstantValueNode::bytes(Base16, "deadb0d1e5");
        assert_eq!(*node.r#type, TypeNode::Bytes(BytesTypeNode::new()));
        assert_eq!(
            *node.value,
            ValueNode::Bytes(BytesValueNode::base16("deadb0d1e5"))
        );
    }

    #[test]
    fn to_json() {
        let node = ConstantValueNode::bytes(Base16, "deadb0d1e5");
        let json = serde_json::to_string(&node).unwrap();
        assert_eq!(
            json,
            r#"{"kind":"constantValueNode","type":{"kind":"bytesTypeNode"},"value":{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}}"#
        );
    }

    #[test]
    fn from_json() {
        let json = r#"{"kind":"constantValueNode","type":{"kind":"bytesTypeNode"},"value":{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}}"#;
        let node: ConstantValueNode = serde_json::from_str(json).unwrap();
        assert_eq!(node, ConstantValueNode::bytes(Base16, "deadb0d1e5"));
    }
}