Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding impl Display for enum #3391

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
meta = { "breaking" = false, "bug" = false, "tada" = false }
author = "rcoh"

[[smithy-rs]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this also impacts the SDK, we should add a duplicate entry under aws-sdk-rust

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"
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ data class InfallibleEnumType(
*preludeScope,
"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)""")
},
)
}
}

Expand Down Expand Up @@ -141,6 +166,17 @@ data class InfallibleEnumType(
rust("&self.0")
}
}
rustTemplate(
"""
impl #{Display} for $UnknownVariantValue {
fn fmt(&self, f: &mut #{Fmt}::Formatter) -> #{Fmt}::Result {
write!(f, "{}", self.0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed the formatting.

}
}
""",
"Display" to RuntimeType.Display,
"Fmt" to RuntimeType.stdFmt,
)
}
}

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