Skip to content

Commit

Permalink
Add support for sparse datastructures & blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Jul 16, 2024
1 parent df6f013 commit 971662e
Show file tree
Hide file tree
Showing 7 changed files with 640 additions and 437 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.rust.codegen.serde

import software.amazon.smithy.model.neighbor.Walker
import software.amazon.smithy.model.shapes.Shape
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.Feature
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
import software.amazon.smithy.rust.codegen.core.util.hasTrait
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator

val SerdeFeature = Feature("serde", false, listOf("dep:serde"))
val Module =
RustModule.public(
"serde_impl",
additionalAttributes = listOf(Attribute.featureGate(SerdeFeature.name)),
documentationOverride = "Implementations of `serde` for model types",
)

class ClientSerdeDecorator : ClientCodegenDecorator {
override val name: String = "ClientSerdeDecorator"
override val order: Byte = 0

override fun extras(
codegenContext: ClientCodegenContext,
rustCrate: RustCrate,
) {
rustCrate.mergeFeature(SerdeFeature)
val generator = SerializeImplGenerator(codegenContext)
rustCrate.withModule(Module) {
serializationRoots(codegenContext).forEach {
generator.generateRootSerializerForShape(
it,
)(this)
}
addDependency(SupportStructures.serializeRedacted().toSymbol())
addDependency(SupportStructures.serializeUnredacted().toSymbol())
}
}
}

class ServerSerdeDecorator : ServerCodegenDecorator {
override val name: String = "ServerSerdeDecorator"
override val order: Byte = 0

override fun extras(
codegenContext: ServerCodegenContext,
rustCrate: RustCrate,
) {
rustCrate.mergeFeature(SerdeFeature)
val generator = SerializeImplGenerator(codegenContext)
rustCrate.withModule(Module) {
serializationRoots(codegenContext).forEach {
generator.generateRootSerializerForShape(
it,
)(this)
}
addDependency(SupportStructures.serializeRedacted().toSymbol())
addDependency(SupportStructures.serializeUnredacted().toSymbol())
}
}
}

/**
* All entry points for serialization in the service closure.
*/
fun serializationRoots(ctx: CodegenContext): List<Shape> {
val serviceShape = ctx.serviceShape
val walker = Walker(ctx.model)
return walker.walkShapes(serviceShape).filter { it.hasTrait<SerdeTrait>() }
}
Loading

0 comments on commit 971662e

Please sign in to comment.