Skip to content

Commit

Permalink
Refactor constrainedValueTypeIsNotFinalType to make it a bit more r…
Browse files Browse the repository at this point in the history
…eadable
  • Loading branch information
Fahad Zubair committed Oct 29, 2024
1 parent 11a20b5 commit 05c6e78
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,31 @@ class UnconstrainedCollectionGenerator(

val finalType =
if (constrainedValueTypeIsNotFinalType) {
constrainedShapeSymbolProvider.toSymbol(shape.member)
symbolProvider.toSymbol(shape.member)
} else {
constrainedMemberSymbol
}

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))
""",
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 05c6e78

Please sign in to comment.