Skip to content

Commit bd5c9d9

Browse files
author
ericszentivanyi
committed
chore: convert type_patch into a struct with named fields
1 parent a210e49 commit bd5c9d9

3 files changed

Lines changed: 53 additions & 41 deletions

File tree

typify-impl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ impl TypeSpacePatch {
582582
}
583583

584584
impl TypeSpace {
585-
/// Create a new TypeSpace with custom settings
585+
/// Create a new TypeSpace with custom settings.
586586
pub fn new(settings: &TypeSpaceSettings) -> Self {
587587
let mut cache = SchemaCache::default();
588588

typify-impl/src/type_entry.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
output::{OutputSpace, OutputSpaceMod},
1414
sanitize,
1515
structs::{generate_serde_attr, DefaultFunction},
16-
util::{get_type_name, metadata_description, type_patch, unique},
16+
util::{get_type_name, metadata_description, unique, TypePatch},
1717
Case, DefaultImpl, Name, Result, TypeId, TypeSpace, TypeSpaceImpl,
1818
};
1919

@@ -291,10 +291,10 @@ impl TypeEntryEnum {
291291
let rename = None;
292292
let description = metadata_description(metadata);
293293

294-
let (name, extra_derives, extra_attrs) = type_patch(type_space, name);
294+
let type_patch = TypePatch::new(type_space, name);
295295

296296
let details = TypeEntryDetails::Enum(Self {
297-
name,
297+
name: type_patch.name,
298298
rename,
299299
description,
300300
default: None,
@@ -307,8 +307,8 @@ impl TypeEntryEnum {
307307

308308
TypeEntry {
309309
details,
310-
extra_derives,
311-
extra_attrs,
310+
extra_derives: type_patch.derives,
311+
extra_attrs: type_patch.attrs,
312312
}
313313
}
314314

@@ -383,10 +383,10 @@ impl TypeEntryStruct {
383383
.cloned()
384384
.map(WrappedValue::new);
385385

386-
let (name, extra_derives, extra_attrs) = type_patch(type_space, name);
386+
let type_patch = TypePatch::new(type_space, name);
387387

388388
let details = TypeEntryDetails::Struct(Self {
389-
name,
389+
name: type_patch.name,
390390
rename,
391391
description,
392392
default,
@@ -397,8 +397,8 @@ impl TypeEntryStruct {
397397

398398
TypeEntry {
399399
details,
400-
extra_derives,
401-
extra_attrs,
400+
extra_derives: type_patch.derives,
401+
extra_attrs: type_patch.attrs,
402402
}
403403
}
404404
}
@@ -415,10 +415,10 @@ impl TypeEntryNewtype {
415415
let rename = None;
416416
let description = metadata_description(metadata);
417417

418-
let (name, extra_derives, extra_attrs) = type_patch(type_space, name);
418+
let type_patch = TypePatch::new(type_space, name);
419419

420420
let details = TypeEntryDetails::Newtype(Self {
421-
name,
421+
name: type_patch.name,
422422
rename,
423423
description,
424424
default: None,
@@ -429,8 +429,8 @@ impl TypeEntryNewtype {
429429

430430
TypeEntry {
431431
details,
432-
extra_derives,
433-
extra_attrs,
432+
extra_derives: type_patch.derives,
433+
extra_attrs: type_patch.attrs,
434434
}
435435
}
436436

@@ -446,10 +446,10 @@ impl TypeEntryNewtype {
446446
let rename = None;
447447
let description = metadata_description(metadata);
448448

449-
let (name, extra_derives, extra_attrs) = type_patch(type_space, name);
449+
let type_patch = TypePatch::new(type_space, name);
450450

451451
let details = TypeEntryDetails::Newtype(Self {
452-
name,
452+
name: type_patch.name,
453453
rename,
454454
description,
455455
default: None,
@@ -462,8 +462,8 @@ impl TypeEntryNewtype {
462462

463463
TypeEntry {
464464
details,
465-
extra_derives,
466-
extra_attrs,
465+
extra_derives: type_patch.derives,
466+
extra_attrs: type_patch.attrs,
467467
}
468468
}
469469

@@ -479,10 +479,10 @@ impl TypeEntryNewtype {
479479
let rename = None;
480480
let description = metadata_description(metadata);
481481

482-
let (name, extra_derives, extra_attrs) = type_patch(type_space, name);
482+
let type_patch = TypePatch::new(type_space, name);
483483

484484
let details = TypeEntryDetails::Newtype(Self {
485-
name,
485+
name: type_patch.name,
486486
rename,
487487
description,
488488
default: None,
@@ -495,8 +495,8 @@ impl TypeEntryNewtype {
495495

496496
TypeEntry {
497497
details,
498-
extra_derives,
499-
extra_attrs,
498+
extra_derives: type_patch.derives,
499+
extra_attrs: type_patch.attrs,
500500
}
501501
}
502502

@@ -518,10 +518,10 @@ impl TypeEntryNewtype {
518518
pattern,
519519
} = validation.clone();
520520

521-
let (name, extra_derives, extra_attrs) = type_patch(type_space, name);
521+
let type_patch = TypePatch::new(type_space, name);
522522

523523
let details = TypeEntryDetails::Newtype(Self {
524-
name,
524+
name: type_patch.name,
525525
rename,
526526
description,
527527
default: None,
@@ -536,8 +536,8 @@ impl TypeEntryNewtype {
536536

537537
TypeEntry {
538538
details,
539-
extra_derives,
540-
extra_attrs,
539+
extra_derives: type_patch.derives,
540+
extra_attrs: type_patch.attrs,
541541
}
542542
}
543543
}

typify-impl/src/util.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -835,21 +835,33 @@ pub(crate) fn get_type_name(type_name: &Name, metadata: &Option<Box<Metadata>>)
835835
Some(sanitize(&name, Case::Pascal))
836836
}
837837

838-
/// Check for patches which include potential type renames and additional
839-
/// derive macros.
840-
pub(crate) fn type_patch(
841-
type_space: &TypeSpace,
842-
type_name: String,
843-
) -> (String, BTreeSet<String>, BTreeSet<String>) {
844-
match type_space.settings.patch.get(&type_name) {
845-
None => (type_name, Default::default(), Default::default()),
846-
847-
Some(patch) => {
848-
let name = patch.rename.clone().unwrap_or(type_name);
849-
let derives = patch.derives.iter().cloned().collect();
850-
let attrs = patch.attrs.iter().cloned().collect();
851-
852-
(name, derives, attrs)
838+
pub(crate) struct TypePatch {
839+
pub name: String,
840+
pub derives: BTreeSet<String>,
841+
pub attrs: BTreeSet<String>,
842+
}
843+
844+
impl TypePatch {
845+
/// Creates a new TypePatch by resolving patches for the given type name.
846+
pub fn new(type_space: &TypeSpace, type_name: String) -> Self {
847+
match type_space.settings.patch.get(&type_name) {
848+
None => Self {
849+
name: type_name,
850+
derives: Default::default(),
851+
attrs: Default::default(),
852+
},
853+
854+
Some(patch) => {
855+
let name = patch.rename.clone().unwrap_or(type_name);
856+
let derives = patch.derives.iter().cloned().collect();
857+
let attrs = patch.attrs.iter().cloned().collect();
858+
859+
Self {
860+
name,
861+
derives,
862+
attrs,
863+
}
864+
}
853865
}
854866
}
855867
}

0 commit comments

Comments
 (0)