snarkvm_console_program/data/literal/cast/
mod.rs1mod boolean;
17mod field;
18mod identifier;
19mod integer;
20mod scalar;
21
22use crate::{Literal, LiteralType};
23use snarkvm_console_network::Network;
24use snarkvm_console_types::{Boolean, integers::Integer, prelude::*};
25
26pub trait Cast<T: Sized = Self> {
28 fn cast(&self) -> Result<T>;
33}
34
35impl<N: Network> Literal<N> {
36 pub fn cast(&self, to_type: LiteralType) -> Result<Self> {
49 match self {
50 Self::Address(address) => cast_group_to_type(address.to_group(), to_type),
51 Self::Boolean(boolean) => cast_boolean_to_type(boolean, to_type),
52 Self::Field(field) => cast_field_to_type(field, to_type),
53 Self::Group(group) => cast_group_to_type(group, to_type),
54 Self::I8(integer) => cast_integer_to_type(integer, to_type),
55 Self::I16(integer) => cast_integer_to_type(integer, to_type),
56 Self::I32(integer) => cast_integer_to_type(integer, to_type),
57 Self::I64(integer) => cast_integer_to_type(integer, to_type),
58 Self::I128(integer) => cast_integer_to_type(integer, to_type),
59 Self::U8(integer) => cast_integer_to_type(integer, to_type),
60 Self::U16(integer) => cast_integer_to_type(integer, to_type),
61 Self::U32(integer) => cast_integer_to_type(integer, to_type),
62 Self::U64(integer) => cast_integer_to_type(integer, to_type),
63 Self::U128(integer) => cast_integer_to_type(integer, to_type),
64 Self::Scalar(scalar) => cast_scalar_to_type(scalar, to_type),
65 Self::Signature(..) => bail!("Cannot cast a signature literal to another type."),
66 Self::String(..) => bail!("Cannot cast a string literal to another type."),
67 Self::Identifier(identifier) => cast_identifier_literal_to_type(identifier, to_type),
68 }
69 }
70}
71
72macro_rules! impl_cast_body {
74 ($type_name:ident, $cast:ident, $input:expr, $to_type:expr) => {
75 match $to_type {
76 LiteralType::Address => Ok(Literal::Address($input.$cast()?)),
77 LiteralType::Boolean => Ok(Literal::Boolean($input.$cast()?)),
78 LiteralType::Field => Ok(Literal::Field($input.$cast()?)),
79 LiteralType::Group => Ok(Literal::Group($input.$cast()?)),
80 LiteralType::I8 => Ok(Literal::I8($input.$cast()?)),
81 LiteralType::I16 => Ok(Literal::I16($input.$cast()?)),
82 LiteralType::I32 => Ok(Literal::I32($input.$cast()?)),
83 LiteralType::I64 => Ok(Literal::I64($input.$cast()?)),
84 LiteralType::I128 => Ok(Literal::I128($input.$cast()?)),
85 LiteralType::U8 => Ok(Literal::U8($input.$cast()?)),
86 LiteralType::U16 => Ok(Literal::U16($input.$cast()?)),
87 LiteralType::U32 => Ok(Literal::U32($input.$cast()?)),
88 LiteralType::U64 => Ok(Literal::U64($input.$cast()?)),
89 LiteralType::U128 => Ok(Literal::U128($input.$cast()?)),
90 LiteralType::Scalar => Ok(Literal::Scalar($input.$cast()?)),
91 LiteralType::Signature => {
92 bail!(concat!("Cannot cast a ", stringify!($type_name), " literal to a signature type."))
93 }
94 LiteralType::String => {
95 bail!(concat!("Cannot cast a ", stringify!($type_name), " literal to a string type."))
96 }
97 LiteralType::Identifier => Ok(Literal::Identifier(Box::new($input.$cast()?))),
98 }
99 };
100}
101
102fn cast_boolean_to_type<N: Network>(input: &Boolean<N>, to_type: LiteralType) -> Result<Literal<N>> {
104 impl_cast_body!(boolean, cast, input, to_type)
105}
106
107fn cast_field_to_type<N: Network>(input: &Field<N>, to_type: LiteralType) -> Result<Literal<N>> {
109 impl_cast_body!(field, cast, input, to_type)
110}
111
112fn cast_group_to_type<N: Network>(input: &Group<N>, to_type: LiteralType) -> Result<Literal<N>> {
114 match to_type {
115 LiteralType::Address => Ok(Literal::Address(Address::new(*input))),
116 LiteralType::Group => Ok(Literal::Group(*input)),
117 _ => cast_field_to_type(&input.to_x_coordinate(), to_type),
118 }
119}
120
121fn cast_integer_to_type<N: Network, I: IntegerType>(
123 input: &integers::Integer<N, I>,
124 to_type: LiteralType,
125) -> Result<Literal<N>>
126where
127 i8: TryFrom<I>,
128 i16: TryFrom<I>,
129 i32: TryFrom<I>,
130 i64: TryFrom<I>,
131 i128: TryFrom<I>,
132 u8: TryFrom<I>,
133 u16: TryFrom<I>,
134 u32: TryFrom<I>,
135 u64: TryFrom<I>,
136 u128: TryFrom<I>,
137{
138 impl_cast_body!(integer, cast, input, to_type)
139}
140
141fn cast_scalar_to_type<N: Network>(input: &Scalar<N>, to_type: LiteralType) -> Result<Literal<N>> {
143 impl_cast_body!(scalar, cast, input, to_type)
144}
145
146fn cast_identifier_literal_to_type<N: Network>(
148 input: &IdentifierLiteral<N>,
149 to_type: LiteralType,
150) -> Result<Literal<N>> {
151 impl_cast_body!(identifier, cast, input, to_type)
152}