Skip to main content

codama_nodes/pda_seed_nodes/
constant_pda_seed_node.rs

1use crate::{ConstantPdaSeedNode, ConstantPdaSeedValue, TypeNode, ValueNode};
2
3impl ConstantPdaSeedNode {
4    pub fn new<T, U>(r#type: T, value: U) -> Self
5    where
6        T: Into<TypeNode>,
7        U: Into<ConstantPdaSeedValue>,
8    {
9        Self {
10            r#type: Box::new(r#type.into()),
11            value: Box::new(value.into()),
12        }
13    }
14}
15
16/// Bridge from the broader value-node union into a `constantPdaSeedNode`
17/// value. The spec union `constantPdaSeedValue` is `programIdValueNode |
18/// valueNode`, so every `ValueNode` cleanly maps to its same-named
19/// `ConstantPdaSeedValue` variant.
20impl From<ValueNode> for ConstantPdaSeedValue {
21    fn from(value: ValueNode) -> Self {
22        match value {
23            ValueNode::Array(n) => Self::Array(n),
24            ValueNode::Boolean(n) => Self::Boolean(n),
25            ValueNode::Bytes(n) => Self::Bytes(n),
26            ValueNode::Constant(n) => Self::Constant(n),
27            ValueNode::Enum(n) => Self::Enum(n),
28            ValueNode::Injected(n) => Self::Injected(n),
29            ValueNode::Map(n) => Self::Map(n),
30            ValueNode::None(n) => Self::None(n),
31            ValueNode::Number(n) => Self::Number(n),
32            ValueNode::PublicKey(n) => Self::PublicKey(n),
33            ValueNode::Set(n) => Self::Set(n),
34            ValueNode::Some(n) => Self::Some(n),
35            ValueNode::String(n) => Self::String(n),
36            ValueNode::Struct(n) => Self::Struct(n),
37            ValueNode::Tuple(n) => Self::Tuple(n),
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::{NumberTypeNode, NumberValueNode, U64};
46
47    #[test]
48    fn new() {
49        let node = ConstantPdaSeedNode::new(NumberTypeNode::le(U64), NumberValueNode::new(42u64));
50        assert_eq!(*node.r#type, TypeNode::Number(NumberTypeNode::le(U64)));
51        assert_eq!(
52            *node.value,
53            ConstantPdaSeedValue::Number(NumberValueNode::new(42u64))
54        );
55    }
56
57    #[test]
58    fn to_json() {
59        let node = ConstantPdaSeedNode::new(NumberTypeNode::le(U64), NumberValueNode::new(42u64));
60        let json = serde_json::to_string(&node).unwrap();
61        assert_eq!(
62            json,
63            r#"{"kind":"constantPdaSeedNode","type":{"kind":"numberTypeNode","format":"u64","endian":"le"},"value":{"kind":"numberValueNode","number":42}}"#
64        );
65    }
66
67    #[test]
68    fn from_json() {
69        let json = r#"{"kind":"constantPdaSeedNode","type":{"kind":"numberTypeNode","format":"u64","endian":"le"},"value":{"kind":"numberValueNode","number":42}}"#;
70        let node: ConstantPdaSeedNode = serde_json::from_str(json).unwrap();
71        assert_eq!(
72            node,
73            ConstantPdaSeedNode::new(NumberTypeNode::le(U64), NumberValueNode::new(42u64))
74        );
75    }
76}