Unverified Commit 014d2e00 authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Implement new JsonSerializerGenerator without document/operation support (#416)

* Rename JSON generators

* Implement new JsonSerializerGenerator without document/operation support

* CR feedback
parent 2b072e08
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -187,6 +187,8 @@ data class CargoDependency(
            "protocol-test-helpers", Local(runtimeConfig.relativePath), scope = DependencyScope.Dev
        )

        fun smithyJson(runtimeConfig: RuntimeConfig): CargoDependency =
            CargoDependency("${runtimeConfig.cratePrefix}-json", Local(runtimeConfig.relativePath))
        fun smithyXml(runtimeConfig: RuntimeConfig): CargoDependency =
            CargoDependency("${runtimeConfig.cratePrefix}-xml", Local(runtimeConfig.relativePath))

+7 −14
Original line number Diff line number Diff line
@@ -128,23 +128,16 @@ fun RustType.render(fullyQualified: Boolean = true): String {
 * Option<Instant>.contains(Instant) would return true.
 * Option<Instant>.contains(Blob) would return false.
 */
fun <T : RustType> RustType.contains(t: T): Boolean {
    if (t == this) {
        return true
    }

    return when (this) {
fun <T : RustType> RustType.contains(t: T): Boolean = when (this) {
    t -> true
    is RustType.Container -> this.member.contains(t)
    else -> false
}
}

inline fun <reified T : RustType.Container> RustType.stripOuter(): RustType {
    return when (this) {
inline fun <reified T : RustType.Container> RustType.stripOuter(): RustType = when (this) {
    is T -> this.member
    else -> this
}
}

/**
 * Meta information about a Rust construction (field, struct, or enum)
+10 −0
Original line number Diff line number Diff line
@@ -73,6 +73,16 @@ fun <T : CodeWriter> T.rust(
    this.write(contents, *args)
}

/**
 * Convenience wrapper that tells Intellij that the contents of this block are Rust
 */
fun <T : CodeWriter> T.rustInline(
    @Language("Rust", prefix = "macro_rules! foo { () =>  {{ ", suffix = "}}}") contents: String,
    vararg args: Any
) {
    this.writeInline(contents, *args)
}

/**
 * Sibling method to [rustBlock] that enables `#{variablename}` style templating
 */
+7 −5
Original line number Diff line number Diff line
@@ -65,16 +65,18 @@ data class RuntimeType(val name: String?, val dependency: RustDependency?, val n
            namespace = "${runtimeConfig.cratePrefix}_types::retry"
        )

        val Default: RuntimeType = RuntimeType("Default", dependency = null, namespace = "std::default")
        val From = RuntimeType("From", dependency = null, namespace = "std::convert")
        val AsRef = RuntimeType("AsRef", dependency = null, namespace = "std::convert")
        val std = RuntimeType(null, dependency = null, namespace = "std")
        val stdfmt = std.member("fmt")
        val StdError = RuntimeType("Error", dependency = null, namespace = "std::error")

        val AsRef = RuntimeType("AsRef", dependency = null, namespace = "std::convert")
        val ByteSlab = RuntimeType("Vec<u8>", dependency = null, namespace = "std::vec")
        val Clone = std.member("clone::Clone")
        val Debug = stdfmt.member("Debug")
        val Default: RuntimeType = RuntimeType("Default", dependency = null, namespace = "std::default")
        val From = RuntimeType("From", dependency = null, namespace = "std::convert")
        val PartialEq = std.member("cmp::PartialEq")
        val Clone = std.member("clone::Clone")
        val StdError = RuntimeType("Error", dependency = null, namespace = "std::error")
        val String = RuntimeType("String", dependency = null, namespace = "std::string")

        fun Instant(runtimeConfig: RuntimeConfig) =
            RuntimeType("Instant", CargoDependency.SmithyTypes(runtimeConfig), "${runtimeConfig.cratePrefix}_types")
+4 −4
Original line number Diff line number Diff line
@@ -36,8 +36,8 @@ import software.amazon.smithy.rust.codegen.smithy.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.smithy.generators.operationBuildError
import software.amazon.smithy.rust.codegen.smithy.locatedIn
import software.amazon.smithy.rust.codegen.smithy.meta
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.JsonParserGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.JsonSerializerGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.SerdeJsonParserGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.SerdeJsonSerializerGenerator
import software.amazon.smithy.rust.codegen.smithy.rustType
import software.amazon.smithy.rust.codegen.smithy.traits.InputBodyTrait
import software.amazon.smithy.rust.codegen.smithy.traits.OutputBodyTrait
@@ -198,7 +198,7 @@ class BasicAwsJsonGenerator(
    }

    override fun RustWriter.body(self: String, operationShape: OperationShape): BodyMetadata {
        val generator = JsonSerializerGenerator(protocolConfig)
        val generator = SerdeJsonSerializerGenerator(protocolConfig)
        val serializer = generator.operationSerializer(operationShape)
        serializer?.also { sym ->
            rustTemplate(
@@ -214,7 +214,7 @@ class BasicAwsJsonGenerator(
        val outputShape = operationIndex.getOutput(operationShape).get()
        val errorSymbol = operationShape.errorSymbol(symbolProvider)
        val jsonErrors = RuntimeType.awsJsonErrors(protocolConfig.runtimeConfig)
        val generator = JsonParserGenerator(protocolConfig)
        val generator = SerdeJsonParserGenerator(protocolConfig)

        fromResponseFun(implBlockWriter, operationShape) {
            rustBlock("if #T::is_error(&response)", jsonErrors) {
Loading