-
Notifications
You must be signed in to change notification settings - Fork 193
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
Add support for unnamed enumerations #2
Conversation
Unnamed enumerations are supported by creating a "newtype" that wraps the string. The newtype provides the valid values as a list for convenience.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question, LGTM otherwise
writer.write("pub struct $enumName(String);") | ||
writer.rustBlock("impl $enumName") { | ||
writer.rustBlock("pub fn as_str(&self) -> &str") { | ||
write("&self.0") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this little bit of wizardry actually do? I don't know that I've seen that syntax before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.0
? it's just what the member is named in tuple structs (what we generate for unamed enums)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
EnumGenerator.deriveName(null, "Signal: creation is in progress") shouldBe("SignalCreationIsInProgress") | ||
fun `it generates unamed enums`() { | ||
val model = """ | ||
namespace test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not particular to this CR or the Rust SDK, but it strikes me that any syntactical style of the emitted codegen is likely to age as languages and styles evolve. I think I recall that the Go SDK performs code formatting as a post processing step. The value to this becomes clear to me, in that the style formatting tool only needs to be updated to match evolving styles, rather that numerous finicky changes to codegen strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah our codegenerator runs cargo format
as part of codegen so our output code is consistently formatted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although the code is more designed to always compile vs. being legible. (as in, it doesn't actually use imports, it refers to everything with a fully qualified name, etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and in case it wasn't clear, the literal string here is smithy, not Rust code that we're validating against.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do that with clang-format on C++ code as well, and autopep8 for python. Highly recommend ignoring style during generation.
Description of changes:
Unnamed enumerations are supported by creating a "newtype" that wraps the string. The newtype provides the valid values as a list for convenience.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.