in codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/Instantiator.kt [72:143]
fun render(
writer: RustWriter,
shape: Shape,
arg: Node,
ctx: Ctx = Ctx(lowercaseMapKeys = false, streaming = false, builder = false)
) {
when (shape) {
// Compound Shapes
is StructureShape -> renderStructure(writer, shape, arg as ObjectNode, ctx.copy(builder = true))
is UnionShape -> renderUnion(writer, shape, arg as ObjectNode, ctx)
// Collections
is ListShape -> renderList(writer, shape, arg as ArrayNode, ctx)
is MapShape -> renderMap(writer, shape, arg as ObjectNode, ctx)
is SetShape -> renderSet(writer, shape, arg as ArrayNode, ctx)
// Members, supporting potentially optional members
is MemberShape -> renderMember(writer, shape, arg, ctx)
// Wrapped Shapes
is TimestampShape -> writer.write(
"#T::from_secs(${(arg as NumberNode).value})",
RuntimeType.DateTime(runtimeConfig)
)
/**
* ```rust
* Blob::new("arg")
* ```
*/
is BlobShape -> if (ctx.streaming) {
writer.write(
"#T::from_static(b${(arg as StringNode).value.dq()})",
RuntimeType.byteStream(runtimeConfig)
)
} else {
writer.write(
"#T::new(${(arg as StringNode).value.dq()})",
RuntimeType.Blob(runtimeConfig)
)
}
// Simple Shapes
is StringShape -> renderString(writer, shape, arg as StringNode)
is NumberShape -> when (arg) {
is StringNode -> {
val numberSymbol = symbolProvider.toSymbol(shape)
// support Smithy custom values, such as Infinity
writer.rust(
"""<#T as #T>::parse_smithy_primitive(${arg.value.dq()}).expect("invalid string for number")""",
numberSymbol,
CargoDependency.SmithyTypes(runtimeConfig).asType().member("primitive::Parse")
)
}
is NumberNode -> writer.write(arg.value)
}
is BooleanShape -> writer.write(arg.asBooleanNode().get().toString())
is DocumentShape -> writer.rustBlock("") {
val smithyJson = CargoDependency.smithyJson(runtimeConfig).asType()
rustTemplate(
"""
let json_bytes = br##"${Node.prettyPrintJson(arg)}"##;
let mut tokens = #{json_token_iter}(json_bytes).peekable();
#{expect_document}(&mut tokens).expect("well formed json")
""",
"expect_document" to smithyJson.member("deserialize::token::expect_document"),
"json_token_iter" to smithyJson.member("deserialize::json_token_iter"),
)
}
else -> writer.writeWithNoFormatting("todo!() /* $shape $arg */")
}
}