Skip to content

Commit

Permalink
Adding impl Display for enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince Kumar Maurya committed Feb 6, 2024
1 parent 8873666 commit e95d5d1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
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]]
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)
}
}
""",
"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

0 comments on commit e95d5d1

Please sign in to comment.