Skip to content

Commit

Permalink
Merge branch 'main' into ysaito/replace-bool-parameter-with-enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Velfi authored Oct 20, 2022
2 parents e084ccd + e74ecf3 commit 3dd997d
Showing 1 changed file with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,15 @@ class HttpBindingGenerator(
}

private fun RustWriter.renderHeaders(httpBinding: HttpBinding) {
check(httpBinding.location == HttpLocation.HEADER)
val memberShape = httpBinding.member
val memberType = model.expectShape(memberShape.target)
val targetShape = model.expectShape(memberShape.target)
val memberSymbol = symbolProvider.toSymbol(memberShape)
val memberName = symbolProvider.toMemberName(memberShape)
ifSet(memberType, memberSymbol, "&input.$memberName") { field ->
val isListHeader = memberType is CollectionShape
listForEach(memberType, field) { innerField, targetId ->

ifSet(targetShape, memberSymbol, "&input.$memberName") { field ->
val isListHeader = targetShape is CollectionShape
listForEach(targetShape, field) { innerField, targetId ->
val innerMemberType = model.expectShape(targetId)
if (innerMemberType.isPrimitive()) {
val encoder = CargoDependency.SmithyTypes(runtimeConfig).asType().member("primitive::Encoder")
Expand All @@ -491,9 +493,14 @@ class HttpBindingGenerator(
"""
let header_value = $safeName;
let header_value = http::header::HeaderValue::try_from(&*header_value).map_err(|err| {
#{build_error}::InvalidField { field: "$memberName", details: format!("`{}` cannot be used as a header value: {}", &${
memberShape.redactIfNecessary(model, "header_value")
}, err)}
#{build_error}::InvalidField {
field: "$memberName",
details: format!(
"`{}` cannot be used as a header value: {}",
&${memberShape.redactIfNecessary(model, "header_value")},
err,
)
}
})?;
builder = builder.header("${httpBinding.locationName}", header_value);
""",
Expand All @@ -505,35 +512,34 @@ class HttpBindingGenerator(
}

private fun RustWriter.renderPrefixHeader(httpBinding: HttpBinding) {
check(httpBinding.location == HttpLocation.PREFIX_HEADERS)
val memberShape = httpBinding.member
val memberType = model.expectShape(memberShape.target)
val targetShape = model.expectShape(memberShape.target, MapShape::class.java)
val memberSymbol = symbolProvider.toSymbol(memberShape)
val memberName = symbolProvider.toMemberName(memberShape)
val target = when (memberType) {
is CollectionShape -> model.expectShape(memberType.member.target)
is MapShape -> model.expectShape(memberType.value.target)
else -> UNREACHABLE("unexpected member for prefix headers: $memberType")
}
ifSet(memberType, memberSymbol, "&input.$memberName") { field ->
val listHeader = memberType is CollectionShape
val valueTargetShape = model.expectShape(targetShape.value.target)

ifSet(targetShape, memberSymbol, "&input.$memberName") { field ->
rustTemplate(
"""
for (k, v) in $field {
use std::str::FromStr;
let header_name = http::header::HeaderName::from_str(&format!("{}{}", "${httpBinding.locationName}", &k)).map_err(|err| {
#{build_error}::InvalidField { field: "$memberName", details: format!("`{}` cannot be used as a header name: {}", k, err)}
})?;
let header_value = ${headerFmtFun(this, target, memberShape, "v", listHeader)};
let header_value = ${headerFmtFun(this, valueTargetShape, memberShape, "v", isListHeader = false)};
let header_value = http::header::HeaderValue::try_from(&*header_value).map_err(|err| {
#{build_error}::InvalidField {
field: "$memberName",
details: format!("`{}` cannot be used as a header value: {}", ${
memberShape.redactIfNecessary(model, "v")
}, err)}
details: format!(
"`{}` cannot be used as a header value: {}",
${memberShape.redactIfNecessary(model, "v")},
err,
)
}
})?;
builder = builder.header(header_name, header_value);
}
""",
"build_error" to runtimeConfig.operationBuildError(),
)
Expand Down

0 comments on commit 3dd997d

Please sign in to comment.