Adding impl Display for enum (#3391)

## Motivation and Context
This change address the
[#3335](https://github.com/smithy-lang/smithy-rs/issues/3336)

## Description
Added Display trait to the Enum. 

## Testing
Updated the
[ClientEnumGeneratorTest.kt](https://github.com/smithy-lang/smithy-rs/blob/main/codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ClientEnumGeneratorTest.kt)
which test the ```impl Display``` for the enums.

## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [x] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates
----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

Co-authored-by: Prince Kumar Maurya <princekumarmaurya06@gmail.com>
This commit is contained in:
Prince Kumar Maurya 2024-02-07 08:55:11 -08:00 committed by GitHub
parent 8873666b0e
commit 4187b639f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 0 deletions

View File

@ -28,3 +28,15 @@ message = "Add `try_into_http1x` and `try_from_http1x` to Request and Response c
references = ["aws-sdk-rust#977", "smithy-rs#3365", "smithy-rs#3373"] references = ["aws-sdk-rust#977", "smithy-rs#3365", "smithy-rs#3373"]
meta = { "breaking" = false, "bug" = false, "tada" = false } meta = { "breaking" = false, "bug" = false, "tada" = false }
author = "rcoh" author = "rcoh"
[[smithy-rs]]
message = "Added impl `Display` to Enums."
references = ["smithy-rs#3336","smithy-rs#3391"]
meta = { "breaking" = false, "tada" = false, "bug" = false , "target" = "client" }
author = "iampkmone"
[[aws-sdk-rust]]
message = "Added impl `Display` to Enums."
references = ["smithy-rs#3336", "smithy-rs#3391"]
meta = { "breaking" = false, "tada" = false, "bug" = false}
author = "iampkmone"

View File

@ -101,6 +101,31 @@ data class InfallibleEnumType(
*preludeScope, *preludeScope,
"UnknownVariantError" to unknownVariantError(), "UnknownVariantError" to unknownVariantError(),
) )
rustTemplate(
"""
impl #{Display} for ${context.enumName} {
fn fmt(&self, f: &mut #{Fmt}::Formatter) -> #{Fmt}::Result {
match self {
#{matchArms}
}
}
}
""",
"Display" to RuntimeType.Display,
"Fmt" to RuntimeType.stdFmt,
"matchArms" to
writable {
context.sortedMembers.forEach { member ->
rust(
"""
${context.enumName}::${member.derivedName()} => write!(f, ${member.value.dq()}),
""",
)
}
rust("""${context.enumName}::Unknown(value) => write!(f, "{}", value)""")
},
)
} }
} }
@ -141,6 +166,17 @@ data class InfallibleEnumType(
rust("&self.0") rust("&self.0")
} }
} }
rustTemplate(
"""
impl #{Display} for $UnknownVariantValue {
fn fmt(&self, f: &mut #{Fmt}::Formatter) -> #{Fmt}::Result {
write!(f, "{}", self.0)
}
}
""",
"Display" to RuntimeType.Display,
"Fmt" to RuntimeType.stdFmt,
)
} }
} }

View File

@ -94,6 +94,8 @@ class ClientEnumGeneratorTest {
""" """
assert_eq!(format!("{:?}", SomeEnum::Foo), "Foo"); assert_eq!(format!("{:?}", SomeEnum::Foo), "Foo");
assert_eq!(format!("{:?}", SomeEnum::Bar), "Bar"); assert_eq!(format!("{:?}", SomeEnum::Bar), "Bar");
assert_eq!(format!("{}", SomeEnum::Foo), "Foo");
assert_eq!(SomeEnum::Bar.to_string(), "Bar");
assert_eq!( assert_eq!(
format!("{:?}", SomeEnum::from("Baz")), format!("{:?}", SomeEnum::from("Baz")),
"Unknown(UnknownVariantValue(\"Baz\"))" "Unknown(UnknownVariantValue(\"Baz\"))"
@ -161,12 +163,14 @@ class ClientEnumGeneratorTest {
let instance = InstanceType::T2Micro; let instance = InstanceType::T2Micro;
assert_eq!(instance.as_str(), "t2.micro"); assert_eq!(instance.as_str(), "t2.micro");
assert_eq!(InstanceType::from("t2.nano"), InstanceType::T2Nano); assert_eq!(InstanceType::from("t2.nano"), InstanceType::T2Nano);
assert_eq!(instance.to_string(), "t2.micro");
// round trip unknown variants: // round trip unknown variants:
assert_eq!( assert_eq!(
InstanceType::from("other"), InstanceType::from("other"),
InstanceType::Unknown(crate::primitives::sealed_enum_unknown::UnknownVariantValue("other".to_owned())) InstanceType::Unknown(crate::primitives::sealed_enum_unknown::UnknownVariantValue("other".to_owned()))
); );
assert_eq!(InstanceType::from("other").as_str(), "other"); assert_eq!(InstanceType::from("other").as_str(), "other");
assert_eq!(InstanceType::from("other").to_string(), "other");
""", """,
) )
} }