Skip to main content

odra_casper_types/
ty.rs

1use alloc::boxed::Box;
2use casper_types::{CLType, CLTyped};
3use odra_types::Type;
4
5/// A type which can be described as a [Type].
6pub trait Typed {
7    fn ty() -> Type;
8}
9
10impl<T: CLTyped> Typed for [T] {
11    fn ty() -> Type {
12        Type::Slice(Box::new(cl_type_to_type(T::cl_type())))
13    }
14}
15
16impl<T: CLTyped> Typed for T {
17    fn ty() -> Type {
18        cl_type_to_type(T::cl_type())
19    }
20}
21
22fn cl_type_to_type(ty: CLType) -> Type {
23    match ty {
24        CLType::Bool => Type::Bool,
25        CLType::I32 => Type::I32,
26        CLType::I64 => Type::I64,
27        CLType::U8 => Type::U8,
28        CLType::U32 => Type::U32,
29        CLType::U64 => Type::U64,
30        CLType::U128 => Type::U128,
31        CLType::U256 => Type::U256,
32        CLType::U512 => Type::U512,
33        CLType::Unit => Type::Unit,
34        CLType::String => Type::String,
35        CLType::Option(ty) => Type::Option(boxed_cl_type_to_boxed_type(ty)),
36        CLType::List(ty) => Type::Vec(boxed_cl_type_to_boxed_type(ty)),
37        CLType::Result { ok, err } => Type::Map {
38            key: boxed_cl_type_to_boxed_type(ok),
39            value: boxed_cl_type_to_boxed_type(err)
40        },
41        CLType::Map { key, value } => Type::Map {
42            key: boxed_cl_type_to_boxed_type(key),
43            value: boxed_cl_type_to_boxed_type(value)
44        },
45        CLType::Tuple1(types) => Type::Tuple1(types.map(boxed_cl_type_to_boxed_type)),
46        CLType::Tuple2(types) => Type::Tuple2(types.map(boxed_cl_type_to_boxed_type)),
47        CLType::Tuple3(types) => Type::Tuple3(types.map(boxed_cl_type_to_boxed_type)),
48        CLType::Any => Type::Any,
49        CLType::Key => Type::Address,
50        CLType::ByteArray(b) => Type::ByteArray(b),
51        CLType::PublicKey => Type::PublicKey,
52        _ => panic!("Unsupported type {:?}", ty)
53    }
54}
55
56#[allow(clippy::boxed_local)]
57fn boxed_cl_type_to_boxed_type(ty: Box<CLType>) -> Box<Type> {
58    Box::new(cl_type_to_type(*ty))
59}