diff --git a/Cargo.lock b/Cargo.lock index 71c6ae13..44bfb38c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2332,7 +2332,7 @@ checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" [[package]] name = "typify" version = "0.2.0" -source = "git+https://github.com/oxidecomputer/typify#9065106fc0bf1c0611dd7a5aea0c7366b7f00135" +source = "git+https://github.com/oxidecomputer/typify#0d08ac16d864fdb3b2dc25e5e6aadbef58bca67b" dependencies = [ "typify-impl", "typify-macro", @@ -2341,7 +2341,7 @@ dependencies = [ [[package]] name = "typify-impl" version = "0.2.0" -source = "git+https://github.com/oxidecomputer/typify#9065106fc0bf1c0611dd7a5aea0c7366b7f00135" +source = "git+https://github.com/oxidecomputer/typify#0d08ac16d864fdb3b2dc25e5e6aadbef58bca67b" dependencies = [ "heck", "log", @@ -2360,7 +2360,7 @@ dependencies = [ [[package]] name = "typify-macro" version = "0.2.0" -source = "git+https://github.com/oxidecomputer/typify#9065106fc0bf1c0611dd7a5aea0c7366b7f00135" +source = "git+https://github.com/oxidecomputer/typify#0d08ac16d864fdb3b2dc25e5e6aadbef58bca67b" dependencies = [ "proc-macro2", "quote", diff --git a/progenitor-impl/src/lib.rs b/progenitor-impl/src/lib.rs index ec74c308..0b8c7489 100644 --- a/progenitor-impl/src/lib.rs +++ b/progenitor-impl/src/lib.rs @@ -67,6 +67,7 @@ pub struct GenerationSettings { post_hook_async: Option, extra_derives: Vec, + map_type: Option, unknown_crates: UnknownPolicy, crates: BTreeMap, @@ -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(&mut self, map_type: MT) -> &mut Self { + self.map_type = Some(map_type.to_string()); + self + } } impl Default for Generator { @@ -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(), diff --git a/progenitor-macro/src/lib.rs b/progenitor-macro/src/lib.rs index 088c5c4b..5ee33fbc 100644 --- a/progenitor-macro/src/lib.rs +++ b/progenitor-macro/src/lib.rs @@ -139,6 +139,8 @@ struct MacroSettings { post_hook: Option>, post_hook_async: Option>, + map_type: Option>, + #[serde(default)] derives: Vec>, @@ -305,6 +307,7 @@ fn do_generate_api(item: TokenStream) -> Result { pre_hook_async, post_hook, post_hook_async, + map_type, unknown_crates, crates, derives, @@ -322,6 +325,7 @@ fn do_generate_api(item: TokenStream) -> Result { 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(