Skip to content

Commit cb9bf78

Browse files
authored
support custom map types (#1011)
1 parent 2727eb2 commit cb9bf78

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

progenitor-impl/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub struct GenerationSettings {
6767
post_hook_async: Option<TokenStream>,
6868
extra_derives: Vec<String>,
6969

70+
map_type: Option<String>,
7071
unknown_crates: UnknownPolicy,
7172
crates: BTreeMap<String, CrateSpec>,
7273

@@ -228,6 +229,18 @@ impl GenerationSettings {
228229
);
229230
self
230231
}
232+
233+
/// Set the type used for key-value maps. Common examples:
234+
/// - [`std::collections::HashMap`] - **Default**
235+
/// - [`std::collections::BTreeMap`]
236+
/// - [`indexmap::IndexMap`]
237+
///
238+
/// The requiremnets for a map type can be found in the
239+
/// [typify::TypeSpaceSettings::with_map_type] documentation.
240+
pub fn with_map_type<MT: ToString>(&mut self, map_type: MT) -> &mut Self {
241+
self.map_type = Some(map_type.to_string());
242+
self
243+
}
231244
}
232245

233246
impl Default for Generator {
@@ -278,6 +291,11 @@ impl Generator {
278291
type_settings.with_conversion(schema.clone(), type_name, impls.iter().cloned());
279292
});
280293

294+
// Set the map type if specified.
295+
if let Some(map_type) = &settings.map_type {
296+
type_settings.with_map_type(map_type.clone());
297+
}
298+
281299
Self {
282300
type_space: TypeSpace::new(&type_settings),
283301
settings: settings.clone(),

progenitor-macro/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ struct MacroSettings {
139139
post_hook: Option<ParseWrapper<ClosureOrPath>>,
140140
post_hook_async: Option<ParseWrapper<ClosureOrPath>>,
141141

142+
map_type: Option<ParseWrapper<syn::Type>>,
143+
142144
#[serde(default)]
143145
derives: Vec<ParseWrapper<syn::Path>>,
144146

@@ -305,6 +307,7 @@ fn do_generate_api(item: TokenStream) -> Result<TokenStream, syn::Error> {
305307
pre_hook_async,
306308
post_hook,
307309
post_hook_async,
310+
map_type,
308311
unknown_crates,
309312
crates,
310313
derives,
@@ -322,6 +325,7 @@ fn do_generate_api(item: TokenStream) -> Result<TokenStream, syn::Error> {
322325
post_hook.map(|post_hook| settings.with_post_hook(post_hook.into_inner().0));
323326
post_hook_async
324327
.map(|post_hook_async| settings.with_post_hook_async(post_hook_async.into_inner().0));
328+
map_type.map(|map_type| settings.with_map_type(map_type.to_token_stream()));
325329

326330
settings.with_unknown_crates(unknown_crates);
327331
crates.into_iter().for_each(

0 commit comments

Comments
 (0)