codama_nodes/discriminator_nodes/
constant_discriminator_node.rs1use crate::{ConstantDiscriminatorNode, ConstantValueNode};
2
3impl ConstantDiscriminatorNode {
4 pub fn new<T>(constant: T, offset: u64) -> Self
5 where
6 T: Into<ConstantValueNode>,
7 {
8 Self {
9 constant: constant.into(),
10 offset,
11 }
12 }
13}
14
15#[cfg(test)]
16mod tests {
17 use crate::{
18 Base16, ConstantDiscriminatorNode, ConstantValueNode, NumberTypeNode, NumberValueNode, U32,
19 };
20
21 #[test]
22 fn new() {
23 let node = ConstantDiscriminatorNode::new(
24 ConstantValueNode::new(NumberTypeNode::le(U32), NumberValueNode::new(42u32)),
25 0,
26 );
27 assert_eq!(
28 node.constant,
29 ConstantValueNode::new(NumberTypeNode::le(U32), NumberValueNode::new(42u32))
30 );
31 assert_eq!(node.offset, 0);
32 }
33
34 #[test]
35 fn to_json() {
36 let node =
37 ConstantDiscriminatorNode::new(ConstantValueNode::bytes(Base16, "deadb0d1e5"), 0);
38 let json = serde_json::to_string(&node).unwrap();
39 assert_eq!(
40 json,
41 r#"{"kind":"constantDiscriminatorNode","offset":0,"constant":{"kind":"constantValueNode","type":{"kind":"bytesTypeNode"},"value":{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}}}"#
42 );
43 }
44
45 #[test]
46 fn from_json() {
47 let json = r#"{"kind":"constantDiscriminatorNode","offset":0,"constant":{"kind":"constantValueNode","type":{"kind":"bytesTypeNode"},"value":{"kind":"bytesValueNode","data":"deadb0d1e5","encoding":"base16"}}}"#;
48 let node: ConstantDiscriminatorNode = serde_json::from_str(json).unwrap();
49 assert_eq!(
50 node,
51 ConstantDiscriminatorNode::new(ConstantValueNode::bytes(Base16, "deadb0d1e5"), 0)
52 );
53 }
54}