-
Notifications
You must be signed in to change notification settings - Fork 180
/
ConstrainedMapGenerator.kt
157 lines (143 loc) · 6.4 KB
/
ConstrainedMapGenerator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rust.codegen.server.smithy.generators
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.shapes.UnionShape
import software.amazon.smithy.model.traits.LengthTrait
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.RustMetadata
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.Visibility
import software.amazon.smithy.rust.codegen.core.rustlang.docs
import software.amazon.smithy.rust.codegen.core.rustlang.documentShape
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.util.expectTrait
import software.amazon.smithy.rust.codegen.server.smithy.PubCrateConstraintViolationSymbolProvider
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
/**
* [ConstrainedMapGenerator] generates a wrapper tuple newtype holding a constrained `std::collections::HashMap`.
* This type can be built from unconstrained values, yielding a `ConstraintViolation` when the input does not satisfy
* the constraints.
*
* The [`length` trait] is the only constraint trait applicable to map shapes.
*
* If [unconstrainedSymbol] is provided, the `MaybeConstrained` trait is implemented for the constrained type, using the
* [unconstrainedSymbol]'s associated type as the associated type for the trait.
*
* [`length` trait]: https://awslabs.github.io/smithy/1.0/spec/core/constraint-traits.html#length-trait
*/
class ConstrainedMapGenerator(
val codegenContext: ServerCodegenContext,
val writer: RustWriter,
val shape: MapShape,
private val unconstrainedSymbol: Symbol? = null,
) {
private val model = codegenContext.model
private val constrainedShapeSymbolProvider = codegenContext.constrainedShapeSymbolProvider
private val publicConstrainedTypes = codegenContext.settings.codegenConfig.publicConstrainedTypes
private val constraintViolationSymbolProvider =
with(codegenContext.constraintViolationSymbolProvider) {
if (publicConstrainedTypes) {
this
} else {
PubCrateConstraintViolationSymbolProvider(this)
}
}
private val symbolProvider = codegenContext.symbolProvider
fun render() {
// The `length` trait is the only constraint trait applicable to map shapes.
val lengthTrait = shape.expectTrait<LengthTrait>()
val name = constrainedShapeSymbolProvider.toSymbol(shape).name
val inner = "std::collections::HashMap<#{KeySymbol}, #{ValueSymbol}>"
val constraintViolation = constraintViolationSymbolProvider.toSymbol(shape)
val constrainedTypeVisibility = Visibility.publicIf(publicConstrainedTypes, Visibility.PUBCRATE)
val constrainedTypeMetadata = RustMetadata(
Attribute.Derives(setOf(RuntimeType.Debug, RuntimeType.Clone, RuntimeType.PartialEq)),
visibility = constrainedTypeVisibility,
)
val codegenScope = arrayOf(
"KeySymbol" to constrainedShapeSymbolProvider.toSymbol(model.expectShape(shape.key.target)),
"ValueSymbol" to constrainedShapeSymbolProvider.toSymbol(model.expectShape(shape.value.target)),
"From" to RuntimeType.From,
"TryFrom" to RuntimeType.TryFrom,
"ConstraintViolation" to constraintViolation,
)
writer.documentShape(shape, model)
writer.docs(rustDocsConstrainedTypeEpilogue(name))
constrainedTypeMetadata.render(writer)
writer.rustTemplate("struct $name(pub(crate) $inner);", *codegenScope)
if (constrainedTypeVisibility == Visibility.PUBCRATE) {
Attribute.AllowUnused.render(writer)
}
writer.rustTemplate(
"""
impl $name {
/// ${rustDocsInnerMethod(inner)}
pub fn inner(&self) -> &$inner {
&self.0
}
/// ${rustDocsIntoInnerMethod(inner)}
pub fn into_inner(self) -> $inner {
self.0
}
}
impl #{TryFrom}<$inner> for $name {
type Error = #{ConstraintViolation};
/// ${rustDocsTryFromMethod(name, inner)}
fn try_from(value: $inner) -> Result<Self, Self::Error> {
let length = value.len();
if ${lengthTrait.rustCondition("length")} {
Ok(Self(value))
} else {
Err(#{ConstraintViolation}::Length(length))
}
}
}
impl #{From}<$name> for $inner {
fn from(value: $name) -> Self {
value.into_inner()
}
}
""",
*codegenScope,
)
val valueShape = model.expectShape(shape.value.target)
if (!publicConstrainedTypes &&
isValueConstrained(valueShape, model, symbolProvider) &&
valueShape !is StructureShape &&
valueShape !is UnionShape
) {
writer.rustTemplate(
"""
impl #{From}<$name> for #{FullyUnconstrainedSymbol} {
fn from(value: $name) -> Self {
value
.into_inner()
.into_iter()
.map(|(k, v)| (k, v.into()))
.collect()
}
}
""",
*codegenScope,
"FullyUnconstrainedSymbol" to symbolProvider.toSymbol(shape),
)
}
if (unconstrainedSymbol != null) {
writer.rustTemplate(
"""
impl #{ConstrainedTrait} for $name {
type Unconstrained = #{UnconstrainedSymbol};
}
""",
"ConstrainedTrait" to RuntimeType.ConstrainedTrait,
"UnconstrainedSymbol" to unconstrainedSymbol,
)
}
}
}