fix(config): serde_json::Value::Object takes a Map instead of HashMap (#1371)

* fix(config): serde_json::Value::Object takes a Map instead of HashMap

* fix: fmt
This commit is contained in:
Lucas Fernandes Nogueira 2021-03-18 12:00:52 -03:00 committed by GitHub
parent 2554fce70e
commit af24034405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -448,10 +448,11 @@ mod build {
quote! { vec![#(#items),*] }
}
/// Create a `HashMap` constructor, mapping keys and values with other `TokenStream`s.
/// Create a map constructor, mapping keys and values with other `TokenStream`s.
///
/// This function is pretty generic because the types of keys AND values get transformed.
fn hashmap_lit<Map, Key, Value, TokenStreamKey, TokenStreamValue, FuncKey, FuncValue>(
fn map_lit<Map, Key, Value, TokenStreamKey, TokenStreamValue, FuncKey, FuncValue>(
map_type: TokenStream,
map: Map,
map_key: FuncKey,
map_value: FuncValue,
@ -475,12 +476,12 @@ mod build {
});
quote! {{
let mut #ident = ::std::collections::HashMap::new();
let mut #ident = #map_type::new();
#(#items)*
#ident
}}
} else {
quote! { ::std::collections::HashMap::new() }
quote! { #map_type::new() }
}
}
@ -523,7 +524,7 @@ mod build {
quote! { #prefix::Array(vec![#(#items),*]) }
}
JsonValue::Object(map) => {
let map = hashmap_lit(map, str_lit, json_value_lit);
let map = map_lit(quote! { ::serde_json::Map }, map, str_lit, json_value_lit);
quote! { #prefix::Object(#map) }
}
}
@ -675,7 +676,14 @@ mod build {
self
.subcommands
.as_ref()
.map(|map| hashmap_lit(map, str_lit, identity))
.map(|map| {
map_lit(
quote! { ::std::collections::HashMap },
map,
str_lit,
identity,
)
})
.as_ref(),
);
@ -721,7 +729,12 @@ mod build {
impl ToTokens for PluginConfig {
fn to_tokens(&self, tokens: &mut TokenStream) {
let config = hashmap_lit(&self.0, str_lit, json_value_lit);
let config = map_lit(
quote! { ::std::collections::HashMap },
&self.0,
str_lit,
json_value_lit,
);
tokens.append_all(quote! { ::tauri::api::config::PluginConfig(#config) })
}
}