sails_sol_gen/
sol_conversion.rs1use alloc::string::String;
2use sails_idl_parser_v2::ast::{PrimitiveType, TypeDecl};
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum ConversionError {
7 #[error("Type is not supported")]
8 UnsupportedType,
9}
10
11pub trait TypeDeclExt {
12 fn get_ty(&self) -> Result<String, ConversionError>;
13
14 fn get_mem_location(&self) -> Option<String>;
15}
16
17impl TypeDeclExt for TypeDecl {
18 fn get_ty(&self) -> Result<String, ConversionError> {
19 match self {
20 TypeDecl::Primitive(ty) => ty.get_ty(),
21 _ => Err(ConversionError::UnsupportedType),
22 }
23 }
24
25 fn get_mem_location(&self) -> Option<String> {
26 match self {
27 TypeDecl::Primitive(ty) => ty.get_mem_location(),
28 TypeDecl::Array { .. } => Some("calldata".into()),
29 TypeDecl::Slice { .. } => Some("calldata".into()),
30 _ => None,
31 }
32 }
33}
34
35impl TypeDeclExt for PrimitiveType {
36 fn get_ty(&self) -> Result<String, ConversionError> {
37 Ok(match self {
38 Self::Bool => "bool".into(),
39 Self::U8 => "uint8".into(),
40 Self::U16 => "uint16".into(),
41 Self::U32 => "uint32".into(),
42 Self::U64 => "uint64".into(),
43 Self::U128 => "uint128".into(),
44 Self::U256 => "uint256".into(),
45 Self::I8 => "int8".into(),
46 Self::I16 => "int16".into(),
47 Self::I32 => "int32".into(),
48 Self::I64 => "int64".into(),
49 Self::I128 => "int128".into(),
50 Self::String => "string".into(),
51 Self::ActorId => "address".into(),
52 Self::H256 | Self::CodeId | Self::MessageId => "bytes32".into(),
53 Self::H160 => "bytes20".into(),
54 _ => return Err(ConversionError::UnsupportedType),
55 })
56 }
57
58 fn get_mem_location(&self) -> Option<String> {
59 match self {
60 Self::String => Some("calldata".into()),
61 _ => None,
62 }
63 }
64}