From 05c6e78604a0fddb35163c03f066c1bbd63de078 Mon Sep 17 00:00:00 2001 From: Fahad Zubair Date: Tue, 29 Oct 2024 17:15:31 +0000 Subject: [PATCH] Refactor `constrainedValueTypeIsNotFinalType` to make it a bit more readable --- .../UnconstrainedCollectionGenerator.kt | 37 +++++++++---------- .../codegen/server/smithy/ConstraintsTest.kt | 2 + 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/UnconstrainedCollectionGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/UnconstrainedCollectionGenerator.kt index be3f3f7300..7dbf2cc1b2 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/UnconstrainedCollectionGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/UnconstrainedCollectionGenerator.kt @@ -153,7 +153,7 @@ class UnconstrainedCollectionGenerator( val finalType = if (constrainedValueTypeIsNotFinalType) { - constrainedShapeSymbolProvider.toSymbol(shape.member) + symbolProvider.toSymbol(shape.member) } else { constrainedMemberSymbol } @@ -161,24 +161,23 @@ class UnconstrainedCollectionGenerator( val constrainValueWritable = writable { conditionalBlock("inner.map(|inner| ", ").transpose()", constrainedMemberSymbol.isOptional()) { - rustTemplate( - """ - inner.try_into() - #{FinalMapping} - .map_err(|inner_violation| (idx, inner_violation)) - """, - "FinalMapping" to - writable { - if (constrainedValueTypeIsNotFinalType) { - rustTemplate( - ".map(|c : #{ConstrainedMemberSymbol}| c.into())", - "ConstrainedMemberSymbol" to constrainedMemberSymbol, - ) - } else { - rust("") - } - }, - ) + if (constrainedValueTypeIsNotFinalType) { + rustTemplate( + """ + #{ConstrainedMemberSymbol}::try_from(inner) + .map(|inner_constrained| inner_constrained.into()) + .map_err(|inner_violation| (idx, inner_violation)) + """, + "ConstrainedMemberSymbol" to constrainedMemberSymbol, + ) + } else { + rustTemplate( + """ + inner.try_into() + .map_err(|inner_violation| (idx, inner_violation)) + """, + ) + } } } diff --git a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstraintsTest.kt b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstraintsTest.kt index da4967c9c0..6c3d6ee296 100644 --- a/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstraintsTest.kt +++ b/codegen-server/src/test/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ConstraintsTest.kt @@ -223,6 +223,8 @@ class ConstraintsTest { primitiveBoolean.isDirectlyConstrained(symbolProvider) shouldBe false } + // TODO: Once issue #3895 is resolved, all tests that use `generateAndCompileServer` + // should be moved to `constraints.smithy`. private fun generateAndCompileServer( model: Model, pubConstraints: Boolean = true,