From 1f3fb6b268d262bdc8987ba7bbdddd53a2f4d12e Mon Sep 17 00:00:00 2001 From: "Adam H. Leventhal" Date: Wed, 28 May 2025 15:35:58 -0700 Subject: [PATCH] allow map_type to be specified by macro consumers --- cargo-typify/src/lib.rs | 4 ++-- typify-impl/src/lib.rs | 22 ---------------------- typify-macro/src/lib.rs | 30 +++++++++++++++++++++++++++--- typify-test/build.rs | 4 +++- typify/tests/schemas.rs | 4 ++-- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/cargo-typify/src/lib.rs b/cargo-typify/src/lib.rs index 101453d3..cb25e2ea 100644 --- a/cargo-typify/src/lib.rs +++ b/cargo-typify/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2024 Oxide Computer Company +// Copyright 2025 Oxide Computer Company //! cargo command to generate Rust code from a JSON Schema. @@ -156,7 +156,7 @@ pub fn convert(args: &CliArgs) -> Result { } if let Some(map_type) = &args.map_type { - settings.with_map_type(map_type.clone()); + settings.with_map_type(map_type.as_str()); } if let Some(unknown_crates) = &args.unknown_crates { diff --git a/typify-impl/src/lib.rs b/typify-impl/src/lib.rs index 0f5f490b..25f01a7d 100644 --- a/typify-impl/src/lib.rs +++ b/typify-impl/src/lib.rs @@ -267,34 +267,12 @@ impl std::fmt::Display for MapType { } } -impl<'de> serde::Deserialize<'de> for MapType { - fn deserialize(deserializer: D) -> std::result::Result - where - D: serde::Deserializer<'de>, - { - let s = <&str>::deserialize(deserializer)?; - Ok(Self::new(s)) - } -} - -impl From for MapType { - fn from(s: String) -> Self { - Self::new(&s) - } -} - impl From<&str> for MapType { fn from(s: &str) -> Self { Self::new(s) } } -impl From for MapType { - fn from(t: syn::Type) -> Self { - Self(t) - } -} - /// Settings that alter type generation. #[derive(Default, Debug, Clone)] pub struct TypeSpaceSettings { diff --git a/typify-macro/src/lib.rs b/typify-macro/src/lib.rs index 8401fdf5..29e4c128 100644 --- a/typify-macro/src/lib.rs +++ b/typify-macro/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2024 Oxide Computer Company +// Copyright 2025 Oxide Computer Company //! typify macro implementation. @@ -84,7 +84,7 @@ struct MacroSettings { #[serde(default)] crates: HashMap, #[serde(default)] - map_type: MapType, + map_type: Option>, #[serde(default)] patch: HashMap, MacroPatch>, @@ -223,7 +223,9 @@ fn do_import_types(item: TokenStream) -> Result { ); settings.with_unknown_crates(unknown_crates); - settings.with_map_type(map_type); + if let Some(map_type) = map_type { + settings.with_map_type(MapType(map_type.into_inner())); + } (schema.into_inner(), settings) }; @@ -263,3 +265,25 @@ fn do_import_types(item: TokenStream) -> Result { fn into_syn_err(e: typify_impl::Error, span: proc_macro2::Span) -> syn::Error { syn::Error::new(span, e.to_string()) } + +#[cfg(test)] +mod tests { + use quote::quote; + + use crate::MacroSettings; + + #[test] + fn test_settings() { + let item = quote! { + schema = "foo.json", + derives = [::foo::Foo, ::bar::Bar], + replace = { + Baz = ::baz::Baz, + }, + struct_builder = true, + map_type = ::my::map::Type, + }; + + let MacroSettings { .. } = serde_tokenstream::from_tokenstream(&item.into()).unwrap(); + } +} diff --git a/typify-test/build.rs b/typify-test/build.rs index ac0ae677..f5c77373 100644 --- a/typify-test/build.rs +++ b/typify-test/build.rs @@ -1,3 +1,5 @@ +// Copyright 2025 Oxide Computer Company + use std::collections::{HashMap, HashSet}; use std::{env, fs, path::Path}; @@ -153,7 +155,7 @@ fn main() { // Generate with a custom map type to validate requirements. let mut settings = TypeSpaceSettings::default(); - settings.with_map_type("CustomMap".to_string()); + settings.with_map_type("CustomMap"); let mut type_space = TypeSpace::new(&settings); WithMap::add(&mut type_space); diff --git a/typify/tests/schemas.rs b/typify/tests/schemas.rs index 041fce22..ad01b014 100644 --- a/typify/tests/schemas.rs +++ b/typify/tests/schemas.rs @@ -1,4 +1,4 @@ -// Copyright 2024 Oxide Computer Company +// Copyright 2025 Oxide Computer Company use std::{error::Error, fs::File, io::BufReader}; @@ -30,7 +30,7 @@ fn test_custom_map() { validate_schema( "tests/schemas/maps.json".into(), "tests/schemas/maps_custom.rs".into(), - TypeSpaceSettings::default().with_map_type("std::collections::BTreeMap".to_string()), + TypeSpaceSettings::default().with_map_type("std::collections::BTreeMap"), ) .unwrap();