Skip to main content

codama_nodes/shared/
instruction_input_value_node.rs

1use crate::{HasKind, InstructionInputValueNode, ValueNode};
2use codama_errors::CodamaError;
3
4/// Bridge from the value-node union into the broader
5/// `InstructionInputValueNode`. The generated `InstructionInputValueNode`
6/// enum (in `generated/contextual_value_nodes/`) auto-derives
7/// `From<Variant>` for each leaf, but `ValueNode` itself (a union of
8/// 15 value leaves) needs this explicit bridge.
9impl From<ValueNode> for InstructionInputValueNode {
10    fn from(value: ValueNode) -> Self {
11        match value {
12            ValueNode::Array(value) => Self::ArrayValue(value),
13            ValueNode::Boolean(value) => Self::BooleanValue(value),
14            ValueNode::Bytes(value) => Self::BytesValue(value),
15            ValueNode::Constant(value) => Self::ConstantValue(value),
16            ValueNode::Enum(value) => Self::EnumValue(value),
17            ValueNode::Injected(value) => Self::InjectedValue(value),
18            ValueNode::Map(value) => Self::MapValue(value),
19            ValueNode::None(value) => Self::NoneValue(value),
20            ValueNode::Number(value) => Self::NumberValue(value),
21            ValueNode::PublicKey(value) => Self::PublicKeyValue(value),
22            ValueNode::Set(value) => Self::SetValue(value),
23            ValueNode::Some(value) => Self::SomeValue(value),
24            ValueNode::String(value) => Self::StringValue(value),
25            ValueNode::Struct(value) => Self::StructValue(value),
26            ValueNode::Tuple(value) => Self::TupleValue(value),
27        }
28    }
29}
30
31/// Inverse bridge: extract the `ValueNode` subset out of
32/// `InstructionInputValueNode`, returning `CodamaError::InvalidNodeConversion`
33/// for the non-value variants (contextual / link members).
34impl TryFrom<InstructionInputValueNode> for ValueNode {
35    type Error = CodamaError;
36
37    fn try_from(value: InstructionInputValueNode) -> Result<Self, Self::Error> {
38        match value {
39            InstructionInputValueNode::ArrayValue(value) => Ok(Self::Array(value)),
40            InstructionInputValueNode::BooleanValue(value) => Ok(Self::Boolean(value)),
41            InstructionInputValueNode::BytesValue(value) => Ok(Self::Bytes(value)),
42            InstructionInputValueNode::ConstantValue(value) => Ok(Self::Constant(value)),
43            InstructionInputValueNode::EnumValue(value) => Ok(Self::Enum(value)),
44            InstructionInputValueNode::InjectedValue(value) => Ok(Self::Injected(value)),
45            InstructionInputValueNode::MapValue(value) => Ok(Self::Map(value)),
46            InstructionInputValueNode::NoneValue(value) => Ok(Self::None(value)),
47            InstructionInputValueNode::NumberValue(value) => Ok(Self::Number(value)),
48            InstructionInputValueNode::PublicKeyValue(value) => Ok(Self::PublicKey(value)),
49            InstructionInputValueNode::SetValue(value) => Ok(Self::Set(value)),
50            InstructionInputValueNode::SomeValue(value) => Ok(Self::Some(value)),
51            InstructionInputValueNode::StringValue(value) => Ok(Self::String(value)),
52            InstructionInputValueNode::StructValue(value) => Ok(Self::Struct(value)),
53            InstructionInputValueNode::TupleValue(value) => Ok(Self::Tuple(value)),
54            _ => Err(CodamaError::InvalidNodeConversion {
55                from: value.kind().to_string(),
56                into: "ValueNode".to_string(),
57            }),
58        }
59    }
60}