From 6362a0df10e1714a9dd4189139ae6c5f28df86e3 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 | 1 + 2 files changed, 19 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..b0b4a01d28 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,7 @@ class ConstraintsTest { primitiveBoolean.isDirectlyConstrained(symbolProvider) shouldBe false } + // TODO(#3895): Move tests that use `generateAndCompileServer` into `constraints.smithy` once issue is resolved private fun generateAndCompileServer( model: Model, pubConstraints: Boolean = true,