Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cargo-typify/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Oxide Computer Company
// Copyright 2025 Oxide Computer Company

//! cargo command to generate Rust code from a JSON Schema.

Expand Down Expand Up @@ -156,7 +156,7 @@ pub fn convert(args: &CliArgs) -> Result<String> {
}

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 {
Expand Down
22 changes: 0 additions & 22 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,34 +267,12 @@ impl std::fmt::Display for MapType {
}
}

impl<'de> serde::Deserialize<'de> for MapType {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = <&str>::deserialize(deserializer)?;
Ok(Self::new(s))
}
}

impl From<String> 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<syn::Type> for MapType {
fn from(t: syn::Type) -> Self {
Self(t)
}
}

/// Settings that alter type generation.
#[derive(Default, Debug, Clone)]
pub struct TypeSpaceSettings {
Expand Down
30 changes: 27 additions & 3 deletions typify-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Oxide Computer Company
// Copyright 2025 Oxide Computer Company

//! typify macro implementation.

Expand Down Expand Up @@ -84,7 +84,7 @@ struct MacroSettings {
#[serde(default)]
crates: HashMap<CrateName, MacroCrateSpec>,
#[serde(default)]
map_type: MapType,
map_type: Option<ParseWrapper<syn::Type>>,

#[serde(default)]
patch: HashMap<ParseWrapper<syn::Ident>, MacroPatch>,
Expand Down Expand Up @@ -223,7 +223,9 @@ fn do_import_types(item: TokenStream) -> Result<TokenStream, syn::Error> {
);
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)
};
Expand Down Expand Up @@ -263,3 +265,25 @@ fn do_import_types(item: TokenStream) -> Result<TokenStream, syn::Error> {
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();
}
}
4 changes: 3 additions & 1 deletion typify-test/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2025 Oxide Computer Company

use std::collections::{HashMap, HashSet};
use std::{env, fs, path::Path};

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions typify/tests/schemas.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Oxide Computer Company
// Copyright 2025 Oxide Computer Company

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

Expand Down Expand Up @@ -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();

Expand Down