scrypto_bindgen/
types.rs

1use radix_common::prelude::*;
2use radix_rust::prelude::{hashmap, HashMap};
3
4// This structure describes function argument and return types replacements.
5#[derive(Debug)]
6pub struct FunctionSignatureReplacementsInput {
7    pub blueprint_name: String, // Name of the blueprint, which shall have replaced types
8    pub func_name: String,      // Name of the function, which shall have replaced types
9    pub replacement_map: FunctionSignatureReplacements,
10}
11
12pub type FunctionSignaturesReplacementMap = HashMap<String, FunctionSignatureReplacements>;
13pub type BlueprintFunctionSignaturesReplacementMap =
14    HashMap<String, FunctionSignaturesReplacementMap>;
15
16#[derive(Debug, Clone)]
17pub struct FunctionSignatureReplacements {
18    pub arg: HashMap<usize, String>, // Map of argument indexes and their new type names
19    pub output: Option<String>,      // Name of the new return type
20}
21
22pub fn prepare_replacement_map(
23    replacement_vec: &[FunctionSignatureReplacementsInput],
24) -> BlueprintFunctionSignaturesReplacementMap {
25    let mut blueprint_replacement_map: BlueprintFunctionSignaturesReplacementMap = hashmap!();
26
27    for item in replacement_vec {
28        if blueprint_replacement_map.contains_key(&item.blueprint_name) {
29            let function_map = blueprint_replacement_map
30                .get_mut(&item.blueprint_name)
31                .unwrap();
32            function_map.insert(item.func_name.clone(), item.replacement_map.clone());
33        } else {
34            let mut function_map = hashmap!();
35            function_map.insert(item.func_name.clone(), item.replacement_map.clone());
36            blueprint_replacement_map.insert(item.blueprint_name.clone(), function_map);
37        };
38    }
39    blueprint_replacement_map
40}
41
42impl FromStr for FunctionSignatureReplacementsInput {
43    type Err = String;
44
45    // Get FunctionSignatureReplacementsInput from a string.
46    // Syntax:
47    //   blueprint_name=<blueprint_name>;func_name=<function_name>;<argument_index>:<type_replacement>;r:<type_replacement>
48    // Example:
49    //   - replace first argument and return type to FungibleBucket of the function 'new'
50    //      blueprint_name=Faucet;func_name=new;0=FungibleBucket;r=FungibleBucket
51    fn from_str(input: &str) -> Result<Self, Self::Err> {
52        let mut items = input.split(";");
53
54        let mut blueprint_name_items = items
55            .next()
56            .ok_or("Cannot determine blueprint name")?
57            .split("=");
58
59        let blueprint_name = match blueprint_name_items.next() {
60            Some("blueprint_name") => blueprint_name_items.next(),
61            Some(_) => None,
62            None => None,
63        }
64        .ok_or("blueprint_name not found")?
65        .to_string();
66
67        let mut func_name_items = items
68            .next()
69            .ok_or("Cannot determine function name")?
70            .split("=");
71
72        let func_name = match func_name_items.next() {
73            Some("func_name") => func_name_items.next(),
74            Some(_) => None,
75            None => None,
76        }
77        .ok_or("func_name not found")?
78        .to_string();
79
80        let mut arg = hashmap!();
81        let mut output = None;
82
83        for item in items {
84            let mut s = item.split("=");
85            match s.next() {
86                Some("r") => {
87                    let ty = s.next().ok_or("Return type not available".to_string())?;
88                    output = Some(ty.to_string());
89                }
90                Some(val) => {
91                    let idx: usize = val.parse().map_err(|_| "Failed to parse integer")?;
92                    let ty = s.next().ok_or("Arg type not available")?;
93                    arg.insert(idx, ty.to_string());
94                }
95                None => Err("Argument index or return type not available")?,
96            }
97        }
98
99        Ok(Self {
100            blueprint_name,
101            func_name,
102            replacement_map: FunctionSignatureReplacements { arg, output },
103        })
104    }
105}