From 97beeb2548bc8b1eddb4f64c6260bf9e83751601 Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sat, 20 Dec 2025 12:38:39 +0000 Subject: [PATCH 1/2] style: remove needless pass by value --- cargo-typify/src/lib.rs | 2 +- typify-impl/src/convert.rs | 122 +++++++++++++----------------- typify-impl/src/enums.rs | 54 ++++++------- typify-impl/src/lib.rs | 71 +++++++++-------- typify-impl/src/output.rs | 4 +- typify-impl/src/rust_extension.rs | 2 +- typify-impl/src/structs.rs | 24 +++--- typify-impl/src/type_entry.rs | 50 ++++++------ typify-impl/src/util.rs | 1 + typify-macro/src/lib.rs | 28 ++++--- 10 files changed, 177 insertions(+), 181 deletions(-) diff --git a/cargo-typify/src/lib.rs b/cargo-typify/src/lib.rs index cb25e2ea..238a1391 100644 --- a/cargo-typify/src/lib.rs +++ b/cargo-typify/src/lib.rs @@ -152,7 +152,7 @@ pub fn convert(args: &CliArgs) -> Result { rename, } in &args.crates { - settings.with_crate(name, version.clone(), rename.as_ref()); + settings.with_crate(name, version.clone(), rename.clone()); } if let Some(map_type) = &args.map_type { diff --git a/typify-impl/src/convert.rs b/typify-impl/src/convert.rs index 22395eef..961dfa86 100644 --- a/typify-impl/src/convert.rs +++ b/typify-impl/src/convert.rs @@ -23,7 +23,7 @@ pub const STD_NUM_NONZERO_PREFIX: &str = "::std::num::NonZero"; impl TypeSpace { pub(crate) fn convert_schema<'a>( &mut self, - type_name: Name, + type_name: &Name, schema: &'a Schema, ) -> Result<(TypeEntry, &'a Option>)> { info!( @@ -47,7 +47,7 @@ impl TypeSpace { pub(crate) fn convert_schema_object<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, schema: &'a SchemaObject, ) -> Result<(TypeEntry, &'a Option>)> { @@ -95,8 +95,8 @@ impl TypeSpace { // wrapper to give it a name). In such a case, we invent a // new name for the inner type; otherwise, the inner type // can just have this name. - let inner_type_name = match &type_name { - Name::Required(name) => Name::Suggested(format!("{}Inner", name)), + let inner_type_name = match type_name { + Name::Required(name) => &Name::Suggested(format!("{}Inner", name)), _ => type_name, }; self.convert_option(inner_type_name, metadata, &ss) @@ -786,7 +786,7 @@ impl TypeSpace { fn convert_string<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, metadata: &'a Option>, format: &Option, @@ -891,7 +891,7 @@ impl TypeSpace { pub(crate) fn convert_enum_string<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, metadata: &'a Option>, enum_values: &[serde_json::Value], @@ -909,7 +909,7 @@ impl TypeSpace { // JSON schema. let mut has_null = false; - let validator = StringValidator::new(&type_name, validation)?; + let validator = StringValidator::new(type_name, validation)?; let variants = enum_values .iter() @@ -1087,9 +1087,9 @@ impl TypeSpace { // Use NonZero types for minimum 1 if min == Some(1.) { - return Ok((TypeEntry::new_integer(nz_ty), metadata)); + return Ok((TypeEntry::new_integer(*nz_ty), metadata)); } else { - return Ok((TypeEntry::new_integer(ty), metadata)); + return Ok((TypeEntry::new_integer(*ty), metadata)); } } @@ -1243,7 +1243,7 @@ impl TypeSpace { fn convert_object<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, metadata: &'a Option>, validation: &Option>, @@ -1265,11 +1265,8 @@ impl TypeSpace { && additional_properties.as_ref().map(AsRef::as_ref) != Some(&Schema::Bool(false)) => { - let type_entry = self.make_map( - type_name.into_option(), - property_names, - additional_properties, - )?; + let type_entry = + self.make_map(type_name.as_option(), property_names, additional_properties)?; Ok((type_entry, metadata)) } @@ -1302,7 +1299,7 @@ impl TypeSpace { )); let type_entry = self.make_map( - type_name.into_option(), + type_name.as_option(), &property_names, &additional_properties, )?; @@ -1311,15 +1308,15 @@ impl TypeSpace { } None => { - let type_entry = self.make_map(type_name.into_option(), &None, &None)?; + let type_entry = self.make_map(type_name.as_option(), &None, &None)?; Ok((type_entry, metadata)) } // The typical case Some(validation) => { - let tmp_type_name = get_type_name(&type_name, metadata); + let tmp_type_name = get_type_name(type_name, metadata); let (properties, deny_unknown_fields) = - self.struct_members(tmp_type_name, validation)?; + self.struct_members(tmp_type_name.as_ref(), validation)?; Ok(( TypeEntryStruct::from_metadata( @@ -1357,7 +1354,7 @@ impl TypeSpace { fn convert_all_of<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, metadata: &'a Option>, subschemas: &[Schema], @@ -1366,9 +1363,7 @@ impl TypeSpace { "all_of {}", serde_json::to_string_pretty(subschemas).unwrap() ); - if let Some(ty) = - self.maybe_singleton_subschema(type_name.clone(), original_schema, subschemas) - { + if let Some(ty) = self.maybe_singleton_subschema(type_name, original_schema, subschemas) { return Ok((ty, metadata)); } @@ -1444,7 +1439,7 @@ impl TypeSpace { fn convert_any_of<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, metadata: &'a Option>, subschemas: &'a [Schema], @@ -1453,7 +1448,7 @@ impl TypeSpace { // for Option. We match this here because the mutual exclusion check // below may fail for cases such as Option where T is defined to be, // say, (). In such a case, both variants are actually null. - if let Some(ty) = self.maybe_option(type_name.clone(), metadata, subschemas) { + if let Some(ty) = self.maybe_option(type_name, metadata, subschemas) { return Ok((ty, metadata)); } @@ -1532,7 +1527,7 @@ impl TypeSpace { /// Untagged enums apply to any set of subschemas so must be applied last. pub(crate) fn convert_one_of<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, metadata: &'a Option>, subschemas: &'a [Schema], @@ -1553,34 +1548,17 @@ impl TypeSpace { // but putting schemas into a predictable form. let ty = self - .maybe_option(type_name.clone(), metadata, subschemas) + .maybe_option(&type_name.clone(), metadata, subschemas) .or_else(|| { - self.maybe_externally_tagged_enum( - type_name.clone(), - original_schema, - metadata, - subschemas, - ) + self.maybe_externally_tagged_enum(type_name, original_schema, metadata, subschemas) }) .or_else(|| { - self.maybe_adjacently_tagged_enum( - type_name.clone(), - original_schema, - metadata, - subschemas, - ) - }) - .or_else(|| { - self.maybe_internally_tagged_enum( - type_name.clone(), - original_schema, - metadata, - subschemas, - ) + self.maybe_adjacently_tagged_enum(type_name, original_schema, metadata, subschemas) }) .or_else(|| { - self.maybe_singleton_subschema(type_name.clone(), original_schema, subschemas) + self.maybe_internally_tagged_enum(type_name, original_schema, metadata, subschemas) }) + .or_else(|| self.maybe_singleton_subschema(type_name, original_schema, subschemas)) .map_or_else( || self.untagged_enum(type_name, original_schema, metadata, subschemas), Ok, @@ -1607,7 +1585,7 @@ impl TypeSpace { /// allow list as is the case for non-string enumerated values). pub(crate) fn convert_not<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, metadata: &'a Option>, subschema: &'a Schema, @@ -1652,7 +1630,7 @@ impl TypeSpace { }; let (type_entry, _) = - self.convert_schema_object(Name::Unknown, original_schema, &type_schema)?; + self.convert_schema_object(&Name::Unknown, original_schema, &type_schema)?; // Make sure all the values are valid. // TODO this isn't strictly legal since we may not yet have @@ -1716,7 +1694,7 @@ impl TypeSpace { }; let (type_entry, _) = self.convert_schema_object( - Name::Unknown, + &Name::Unknown, original_schema, &typed_schema, )?; @@ -1754,7 +1732,7 @@ impl TypeSpace { fn convert_array<'a>( &mut self, - type_name: Name, + type_name: &Name, metadata: &'a Option>, validation: &ArrayValidation, ) -> Result<(TypeEntry, &'a Option>)> { @@ -1775,13 +1753,13 @@ impl TypeSpace { Some(SingleOrVec::Vec(items)) if items.len() < *max_items as usize => { let rest_name = type_name.append("additional"); let rest_id = if let Some(rest_schema) = additional_items { - self.id_for_schema(rest_name, rest_schema)?.0 + self.id_for_schema(&rest_name, rest_schema)?.0 } else { - self.id_for_schema(rest_name, &Schema::Bool(true))?.0 + self.id_for_schema(&rest_name, &Schema::Bool(true))?.0 }; let start = items.iter().enumerate().map(|(ii, item_schema)| { let item_name = type_name.append(&format!("item{}", ii)); - Ok(self.id_for_schema(item_name, item_schema)?.0) + Ok(self.id_for_schema(&item_name, item_schema)?.0) }); let rest = (items.len()..*max_items as usize).map(|_| Ok(rest_id.clone())); let types = start.chain(rest).collect::>>()?; @@ -1795,7 +1773,7 @@ impl TypeSpace { .enumerate() .map(|(ii, item_schema)| { let item_name = type_name.append(&format!("item{}", ii)); - Ok(self.id_for_schema(item_name, item_schema)?.0) + Ok(self.id_for_schema(&item_name, item_schema)?.0) }) .collect::>()?; Ok((TypeEntryDetails::Tuple(types).into(), metadata)) @@ -1803,7 +1781,9 @@ impl TypeSpace { // Array with a schema for the item. Some(SingleOrVec::Single(item_schema)) => { - let item_id = self.id_for_schema(type_name.append("item"), item_schema)?.0; + let item_id = self + .id_for_schema(&type_name.append("item"), item_schema)? + .0; Ok(( TypeEntryDetails::Array(item_id, *max_items as usize).into(), metadata, @@ -1812,7 +1792,7 @@ impl TypeSpace { // Array with no schema for the item. None => { let any_id = self - .id_for_schema(type_name.append("item"), &Schema::Bool(true))? + .id_for_schema(&type_name.append("item"), &Schema::Bool(true))? .0; Ok(( TypeEntryDetails::Array(any_id, *max_items as usize).into(), @@ -1830,11 +1810,11 @@ impl TypeSpace { unique_items, contains: None, } => { - let item_type_name = match get_type_name(&type_name, metadata) { + let item_type_name = match get_type_name(type_name, metadata) { Some(s) => Name::Suggested(format!("{}Item", s)), None => Name::Unknown, }; - let (type_id, _) = self.id_for_schema(item_type_name, item.as_ref())?; + let (type_id, _) = self.id_for_schema(&item_type_name, item.as_ref())?; // If items are unique, this is a Set; otherwise it's an Array. match unique_items { @@ -1863,7 +1843,7 @@ impl TypeSpace { } _ => Err(Error::InvalidSchema { - type_name: type_name.into_option(), + type_name: type_name.clone().into_option(), reason: format!("unhandled array validation {:#?}", validation), }), } @@ -1896,7 +1876,7 @@ impl TypeSpace { fn convert_never<'a>( &mut self, - type_name: Name, + type_name: &Name, schema: &'a Schema, ) -> Result<(TypeEntry, &'a Option>)> { let ty = TypeEntryEnum::from_metadata( @@ -1913,7 +1893,7 @@ impl TypeSpace { fn convert_typed_enum<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, schema: &'a SchemaObject, enum_values: &[serde_json::Value], @@ -1923,13 +1903,13 @@ impl TypeSpace { ..schema.clone() }; - let inner_type_name = match get_type_name(&type_name, &schema.metadata) { + let inner_type_name = match get_type_name(type_name, &schema.metadata) { Some(s) => Name::Suggested(format!("{}Inner", s)), None => Name::Unknown, }; let (type_entry, metadata) = - self.convert_schema_object(inner_type_name, original_schema, &type_schema)?; + self.convert_schema_object(&inner_type_name, original_schema, &type_schema)?; // Make sure all the values are valid. enum_values @@ -1959,7 +1939,7 @@ impl TypeSpace { fn convert_unknown_enum<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, metadata: &'a Option>, enum_values: &[serde_json::Value], @@ -2051,7 +2031,7 @@ impl TypeSpace { pub(crate) fn convert_option<'a>( &mut self, - type_name: Name, + type_name: &Name, metadata: &'a Option>, schema: &'_ Schema, ) -> Result<(TypeEntry, &'a Option>)> { @@ -2065,7 +2045,7 @@ impl TypeSpace { /// level for annotation such as a "title" or "description". pub(crate) fn maybe_singleton_subschema( &mut self, - type_name: Name, + type_name: &Name, _original_schema: &Schema, subschemas: &[Schema], ) -> Option { @@ -2103,7 +2083,7 @@ mod tests { .unwrap(); let (ty, _) = type_space .convert_schema_object( - Name::Unknown, + &Name::Unknown, &schemars::schema::Schema::Object(schema.schema.clone()), &schema.schema, ) @@ -2217,7 +2197,7 @@ mod tests { let mut type_space = TypeSpace::default(); match type_space.convert_schema_object( - Name::Unknown, + &Name::Unknown, &schemars::schema::Schema::Object(schema.clone()), &schema, ) { @@ -2249,7 +2229,7 @@ mod tests { let mut type_space = TypeSpace::default(); match type_space.convert_schema_object( - Name::Unknown, + &Name::Unknown, &schemars::schema::Schema::Object(schema.clone()), &schema, ) { diff --git a/typify-impl/src/enums.rs b/typify-impl/src/enums.rs index 9ec58297..68d80913 100644 --- a/typify-impl/src/enums.rs +++ b/typify-impl/src/enums.rs @@ -26,7 +26,7 @@ use crate::{ impl TypeSpace { pub(crate) fn maybe_option( &mut self, - type_name: Name, + type_name: &Name, metadata: &Option>, subschemas: &[Schema], ) -> Option { @@ -53,7 +53,7 @@ impl TypeSpace { pub(crate) fn maybe_externally_tagged_enum( &mut self, - type_name: Name, + type_name: &Name, original_schema: &Schema, enum_metadata: &Option>, subschemas: &[Schema], @@ -220,7 +220,7 @@ impl TypeSpace { // Append the variant name to the type_name for our new // type name hint. let (details, deny) = self - .external_variant(type_name.append(variant_name), schema) + .external_variant(&type_name.append(variant_name), schema) .ok()?; deny_unknown_fields |= deny; @@ -244,7 +244,7 @@ impl TypeSpace { /// unknown fields. fn external_variant( &mut self, - prop_type_name: Name, + prop_type_name: &Name, variant_schema: &Schema, ) -> Result<(VariantDetails, bool)> { let (ty, _) = self.convert_schema(prop_type_name, variant_schema)?; @@ -292,7 +292,7 @@ impl TypeSpace { pub(crate) fn maybe_internally_tagged_enum( &mut self, - type_name: Name, + type_name: &Name, original_schema: &Schema, metadata: &Option>, subschemas: &[Schema], @@ -368,7 +368,7 @@ impl TypeSpace { _ => unreachable!(), } - self.internal_variant(type_name.clone(), metadata, validation, tag) + self.internal_variant(type_name, metadata, validation, tag) }) .collect::>>() .ok()?; @@ -386,7 +386,7 @@ impl TypeSpace { fn internal_variant( &mut self, - enum_type_name: Name, + enum_type_name: &Name, metadata: &Option>, validation: &ObjectValidation, tag: &str, @@ -414,7 +414,7 @@ impl TypeSpace { new_validation.required.remove(tag); let (properties, _) = - self.struct_members(enum_type_name.into_option(), &new_validation)?; + self.struct_members(enum_type_name.as_option(), &new_validation)?; Ok(Variant::new( variant_name.to_string(), metadata_title_and_description(metadata), @@ -425,7 +425,7 @@ impl TypeSpace { pub(crate) fn maybe_adjacently_tagged_enum( &mut self, - type_name: Name, + type_name: &Name, original_schema: &Schema, metadata: &Option>, subschemas: &[Schema], @@ -552,7 +552,7 @@ impl TypeSpace { }; let content_schema = validation.properties.get(content).unwrap(); - let (details, deny) = self.external_variant(sub_type_name, content_schema)?; + let (details, deny) = self.external_variant(&sub_type_name, content_schema)?; let variant = Variant::new( variant_name.to_string(), @@ -594,12 +594,12 @@ impl TypeSpace { /// ``` pub(crate) fn untagged_enum( &mut self, - type_name: Name, + type_name: &Name, original_schema: &Schema, metadata: &Option>, subschemas: &[Schema], ) -> Result { - let tmp_type_name = get_type_name(&type_name, metadata); + let tmp_type_name = get_type_name(type_name, metadata); let mut deny_unknown_fields = false; @@ -658,7 +658,7 @@ impl TypeSpace { } .append(&variant_name); - let (details, deny) = self.external_variant(prop_type_name, schema)?; + let (details, deny) = self.external_variant(&prop_type_name, schema)?; // Note that this is really only relevant for in-line schemas; // referenced schemas will enforce their own policy on their // generated types. @@ -884,7 +884,7 @@ mod tests { assert!(type_space .maybe_externally_tagged_enum( - Name::Required("ExternallyTaggedEnum".to_string()), + &Name::Required("ExternallyTaggedEnum".to_string()), &original_schema, &None, &subschemas, @@ -892,7 +892,7 @@ mod tests { .is_some()); assert!(type_space .maybe_adjacently_tagged_enum( - Name::Required("ExternallyTaggedEnum".to_string()), + &Name::Required("ExternallyTaggedEnum".to_string()), &original_schema, &None, &subschemas, @@ -900,7 +900,7 @@ mod tests { .is_none()); assert!(type_space .maybe_internally_tagged_enum( - Name::Required("ExternallyTaggedEnum".to_string()), + &Name::Required("ExternallyTaggedEnum".to_string()), &original_schema, &None, &subschemas, @@ -937,7 +937,7 @@ mod tests { assert!(type_space .maybe_adjacently_tagged_enum( - Name::Required("AdjacentlyTaggedEnum".to_string()), + &Name::Required("AdjacentlyTaggedEnum".to_string()), &original_schema, &None, &subschemas, @@ -945,7 +945,7 @@ mod tests { .is_some()); assert!(type_space .maybe_externally_tagged_enum( - Name::Required("AdjacentlyTaggedEnum".to_string()), + &Name::Required("AdjacentlyTaggedEnum".to_string()), &original_schema, &None, &subschemas, @@ -982,7 +982,7 @@ mod tests { assert!(type_space .maybe_internally_tagged_enum( - Name::Required("InternallyTaggedEnum".to_string()), + &Name::Required("InternallyTaggedEnum".to_string()), &original_schema, &None, &subschemas, @@ -990,7 +990,7 @@ mod tests { .is_some()); assert!(type_space .maybe_adjacently_tagged_enum( - Name::Required("InternallyTaggedEnum".to_string()), + &Name::Required("InternallyTaggedEnum".to_string()), &original_schema, &None, &subschemas, @@ -998,7 +998,7 @@ mod tests { .is_none()); assert!(type_space .maybe_externally_tagged_enum( - Name::Required("InternallyTaggedEnum".to_string()), + &Name::Required("InternallyTaggedEnum".to_string()), &original_schema, &None, &subschemas, @@ -1034,7 +1034,7 @@ mod tests { let subschemas = schema.schema.subschemas.unwrap().any_of.unwrap(); let ty = type_space .untagged_enum( - Name::Required("UntaggedEnum".to_string()), + &Name::Required("UntaggedEnum".to_string()), &original_schema, &None, &subschemas, @@ -1110,7 +1110,7 @@ mod tests { let (ty, _) = type_space .convert_one_of( - Name::Required("Xyz".to_string()), + &Name::Required("Xyz".to_string()), &original_schema, &None, &subschemas, @@ -1208,7 +1208,7 @@ mod tests { let (type_entry, _) = type_space .convert_schema_object( - Name::Unknown, + &Name::Unknown, &schemars::schema::Schema::Object(schema.schema.clone()), &schema.schema, ) @@ -1250,7 +1250,7 @@ mod tests { let mut type_space = TypeSpace::default(); let type_entry = type_space - .maybe_option(Name::Unknown, &None, &subschemas) + .maybe_option(&Name::Unknown, &None, &subschemas) .unwrap(); assert_eq!(type_entry.details, TypeEntryDetails::Option(TypeId(1))) @@ -1461,7 +1461,7 @@ mod tests { let subschemas = schema.schema.subschemas.unwrap().one_of.unwrap(); let type_entry = type_space .maybe_externally_tagged_enum( - Name::Required("ResultX".to_string()), + &Name::Required("ResultX".to_string()), &original_schema, &None, &subschemas, @@ -1517,7 +1517,7 @@ mod tests { let subschemas = schema.schema.subschemas.unwrap().one_of.unwrap(); let type_entry = type_space .maybe_externally_tagged_enum( - Name::Required("ResultX".to_string()), + &Name::Required("ResultX".to_string()), &schemars::schema::Schema::Bool(true), &None, &subschemas, diff --git a/typify-impl/src/lib.rs b/typify-impl/src/lib.rs index 2f791ba2..7427171c 100644 --- a/typify-impl/src/lib.rs +++ b/typify-impl/src/lib.rs @@ -152,6 +152,13 @@ pub(crate) enum Name { } impl Name { + pub fn as_option(&self) -> Option<&String> { + match self { + Name::Required(s) | Name::Suggested(s) => Some(s), + Name::Unknown => None, + } + } + pub fn into_option(self) -> Option { match self { Name::Required(s) | Name::Suggested(s) => Some(s), @@ -431,16 +438,20 @@ impl TypeSpaceSettings { /// Replace a referenced type with a named type. This causes the referenced /// type *not* to be generated. If the same `type_name` is specified multiple times, /// the last one is honored. - pub fn with_replacement>( + pub fn with_replacement< + TS: Into, + RS: Into, + I: Iterator, + >( &mut self, type_name: TS, replace_type: RS, impls: I, ) -> &mut Self { self.replace.insert( - type_name.to_string(), + type_name.into(), TypeSpaceReplace { - replace_type: replace_type.to_string(), + replace_type: replace_type.into(), impls: impls.collect(), }, ); @@ -451,12 +462,12 @@ impl TypeSpaceSettings { /// created by the input JSON schema does **not** result in an error and is /// silently ignored. If the same `type_name` is specified multiple times, /// the last one is honored. - pub fn with_patch( + pub fn with_patch>( &mut self, type_name: S, - type_patch: &TypeSpacePatch, + type_patch: TypeSpacePatch, ) -> &mut Self { - self.patch.insert(type_name.to_string(), type_patch.clone()); + self.patch.insert(type_name.into(), type_patch); self } @@ -485,7 +496,7 @@ impl TypeSpaceSettings { /// ), /// ); /// ``` - pub fn with_conversion>( + pub fn with_conversion, I: Iterator>( &mut self, schema: schemars::schema::SchemaObject, type_name: S, @@ -493,7 +504,7 @@ impl TypeSpaceSettings { ) -> &mut Self { self.convert.push(TypeSpaceConversion { schema, - type_name: type_name.to_string(), + type_name: type_name.into(), impls: impls.collect(), }); self @@ -515,19 +526,14 @@ impl TypeSpaceSettings { /// generate) types from the given crate and version. The version should /// precisely match the version of the crate that you expect as a /// dependency. - pub fn with_crate( + pub fn with_crate>( &mut self, crate_name: S1, version: CrateVers, - rename: Option<&String>, + rename: Option, ) -> &mut Self { - self.crates.insert( - crate_name.to_string(), - CrateSpec { - version, - rename: rename.cloned(), - }, - ); + self.crates + .insert(crate_name.into(), CrateSpec { version, rename }); self } @@ -553,14 +559,14 @@ impl TypeSpaceSettings { impl TypeSpacePatch { /// Specify the new name for patched type. - pub fn with_rename(&mut self, rename: S) -> &mut Self { - self.rename = Some(rename.to_string()); + pub fn with_rename>(mut self, rename: S) -> Self { + self.rename = Some(rename.into()); self } /// Specify an additional derive to apply to the patched type. - pub fn with_derive(&mut self, derive: S) -> &mut Self { - self.derives.push(derive.to_string()); + pub fn with_derive>(mut self, derive: S) -> Self { + self.derives.push(derive.into()); self } } @@ -657,7 +663,7 @@ impl TypeSpace { } else { Name::Unknown }; - self.convert_ref_type(type_name, schema, type_id)? + self.convert_ref_type(&type_name, &schema, type_id)? } Some(replace_type) => { @@ -685,8 +691,13 @@ impl TypeSpace { Ok(()) } - fn convert_ref_type(&mut self, type_name: Name, schema: Schema, type_id: TypeId) -> Result<()> { - let (mut type_entry, metadata) = self.convert_schema(type_name.clone(), &schema)?; + fn convert_ref_type( + &mut self, + type_name: &Name, + schema: &Schema, + type_id: TypeId, + ) -> Result<()> { + let (mut type_entry, metadata) = self.convert_schema(type_name, schema)?; let default = metadata .as_ref() .and_then(|m| m.default.as_ref()) @@ -719,7 +730,7 @@ impl TypeSpace { schema.clone(), ), - TypeEntryDetails::Native(native) if native.name_match(&type_name) => type_entry, + TypeEntryDetails::Native(native) if native.name_match(type_name) => type_entry, // For types that don't have names, this is effectively a type // alias which we treat as a newtype. @@ -767,7 +778,7 @@ impl TypeSpace { Some(s) => Name::Suggested(s), None => Name::Unknown, }; - let (type_id, _) = self.id_for_schema(name, schema)?; + let (type_id, _) = self.id_for_schema(&name, schema)?; // Finalize all created types. for index in base_id..self.next_id { @@ -961,7 +972,7 @@ impl TypeSpace { /// properties of a struct. fn id_for_schema<'a>( &mut self, - type_name: Name, + type_name: &Name, schema: &'a Schema, ) -> Result<(TypeId, &'a Option>)> { let (mut type_entry, metadata) = self.convert_schema(type_name, schema)?; @@ -1261,7 +1272,7 @@ mod tests { type_space.add_ref_types(schema.definitions).unwrap(); let (ty, _) = type_space .convert_schema_object( - Name::Unknown, + &Name::Unknown, &schemars::schema::Schema::Object(schema.schema.clone()), &schema.schema, ) @@ -1329,7 +1340,7 @@ mod tests { type_space.add_ref_types(schema.definitions).unwrap(); let (ty, _) = type_space .convert_schema_object( - Name::Unknown, + &Name::Unknown, &schemars::schema::Schema::Object(schema.schema.clone()), &schema.schema, ) @@ -1374,7 +1385,7 @@ mod tests { let mut type_space = TypeSpace::default(); let (te, _) = type_space .convert_enum_string( - Name::Required("OnTheGo".to_string()), + &Name::Required("OnTheGo".to_string()), &serde_json::from_value(original_schema).unwrap(), &None, &enum_values, diff --git a/typify-impl/src/output.rs b/typify-impl/src/output.rs index 876c92a0..663f0d90 100644 --- a/typify-impl/src/output.rs +++ b/typify-impl/src/output.rs @@ -22,11 +22,11 @@ impl OutputSpace { pub fn add_item( &mut self, location: OutputSpaceMod, - order_hint: impl ToString, + order_hint: impl Into, stream: TokenStream, ) { self.items - .entry((location, order_hint.to_string())) + .entry((location, order_hint.into())) .or_default() .extend(stream); } diff --git a/typify-impl/src/rust_extension.rs b/typify-impl/src/rust_extension.rs index e7a6cf5f..5e343b40 100644 --- a/typify-impl/src/rust_extension.rs +++ b/typify-impl/src/rust_extension.rs @@ -90,7 +90,7 @@ impl TypeSpace { .iter() .map(|p_schema| { // TODO could we have some reasonable type name? Do we need to? - let (param_id, _) = self.id_for_schema(Name::Unknown, p_schema)?; + let (param_id, _) = self.id_for_schema(&Name::Unknown, p_schema)?; Ok(param_id) }) .collect::>>() diff --git a/typify-impl/src/structs.rs b/typify-impl/src/structs.rs index 4e285259..e8b5b901 100644 --- a/typify-impl/src/structs.rs +++ b/typify-impl/src/structs.rs @@ -18,7 +18,7 @@ use crate::{ impl TypeSpace { pub(crate) fn struct_members( &mut self, - type_name: Option, + type_name: Option<&String>, validation: &ObjectValidation, ) -> Result<(Vec, bool)> { // These are the fields we don't currently handle @@ -98,7 +98,7 @@ impl TypeSpace { additional_properties @ Some(_) => { let sub_type_name = type_name.as_ref().map(|base| format!("{}_extra", base)); let map_type = self.make_map( - sub_type_name, + sub_type_name.as_ref(), &validation.property_names, additional_properties, )?; @@ -130,7 +130,7 @@ impl TypeSpace { Some(name) => Name::Suggested(name), None => Name::Unknown, }; - let (mut type_id, metadata) = self.id_for_schema(sub_type_name, schema)?; + let (mut type_id, metadata) = self.id_for_schema(&sub_type_name, schema)?; let state = if required.contains(prop_name) { StructPropertyState::Required @@ -184,7 +184,7 @@ impl TypeSpace { pub(crate) fn make_map( &mut self, - type_name: Option, + type_name: Option<&String>, property_names: &Option>, additional_properties: &Option>, ) -> Result { @@ -200,7 +200,7 @@ impl TypeSpace { Some(name) => Name::Suggested(format!("{}Key", name)), None => Name::Unknown, }; - self.id_for_schema_string(key_type_name, obj)? + self.id_for_schema_string(&key_type_name, obj)? } }; @@ -210,10 +210,10 @@ impl TypeSpace { Some(name) => Name::Suggested(format!("{}Value", name)), None => Name::Unknown, }; - self.id_for_schema(value_type_name, value_schema)? + self.id_for_schema(&value_type_name, value_schema)? } - None => self.id_for_schema(Name::Unknown, &Schema::Bool(true))?, + None => self.id_for_schema(&Name::Unknown, &Schema::Bool(true))?, }; Ok(TypeEntryDetails::Map(key_id, value_id).into()) @@ -222,7 +222,7 @@ impl TypeSpace { /// Perform a schema conversion for a type that must be string-like. pub(crate) fn id_for_schema_string( &mut self, - type_name: Name, + type_name: &Name, schema_obj: &SchemaObject, ) -> Result { match schema_obj { @@ -267,7 +267,7 @@ impl TypeSpace { /// has type T_N for each member of the struct, the former has Option. pub(crate) fn flattened_union_struct<'a>( &mut self, - type_name: Name, + type_name: &Name, original_schema: &'a Schema, metadata: &'a Option>, subschemas: &[Schema], @@ -277,12 +277,12 @@ impl TypeSpace { .iter() .enumerate() .map(|(idx, schema)| { - let type_name = match get_type_name(&type_name, metadata) { + let type_name = match get_type_name(type_name, metadata) { Some(name) => Name::Suggested(format!("{}Subtype{}", name, idx)), None => Name::Unknown, }; - let (mut type_id, _) = self.id_for_schema(type_name, schema)?; + let (mut type_id, _) = self.id_for_schema(&type_name, schema)?; if optional { type_id = self.id_to_option(&type_id); } @@ -567,7 +567,7 @@ mod tests { }); let mut type_space = TypeSpace::default(); - let (ty, _) = type_space.convert_schema(Name::Unknown, &schema).unwrap(); + let (ty, _) = type_space.convert_schema(&Name::Unknown, &schema).unwrap(); let output = ty.type_name(&type_space).replace(" ", ""); assert_eq!( output, diff --git a/typify-impl/src/type_entry.rs b/typify-impl/src/type_entry.rs index 913e78bf..ebc7b246 100644 --- a/typify-impl/src/type_entry.rs +++ b/typify-impl/src/type_entry.rs @@ -239,7 +239,7 @@ fn variants_unique(variants: &[Variant]) -> bool { impl TypeEntryEnum { pub(crate) fn from_metadata( type_space: &TypeSpace, - type_name: Name, + type_name: &Name, metadata: &Option>, tag_type: EnumTagType, mut variants: Vec, @@ -286,7 +286,7 @@ impl TypeEntryEnum { panic!("Failed to make unique variant names for [{}]", dups); } - let name = get_type_name(&type_name, metadata).unwrap(); + let name = get_type_name(type_name, metadata).unwrap(); let rename = None; let description = metadata_description(metadata); @@ -366,13 +366,13 @@ impl Variant { impl TypeEntryStruct { pub(crate) fn from_metadata( type_space: &TypeSpace, - type_name: Name, + type_name: &Name, metadata: &Option>, properties: Vec, deny_unknown_fields: bool, schema: Schema, ) -> TypeEntry { - let name = get_type_name(&type_name, metadata).unwrap(); + let name = get_type_name(type_name, metadata).unwrap(); let rename = None; let description = metadata_description(metadata); let default = metadata @@ -403,12 +403,12 @@ impl TypeEntryStruct { impl TypeEntryNewtype { pub(crate) fn from_metadata( type_space: &TypeSpace, - type_name: Name, + type_name: &Name, metadata: &Option>, type_id: TypeId, schema: Schema, ) -> TypeEntry { - let name = get_type_name(&type_name, metadata).unwrap(); + let name = get_type_name(type_name, metadata).unwrap(); let rename = None; let description = metadata_description(metadata); @@ -432,13 +432,13 @@ impl TypeEntryNewtype { pub(crate) fn from_metadata_with_enum_values( type_space: &TypeSpace, - type_name: Name, + type_name: &Name, metadata: &Option>, type_id: TypeId, enum_values: &[serde_json::Value], schema: Schema, ) -> TypeEntry { - let name = get_type_name(&type_name, metadata).unwrap(); + let name = get_type_name(type_name, metadata).unwrap(); let rename = None; let description = metadata_description(metadata); @@ -464,13 +464,13 @@ impl TypeEntryNewtype { pub(crate) fn from_metadata_with_deny_values( type_space: &TypeSpace, - type_name: Name, + type_name: &Name, metadata: &Option>, type_id: TypeId, enum_values: &[serde_json::Value], schema: Schema, ) -> TypeEntry { - let name = get_type_name(&type_name, metadata).unwrap(); + let name = get_type_name(type_name, metadata).unwrap(); let rename = None; let description = metadata_description(metadata); @@ -496,13 +496,13 @@ impl TypeEntryNewtype { pub(crate) fn from_metadata_with_string_validation( type_space: &TypeSpace, - type_name: Name, + type_name: &Name, metadata: &Option>, type_id: TypeId, validation: &schemars::schema::StringValidation, schema: Schema, ) -> TypeEntry { - let name = get_type_name(&type_name, metadata).unwrap(); + let name = get_type_name(type_name, metadata).unwrap(); let rename = None; let description = metadata_description(metadata); @@ -545,20 +545,20 @@ impl From for TypeEntry { } impl TypeEntry { - pub(crate) fn new_native(type_name: S, impls: &[TypeSpaceImpl]) -> Self { + pub(crate) fn new_native>(type_name: S, impls: &[TypeSpaceImpl]) -> Self { TypeEntry { details: TypeEntryDetails::Native(TypeEntryNative { - type_name: type_name.to_string(), + type_name: type_name.into(), impls: impls.to_vec(), parameters: Default::default(), }), extra_derives: Default::default(), } } - pub(crate) fn new_native_params(type_name: S, params: &[TypeId]) -> Self { + pub(crate) fn new_native_params>(type_name: S, params: &[TypeId]) -> Self { TypeEntry { details: TypeEntryDetails::Native(TypeEntryNative { - type_name: type_name.to_string(), + type_name: type_name.into(), impls: Default::default(), parameters: params.to_vec(), }), @@ -571,12 +571,12 @@ impl TypeEntry { extra_derives: Default::default(), } } - pub(crate) fn new_integer(type_name: S) -> Self { - TypeEntryDetails::Integer(type_name.to_string()).into() + pub(crate) fn new_integer>(type_name: S) -> Self { + TypeEntryDetails::Integer(type_name.into()).into() } - pub(crate) fn new_float(type_name: S) -> Self { + pub(crate) fn new_float>(type_name: S) -> Self { TypeEntry { - details: TypeEntryDetails::Float(type_name.to_string()), + details: TypeEntryDetails::Float(type_name.into()), extra_derives: Default::default(), } } @@ -736,7 +736,7 @@ impl TypeEntry { self.output_enum(type_space, output, enum_details, derive_set) } TypeEntryDetails::Struct(struct_details) => { - self.output_struct(type_space, output, struct_details, derive_set) + self.output_struct(type_space, output, struct_details, &derive_set) } TypeEntryDetails::Newtype(newtype_details) => { self.output_newtype(type_space, output, newtype_details, derive_set) @@ -1069,7 +1069,7 @@ impl TypeEntry { }; let derives = strings_to_derives( - derive_set, + &derive_set, &self.extra_derives, &type_space.settings.extra_derives, ); @@ -1102,7 +1102,7 @@ impl TypeEntry { type_space: &TypeSpace, output: &mut OutputSpace, struct_details: &TypeEntryStruct, - derive_set: BTreeSet<&str>, + derive_set: &BTreeSet<&str>, ) { enum PropDefault { None(String), @@ -1653,7 +1653,7 @@ impl TypeEntry { }); let derives = strings_to_derives( - derive_set, + &derive_set, &self.extra_derives, &type_space.settings.extra_derives, ); @@ -2005,7 +2005,7 @@ fn make_doc(name: &str, description: Option<&String>, schema: &Schema) -> TokenS } fn strings_to_derives<'a>( - derive_set: BTreeSet<&'a str>, + derive_set: &'a BTreeSet<&'a str>, type_derives: &'a BTreeSet, extra_derives: &'a [String], ) -> impl Iterator + 'a { diff --git a/typify-impl/src/util.rs b/typify-impl/src/util.rs index 848ea5b6..1d711b5f 100644 --- a/typify-impl/src/util.rs +++ b/typify-impl/src/util.rs @@ -763,6 +763,7 @@ pub(crate) fn singleton_subschema(subschemas: &SubschemaValidation) -> Option<&S } } +#[derive(Clone, Copy)] pub(crate) enum Case { Pascal, Snake, diff --git a/typify-macro/src/lib.rs b/typify-macro/src/lib.rs index 29e4c128..f5daa8b2 100644 --- a/typify-macro/src/lib.rs +++ b/typify-macro/src/lib.rs @@ -168,12 +168,12 @@ struct MacroPatch { impl From for TypeSpacePatch { fn from(a: MacroPatch) -> Self { let mut s = Self::default(); - a.rename.iter().for_each(|rename| { - s.with_rename(rename); - }); - a.derives.iter().for_each(|derive| { - s.with_derive(derive.to_token_stream()); - }); + if let Some(rename) = &a.rename { + s = s.with_rename(rename.to_string()); + } + for derive in &a.derives { + s = s.with_derive(derive.to_token_stream().to_string()); + } s } } @@ -201,11 +201,15 @@ fn do_import_types(item: TokenStream) -> Result { settings.with_struct_builder(struct_builder); patch.into_iter().for_each(|(type_name, patch)| { - settings.with_patch(type_name.to_token_stream(), &patch.into()); + settings.with_patch(type_name.to_token_stream().to_string(), patch.into()); }); replace.into_iter().for_each(|(type_name, type_and_impls)| { let (replace_type, impls) = type_and_impls.into_inner().into_name_and_impls(); - settings.with_replacement(type_name.to_token_stream(), replace_type, impls.into_iter()); + settings.with_replacement( + type_name.to_token_stream().to_string(), + replace_type, + impls.into_iter(), + ); }); convert.into_iter().for_each(|(schema, type_and_impls)| { let (type_name, impls) = type_and_impls.into_inner().into_name_and_impls(); @@ -215,7 +219,7 @@ fn do_import_types(item: TokenStream) -> Result { crates.into_iter().for_each( |(CrateName(crate_name), MacroCrateSpec { original, version })| { if let Some(original_crate) = original { - settings.with_crate(original_crate, version, Some(&crate_name)); + settings.with_crate(original_crate, version, Some(crate_name)); } else { settings.with_crate(crate_name, version, None); } @@ -249,7 +253,7 @@ fn do_import_types(item: TokenStream) -> Result { let mut type_space = TypeSpace::new(&settings); type_space .add_root_schema(root_schema) - .map_err(|e| into_syn_err(e, schema.span()))?; + .map_err(|e| into_syn_err(&e, schema.span()))?; let path_str = path.to_string_lossy(); let output = quote! { @@ -262,7 +266,7 @@ fn do_import_types(item: TokenStream) -> Result { Ok(output.into()) } -fn into_syn_err(e: typify_impl::Error, span: proc_macro2::Span) -> syn::Error { +fn into_syn_err(e: &typify_impl::Error, span: proc_macro2::Span) -> syn::Error { syn::Error::new(span, e.to_string()) } @@ -284,6 +288,6 @@ mod tests { map_type = ::my::map::Type, }; - let MacroSettings { .. } = serde_tokenstream::from_tokenstream(&item.into()).unwrap(); + let MacroSettings { .. } = serde_tokenstream::from_tokenstream(&item).unwrap(); } } From 22e9ca5c2f73c3ad5afe9390fa81e8ca96f8afba Mon Sep 17 00:00:00 2001 From: Daniel Eades Date: Sat, 20 Dec 2025 13:02:40 +0000 Subject: [PATCH 2/2] enforce lint --- Cargo.toml | 3 +++ cargo-typify/Cargo.toml | 3 +++ example-build/Cargo.toml | 3 +++ example-macro/Cargo.toml | 3 +++ typify-impl/Cargo.toml | 3 +++ typify-macro/Cargo.toml | 3 +++ typify-test/Cargo.toml | 3 +++ typify/Cargo.toml | 3 +++ 8 files changed, 24 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 37c2c943..85430142 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,3 +44,6 @@ thiserror = "2.0.17" trybuild = "1.0.114" unicode-ident = "1.0.22" uuid = "1.16.0" + +[workspace.lints.clippy] +needless_pass_by_value = "warn" diff --git a/cargo-typify/Cargo.toml b/cargo-typify/Cargo.toml index d09cdd54..3183a3fc 100644 --- a/cargo-typify/Cargo.toml +++ b/cargo-typify/Cargo.toml @@ -27,3 +27,6 @@ assert_cmd = { workspace = true } expectorate = { workspace = true } newline-converter = { workspace = true } tempdir = { workspace = true } + +[lints] +workspace = true diff --git a/example-build/Cargo.toml b/example-build/Cargo.toml index ca9a9d41..949f10b8 100644 --- a/example-build/Cargo.toml +++ b/example-build/Cargo.toml @@ -13,3 +13,6 @@ schemars = "0.8" serde_json = "1.0" syn = "2.0" typify = { path = "../typify" } + +[lints] +workspace = true diff --git a/example-macro/Cargo.toml b/example-macro/Cargo.toml index 8d90ad19..4eb0dfcd 100644 --- a/example-macro/Cargo.toml +++ b/example-macro/Cargo.toml @@ -7,3 +7,6 @@ edition = "2021" typify = { path = "../typify" } serde = "1.0" serde_json = "1.0" + +[lints] +workspace = true diff --git a/typify-impl/Cargo.toml b/typify-impl/Cargo.toml index 11cdb3f1..b9a92b3c 100644 --- a/typify-impl/Cargo.toml +++ b/typify-impl/Cargo.toml @@ -30,3 +30,6 @@ schema = { workspace = true } schemars = { workspace = true, features = ["uuid1", "impl_json_schema"] } syn = { workspace = true, features = ["full", "extra-traits", "visit-mut"] } uuid = { workspace = true } + +[lints] +workspace = true diff --git a/typify-macro/Cargo.toml b/typify-macro/Cargo.toml index acf12bf6..ae3c6d21 100644 --- a/typify-macro/Cargo.toml +++ b/typify-macro/Cargo.toml @@ -20,3 +20,6 @@ serde_json = "1.0.145" serde_tokenstream = "0.2.2" syn = { version = "2.0", features = ["full", "extra-traits"] } typify-impl = { version = "0.5.0", path = "../typify-impl" } + +[lints] +workspace = true diff --git a/typify-test/Cargo.toml b/typify-test/Cargo.toml index 8e134288..86993ac4 100644 --- a/typify-test/Cargo.toml +++ b/typify-test/Cargo.toml @@ -16,3 +16,6 @@ prettyplease = { workspace = true } schemars = { workspace = true } serde = { workspace = true } syn = { workspace = true } + +[lints] +workspace = true diff --git a/typify/Cargo.toml b/typify/Cargo.toml index 534f1fa8..83a709be 100644 --- a/typify/Cargo.toml +++ b/typify/Cargo.toml @@ -30,3 +30,6 @@ serde = { workspace = true } serde_json = { workspace = true } trybuild = { workspace = true } uuid = { workspace = true, features = ["serde"] } + +[lints] +workspace = true