substreams_antelope_abigen/
rust.rs

1use heck::ToUpperCamelCase;
2
3pub fn abi_type_to_rust_type(tp: &str) -> Option<&str> {
4    Some(match tp {
5        "bool" => "Bool",
6        "int8" => "Int8",
7        "uint8" => "Uint8",
8        "int16" => "Int16",
9        "uint16" => "Uint16",
10        "int32" => "Int32",
11        "uint32" => "Uint32",
12        "int64" => "Int64",
13        "uint64" => "Uint64",
14        "int128" => "Int128",
15        "uint128" => "Uint128",
16        "varint32" => "VarInt32",
17        "varuint32" => "VarUint32",
18        "float32" => "Float32",
19        "float64" => "Float64",
20        "float128" => "Float128",
21        "time_point" => "TimePoint",
22        "time_point_sec" => "TimePointSec",
23        "block_timestamp_type" => "BlockTimestampType",
24        "name" => "Name",
25        "&[u8]" => "Bytes",
26        "string" => "String",
27        "checksum160" => "Checksum160",
28        "checksum256" => "Checksum256",
29        "checksum512" => "Checksum512",
30        "public_key" => "PublicKey",
31        "signature" => "Signature",
32        "symbol" => "Symbol",
33        "symbol_code" => "SymbolCode",
34        "asset" => "Asset",
35        "extended_asset" => "ExtendedAsset",
36        _ => return None,
37    })
38}
39
40pub fn custom_deserializer(ty: &str, is_array: bool, is_optional: bool) -> Option<String> {
41    let de = match (ty, is_array, is_optional) {
42        ("uint64", false, _) => "substreams_antelope::decoder::str_or_u64".to_string(),
43        ("int64", false, _) => "substreams_antelope::decoder::str_or_i64".to_string(),
44        ("uint64", true, _) => "substreams_antelope::decoder::vec_str_or_u64".to_string(),
45        ("int64", true, _) => "substreams_antelope::decoder::vec_str_or_i64".to_string(),
46        _ => return None,
47    };
48    Some(de)
49}
50
51const RESERVED: [&str; 56] = [
52    "as", "use", "break", "const", "continue", "crate", "else", "if", "enum", "extern", "false", "fn", "for", "if", "impl", "in", "for",
53    "let", "loop", "match", "mod", "move", "mut", "pub", "impl", "ref", "return", "Self", "self", "static", "struct", "super", "trait",
54    "true", "type", "unsafe", "use", "where", "while", "abstract", "alignof", "become", "box", "do", "final", "macro", "offsetof",
55    "override", "priv", "proc", "pure", "sizeof", "typeof", "unsized", "virtual", "yield",
56];
57
58pub fn is_reserved(id: &str) -> bool {
59    RESERVED.contains(&id)
60}
61
62pub fn rust_type(ty: &str) -> String {
63    match abi_type_to_rust_type(ty) {
64        Some(ty) => ty.to_string(),
65        _ => ty.to_string().to_upper_camel_case(),
66    }
67}