Skip to content

Commit f67f113

Browse files
authored
allow map_type to be specified by macro consumers (#835)
1 parent 6c51bb9 commit f67f113

5 files changed

Lines changed: 34 additions & 30 deletions

File tree

cargo-typify/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Oxide Computer Company
1+
// Copyright 2025 Oxide Computer Company
22

33
//! cargo command to generate Rust code from a JSON Schema.
44
@@ -156,7 +156,7 @@ pub fn convert(args: &CliArgs) -> Result<String> {
156156
}
157157

158158
if let Some(map_type) = &args.map_type {
159-
settings.with_map_type(map_type.clone());
159+
settings.with_map_type(map_type.as_str());
160160
}
161161

162162
if let Some(unknown_crates) = &args.unknown_crates {

typify-impl/src/lib.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -267,34 +267,12 @@ impl std::fmt::Display for MapType {
267267
}
268268
}
269269

270-
impl<'de> serde::Deserialize<'de> for MapType {
271-
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
272-
where
273-
D: serde::Deserializer<'de>,
274-
{
275-
let s = <&str>::deserialize(deserializer)?;
276-
Ok(Self::new(s))
277-
}
278-
}
279-
280-
impl From<String> for MapType {
281-
fn from(s: String) -> Self {
282-
Self::new(&s)
283-
}
284-
}
285-
286270
impl From<&str> for MapType {
287271
fn from(s: &str) -> Self {
288272
Self::new(s)
289273
}
290274
}
291275

292-
impl From<syn::Type> for MapType {
293-
fn from(t: syn::Type) -> Self {
294-
Self(t)
295-
}
296-
}
297-
298276
/// Settings that alter type generation.
299277
#[derive(Default, Debug, Clone)]
300278
pub struct TypeSpaceSettings {

typify-macro/src/lib.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Oxide Computer Company
1+
// Copyright 2025 Oxide Computer Company
22

33
//! typify macro implementation.
44
@@ -84,7 +84,7 @@ struct MacroSettings {
8484
#[serde(default)]
8585
crates: HashMap<CrateName, MacroCrateSpec>,
8686
#[serde(default)]
87-
map_type: MapType,
87+
map_type: Option<ParseWrapper<syn::Type>>,
8888

8989
#[serde(default)]
9090
patch: HashMap<ParseWrapper<syn::Ident>, MacroPatch>,
@@ -223,7 +223,9 @@ fn do_import_types(item: TokenStream) -> Result<TokenStream, syn::Error> {
223223
);
224224
settings.with_unknown_crates(unknown_crates);
225225

226-
settings.with_map_type(map_type);
226+
if let Some(map_type) = map_type {
227+
settings.with_map_type(MapType(map_type.into_inner()));
228+
}
227229

228230
(schema.into_inner(), settings)
229231
};
@@ -263,3 +265,25 @@ fn do_import_types(item: TokenStream) -> Result<TokenStream, syn::Error> {
263265
fn into_syn_err(e: typify_impl::Error, span: proc_macro2::Span) -> syn::Error {
264266
syn::Error::new(span, e.to_string())
265267
}
268+
269+
#[cfg(test)]
270+
mod tests {
271+
use quote::quote;
272+
273+
use crate::MacroSettings;
274+
275+
#[test]
276+
fn test_settings() {
277+
let item = quote! {
278+
schema = "foo.json",
279+
derives = [::foo::Foo, ::bar::Bar],
280+
replace = {
281+
Baz = ::baz::Baz,
282+
},
283+
struct_builder = true,
284+
map_type = ::my::map::Type,
285+
};
286+
287+
let MacroSettings { .. } = serde_tokenstream::from_tokenstream(&item.into()).unwrap();
288+
}
289+
}

typify-test/build.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright 2025 Oxide Computer Company
2+
13
use std::collections::{HashMap, HashSet};
24
use std::{env, fs, path::Path};
35

@@ -153,7 +155,7 @@ fn main() {
153155

154156
// Generate with a custom map type to validate requirements.
155157
let mut settings = TypeSpaceSettings::default();
156-
settings.with_map_type("CustomMap".to_string());
158+
settings.with_map_type("CustomMap");
157159
let mut type_space = TypeSpace::new(&settings);
158160

159161
WithMap::add(&mut type_space);

typify/tests/schemas.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Oxide Computer Company
1+
// Copyright 2025 Oxide Computer Company
22

33
use std::{error::Error, fs::File, io::BufReader};
44

@@ -30,7 +30,7 @@ fn test_custom_map() {
3030
validate_schema(
3131
"tests/schemas/maps.json".into(),
3232
"tests/schemas/maps_custom.rs".into(),
33-
TypeSpaceSettings::default().with_map_type("std::collections::BTreeMap".to_string()),
33+
TypeSpaceSettings::default().with_map_type("std::collections::BTreeMap"),
3434
)
3535
.unwrap();
3636

0 commit comments

Comments
 (0)