serde_hkx_features/
alt_map.rs

1use crate::ClassMap;
2use crate::error::{KeyParseSnafu, Result};
3use havok_classes::Classes;
4use indexmap::IndexMap;
5use rayon::prelude::*;
6use snafu::ResultExt as _;
7use std::borrow::Cow;
8
9/// - key: class index(e.g `"1"`)
10/// - value: C++ Class
11pub type ClassMapAlt<'a> = IndexMap<Cow<'a, str>, Classes<'a>>;
12
13/// Converts a `ClassMapAlt` to a `ClassMap` if all keys can be parsed into [`usize`].
14///
15/// # Errors
16/// - If the first key that failed to parse.
17pub fn convert_to_usize_keys(input: ClassMapAlt<'_>) -> Result<ClassMap<'_>> {
18    input
19        .into_par_iter()
20        .map(|(key, value)| {
21            key.parse::<usize>()
22                .map(|parsed_key| (parsed_key, value))
23                .with_context(|_| KeyParseSnafu {
24                    key: key.into_owned(),
25                })
26        })
27        .collect()
28}
29
30/// Converts a `ClassMap` to a `ClassMapAlt` by converting integer keys to strings.
31pub fn convert_to_string_keys(input: ClassMap<'_>) -> ClassMapAlt<'_> {
32    input
33        .into_par_iter()
34        .map(|(key, value)| (Cow::Owned(key.to_string()), value))
35        .collect()
36}