Skip to content

Commit

Permalink
support custom map types (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahBull authored Dec 24, 2024
1 parent 2727eb2 commit cb9bf78
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions progenitor-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct GenerationSettings {
post_hook_async: Option<TokenStream>,
extra_derives: Vec<String>,

map_type: Option<String>,
unknown_crates: UnknownPolicy,
crates: BTreeMap<String, CrateSpec>,

Expand Down Expand Up @@ -228,6 +229,18 @@ impl GenerationSettings {
);
self
}

/// Set the type used for key-value maps. Common examples:
/// - [`std::collections::HashMap`] - **Default**
/// - [`std::collections::BTreeMap`]
/// - [`indexmap::IndexMap`]
///
/// The requiremnets for a map type can be found in the
/// [typify::TypeSpaceSettings::with_map_type] documentation.
pub fn with_map_type<MT: ToString>(&mut self, map_type: MT) -> &mut Self {
self.map_type = Some(map_type.to_string());
self
}
}

impl Default for Generator {
Expand Down Expand Up @@ -278,6 +291,11 @@ impl Generator {
type_settings.with_conversion(schema.clone(), type_name, impls.iter().cloned());
});

// Set the map type if specified.
if let Some(map_type) = &settings.map_type {
type_settings.with_map_type(map_type.clone());
}

Self {
type_space: TypeSpace::new(&type_settings),
settings: settings.clone(),
Expand Down
4 changes: 4 additions & 0 deletions progenitor-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ struct MacroSettings {
post_hook: Option<ParseWrapper<ClosureOrPath>>,
post_hook_async: Option<ParseWrapper<ClosureOrPath>>,

map_type: Option<ParseWrapper<syn::Type>>,

#[serde(default)]
derives: Vec<ParseWrapper<syn::Path>>,

Expand Down Expand Up @@ -305,6 +307,7 @@ fn do_generate_api(item: TokenStream) -> Result<TokenStream, syn::Error> {
pre_hook_async,
post_hook,
post_hook_async,
map_type,
unknown_crates,
crates,
derives,
Expand All @@ -322,6 +325,7 @@ fn do_generate_api(item: TokenStream) -> Result<TokenStream, syn::Error> {
post_hook.map(|post_hook| settings.with_post_hook(post_hook.into_inner().0));
post_hook_async
.map(|post_hook_async| settings.with_post_hook_async(post_hook_async.into_inner().0));
map_type.map(|map_type| settings.with_map_type(map_type.to_token_stream()));

settings.with_unknown_crates(unknown_crates);
crates.into_iter().for_each(
Expand Down

0 comments on commit cb9bf78

Please sign in to comment.