fn construct_mapping()

in amzn-smt-string-transformer/src/transpiler.rs [260:310]


fn construct_mapping(
    sorted_string_list: Vec<&String>,
    char_map: &mut CharMap,
) -> Result<HashMap<String, String>, string_mappings::StringMapError> {
    let mut string_map: HashMap<String, String> = HashMap::new();
    // first, map all the strings where the substrings need to be preserved
    // for the char-->char case it doesn't matter
    // but, for the no_reconstruct case we need to do this first to spot cases
    // where the mapping fails for substring containment
    // in these cases, we bail and map all of them to char-->char
    if !char_map.char_to_char {
        for s in &sorted_string_list {
            if let Some(StringSetProperties::Some {
                len: len_bool,
                ranges: range_set,
                keep_ints: ki,
                keep_substrings: ks,
                keep_prefix_suffix: _kps,
            }) = char_map.string_lit_props.get(&(*s).clone())
            {
                let len_bool = *len_bool;
                let ki = *ki;
                let keep_ranges = !range_set.is_empty();
                if *ks {
                    // this bails out if it's an error; right now behaviour is to try again remapping char_to_char
                    string_mappings::gen_string_keep_substrings(
                        s,
                        char_map,
                        len_bool,
                        ki,
                        keep_ranges,
                    )?;
                }
            }
        }
    }

    // now, iterate over the list and construct the mapping
    // for strings that have already been mapped, this just pulls them back out of the map
    for s in sorted_string_list {
        // foreach char in the string, map it to what that character has already been mapped to
        // or give is a new mapping if it doesn't exist yet in the map
        let mapped_string = if char_map.char_to_char {
            string_mappings::map_string_char_to_char(s, char_map)
        } else {
            string_mappings::map_string_no_reconstruct(s, char_map)
        }?;
        string_map.insert(s.clone(), mapped_string.clone());
    }
    Ok(string_map)
}