Skip to content

Commit

Permalink
Use Attribute rather than string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Barber committed Feb 13, 2023
1 parent 943b76e commit e0a4d83
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import software.amazon.smithy.model.neighbor.Walker
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.model.traits.PatternTrait
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWords
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
Expand Down Expand Up @@ -207,11 +208,11 @@ class ServerServiceGeneratorV2(
}
}

var missingOperationsUnusedMut = ""
var unusedExpected = ""
var missingOperationsUnusedMut = writable("")
var unusedExpected = writable("")
if (operations.isEmpty()) {
missingOperationsUnusedMut = "##[allow(unused_mut)]"
unusedExpected = "##[allow(unused_variables)]"
missingOperationsUnusedMut = writable { Attribute.AllowUnusedMut.render(this) }
unusedExpected = writable { Attribute.AllowUnusedVariables.render(this) }
}

rustTemplate(
Expand All @@ -225,15 +226,15 @@ class ServerServiceGeneratorV2(
pub fn build(self) -> Result<$serviceName<#{SmithyHttpServer}::routing::Route<$builderBodyGenericTypeName>>, MissingOperationsError>
{
let router = {
$missingOperationsUnusedMut
#{MissingOperationsUnusedMut:W}
let mut $missingOperationsVariableName = std::collections::HashMap::new();
#{NullabilityChecks:W}
if !$missingOperationsVariableName.is_empty() {
return Err(MissingOperationsError {
operation_names2setter_methods: $missingOperationsVariableName,
});
}
$unusedExpected
#{UnusedExpected:W}
let $expectMessageVariableName = "this should never panic since we are supposed to check beforehand that a handler has been registered for this operation; please file a bug report under https://github.com/awslabs/smithy-rs/issues";
#{PatternInitializations:W}
Expand All @@ -250,6 +251,8 @@ class ServerServiceGeneratorV2(
"RoutesArrayElements" to routesArrayElements,
"SmithyHttpServer" to smithyHttpServer,
"PatternInitializations" to patternInitializations(),
"MissingOperationsUnusedMut" to missingOperationsUnusedMut,
"UnusedExpected" to unusedExpected,
)
}

Expand Down Expand Up @@ -330,20 +333,20 @@ class ServerServiceGeneratorV2(
private fun builder(): Writable = writable {
val builderGenerics = listOf(builderBodyGenericTypeName, builderPluginGenericTypeName).joinToString(", ")
var allBuilderFields = builderFields + "plugin: $builderPluginGenericTypeName"
var allowDeadCodePlugin = ""
var allowDeadCodePlugin = writable("")

// With no operations there is no use of the `Body` type variable and `plugin: Plugin` becomes dead code
if (operations.isEmpty()) {
allBuilderFields += "_body: std::marker::PhantomData<$builderBodyGenericTypeName>"
allowDeadCodePlugin = "##[allow(dead_code)]"
allowDeadCodePlugin = writable { Attribute.AllowDeadCode.render(this) }
}

rustTemplate(
"""
/// The service builder for [`$serviceName`].
///
/// Constructed via [`$serviceName::builder_with_plugins`] or [`$serviceName::builder_without_plugins`].
$allowDeadCodePlugin
#{AllowDeadCodePlugin:W}
pub struct $builderName<$builderGenerics> {
${allBuilderFields.joinToString(",")}
}
Expand All @@ -361,6 +364,7 @@ class ServerServiceGeneratorV2(
"Setters" to builderSetters(),
"BuildMethod" to buildMethod(),
"BuildUncheckedMethod" to buildUncheckedMethod(),
"AllowDeadCodePlugin" to allowDeadCodePlugin,
*codegenScope,
)
}
Expand Down

0 comments on commit e0a4d83

Please sign in to comment.