Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ data class InfallibleEnumType(
writable {
rustTemplate(
"""
impl<T> #{From}<T> for ${context.enumName} where T: #{AsRef}<str> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This would be a breaking change.

Also see #3886

fn from(s: T) -> Self {
${context.enumName}(s.as_ref().to_owned())
impl #{From}<&str> for ${context.enumName} {
fn from(s: &str) -> Self {
${context.enumName}(s.to_owned())
}
}
""",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,40 @@ class ClientEnumGeneratorTest {
expectMatchExpressionCompiles(modelV2, "test#SomeEnum", variant3AsVariant3)
}

@Test
fun `unnamed enums implement AsRef`() {
val model =
"""
namespace test
@enum([
{ value: "Foo" },
{ value: "Bar" },
])
string SomeEnum
""".asSmithyModel()

val shape = model.lookup<StringShape>("test#SomeEnum")
val context = testClientCodegenContext(model)
val project = TestWorkspace.testProject(context.symbolProvider)
project.moduleFor(shape) {
ClientEnumGenerator(context, shape, emptyList()).render(this)
unitTest(
"unnamed_enums_implement_asref",
"""
let foo = SomeEnum::from("Foo");
let foo_ref: &str = foo.as_ref();
assert_eq!(foo_ref, "Foo");

fn takes_asref(s: impl AsRef<str>) -> String {
s.as_ref().to_owned()
}
assert_eq!(takes_asref(SomeEnum::from("Bar")), "Bar");
""",
)
}
project.compileAndTest()
}

@Test
fun `impl debug for non-sensitive enum should implement the derived debug trait`() {
val model =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ open class EnumGenerator(
rust("&self.0")
},
)
rustTemplate(
"""
impl #{AsRef}<str> for ${context.enumName} {
fn as_ref(&self) -> &str {
self.as_str()
}
}
""",
*preludeScope,
)
// impl From<str> for Blah { ... }
enumType.implFromForStrForUnnamedEnum(context)(this)
// impl FromStr for Blah { ... }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,40 @@ class EnumGeneratorTest {
project.compileAndTest()
}

@Test
fun `unnamed enums implement AsRef`() {
val model =
"""
namespace test
@enum([
{ value: "Foo" },
{ value: "Bar" },
])
string SomeEnum
""".asSmithyModel()

val shape = model.lookup<StringShape>("test#SomeEnum")
val provider = testSymbolProvider(model)
val project = TestWorkspace.testProject(provider)
project.moduleFor(shape) {
renderEnum(model, provider, shape)
unitTest(
"unnamed_enums_implement_asref",
"""
let foo = SomeEnum::from("Foo");
let foo_ref: &str = foo.as_ref();
assert_eq!(foo_ref, "Foo");

fn takes_asref(s: impl AsRef<str>) -> String {
s.as_ref().to_owned()
}
assert_eq!(takes_asref(SomeEnum::from("Bar")), "Bar");
""",
)
}
project.compileAndTest()
}

@Test
fun `it generates unnamed enums`() {
val model =
Expand Down