Skip to content

Commit

Permalink
RustType renders types with wrong syntax (#3090)
Browse files Browse the repository at this point in the history
The correct syntax is `HashMap::<T, U>` instead of `HashMap<T, U>` and
so on.

----

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

---------

Signed-off-by: Daniele Ahmed <[email protected]>
  • Loading branch information
82marbag authored Oct 25, 2023
1 parent 12bed90 commit 08a533f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class FluentClientGeneratorTest {
fun `generate correct input docs`() {
val expectations = mapOf(
"listValue" to "list_value(impl Into<String>)",
"doubleListValue" to "double_list_value(Vec<String>)",
"mapValue" to "map_value(impl Into<String>, Vec<String>)",
"doubleListValue" to "double_list_value(Vec::<String>)",
"mapValue" to "map_value(impl Into<String>, Vec::<String>)",
"byteValue" to "byte_value(i8)",
)
expectations.forEach { (name, expect) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,29 @@ sealed class RustType {
*
* When formatted, the converted type will appear as such:
*
* | Type | Formatted |
* | -------------------------------------------------- | ------------------------------------------------------------------- |
* | RustType.Unit | () |
* | RustType.Bool | bool |
* | RustType.Float(32) | f32 |
* | RustType.Float(64) | f64 |
* | RustType.Integer(8) | i8 |
* | RustType.Integer(16) | i16 |
* | RustType.Integer(32) | i32 |
* | RustType.Integer(64) | i64 |
* | RustType.String | std::string::String |
* | RustType.Vec(RustType.String) | std::vec::Vec<std::string::String> |
* | RustType.Slice(RustType.String) | [std::string::String] |
* | RustType.HashMap(RustType.String, RustType.String) | std::collections::HashMap<std::string::String, std::string::String> |
* | RustType.HashSet(RustType.String) | std::vec::Vec<std::string::String> |
* | RustType.Reference("&", RustType.String) | &std::string::String |
* | RustType.Reference("&mut", RustType.String) | &mut std::string::String |
* | RustType.Reference("&'static", RustType.String) | &'static std::string::String |
* | RustType.Option(RustType.String) | std::option::Option<std::string::String> |
* | RustType.Box(RustType.String) | std::boxed::Box<std::string::String> |
* | RustType.Opaque("SoCool", "zelda_is") | zelda_is::SoCool |
* | RustType.Opaque("SoCool") | SoCool |
* | RustType.Dyn(RustType.Opaque("Foo", "foo")) | dyn foo::Foo |
* | Type | Formatted |
* | -------------------------------------------------- | ------------------------------------------------------------------- |
* | RustType.Unit | () |
* | RustType.Bool | bool |
* | RustType.Float(32) | f32 |
* | RustType.Float(64) | f64 |
* | RustType.Integer(8) | i8 |
* | RustType.Integer(16) | i16 |
* | RustType.Integer(32) | i32 |
* | RustType.Integer(64) | i64 |
* | RustType.String | std::string::String |
* | RustType.Vec(RustType.String) | std::vec::Vec::<std::string::String> |
* | RustType.Slice(RustType.String) | [std::string::String] |
* | RustType.HashMap(RustType.String, RustType.String) | std::collections::HashMap::<std::string::String, std::string::String>|
* | RustType.HashSet(RustType.String) | std::vec::Vec::<std::string::String> |
* | RustType.Reference("&", RustType.String) | &std::string::String |
* | RustType.Reference("&mut", RustType.String) | &mut std::string::String |
* | RustType.Reference("&'static", RustType.String) | &'static std::string::String |
* | RustType.Option(RustType.String) | std::option::Option<std::string::String> |
* | RustType.Box(RustType.String) | std::boxed::Box<std::string::String> |
* | RustType.Opaque("SoCool", "zelda_is") | zelda_is::SoCool |
* | RustType.Opaque("SoCool") | SoCool |
* | RustType.Dyn(RustType.Opaque("Foo", "foo")) | dyn foo::Foo |
*/
val writable = writable { rustInlineTemplate("#{this}", "this" to this@RustType) }

Expand Down Expand Up @@ -244,10 +244,10 @@ fun RustType.render(fullyQualified: Boolean = true): String {
is RustType.Float -> this.name
is RustType.Integer -> this.name
is RustType.String -> this.name
is RustType.Vec -> "${this.name}<${this.member.render(fullyQualified)}>"
is RustType.Vec -> "${this.name}::<${this.member.render(fullyQualified)}>"
is RustType.Slice -> "[${this.member.render(fullyQualified)}]"
is RustType.HashMap -> "${this.name}<${this.key.render(fullyQualified)}, ${this.member.render(fullyQualified)}>"
is RustType.HashSet -> "${this.name}<${this.member.render(fullyQualified)}>"
is RustType.HashMap -> "${this.name}::<${this.key.render(fullyQualified)}, ${this.member.render(fullyQualified)}>"
is RustType.HashSet -> "${this.name}::<${this.member.render(fullyQualified)}>"
is RustType.Reference -> {
if (this.lifetime == "&") {
"&${this.member.render(fullyQualified)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal class RustTypesTest {
fun `RustType_Vec_writable produces a template-compatible RuntimeType`() {
forInputExpectOutput(
RustType.Vec(RustType.String).writable,
"'::std::vec::Vec<::std::string::String>'",
"'::std::vec::Vec::<::std::string::String>'",
)
}

Expand All @@ -77,7 +77,7 @@ internal class RustTypesTest {
fun `RustType_HashMap_writable produces a template-compatible RuntimeType`() {
forInputExpectOutput(
RustType.HashMap(RustType.String, RustType.String).writable,
"'::std::collections::HashMap<::std::string::String, ::std::string::String>'",
"'::std::collections::HashMap::<::std::string::String, ::std::string::String>'",
)
}

Expand All @@ -87,7 +87,7 @@ internal class RustTypesTest {
RustType.HashSet(RustType.String).writable,
// Rust doesn't guarantee that `HashSet`s are insertion ordered, so we use a `Vec` instead.
// This is called out in a comment in the RustType.HashSet declaration
"'::std::vec::Vec<::std::string::String>'",
"'::std::vec::Vec::<::std::string::String>'",
)
}

Expand Down Expand Up @@ -146,8 +146,8 @@ internal class RustTypesTest {
@Test
fun `types render properly`() {
val type = RustType.Box(RustType.Option(RustType.Reference("a", RustType.Vec(RustType.String))))
type.render(false) shouldBe "Box<Option<&'a Vec<String>>>"
type.render(true) shouldBe "::std::boxed::Box<::std::option::Option<&'a ::std::vec::Vec<::std::string::String>>>"
type.render(false) shouldBe "Box<Option<&'a Vec::<String>>>"
type.render(true) shouldBe "::std::boxed::Box<::std::option::Option<&'a ::std::vec::Vec::<::std::string::String>>>"
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class SymbolVisitorTest {

val provider: SymbolProvider = testSymbolProvider(model)
val setSymbol = provider.toSymbol(set)
setSymbol.rustType().render(false) shouldBe "${RustType.HashSet.Type}<String>"
setSymbol.rustType().render(false) shouldBe "${RustType.HashSet.Type}::<String>"
setSymbol.referenceClosure().map { it.name } shouldBe listOf(RustType.HashSet.Type, "String")
}

Expand All @@ -172,7 +172,7 @@ class SymbolVisitorTest {

val provider: SymbolProvider = testSymbolProvider(model)
val setSymbol = provider.toSymbol(set)
setSymbol.rustType().render(false) shouldBe "Vec<Record>"
setSymbol.rustType().render(false) shouldBe "Vec::<Record>"
setSymbol.referenceClosure().map { it.name } shouldBe listOf("Vec", "Record")
}

Expand All @@ -193,7 +193,7 @@ class SymbolVisitorTest {

val provider: SymbolProvider = testSymbolProvider(model)
val setSymbol = provider.toSymbol(set)
setSymbol.rustType().render(false) shouldBe "Vec<Option<Record>>"
setSymbol.rustType().render(false) shouldBe "Vec::<Option<Record>>"
setSymbol.referenceClosure().map { it.name } shouldBe listOf("Vec", "Option", "Record")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ class UnconstrainedShapeSymbolProviderTest {
val structureBShape = model.lookup<StructureShape>("test#StructureB")

unconstrainedShapeSymbolProvider.toSymbol(structureBShape).rustType().render() shouldBe "crate::model::StructureB"
unconstrainedShapeSymbolProvider.toSymbol(listAShape).rustType().render() shouldBe "::std::vec::Vec<crate::model::StructureB>"
unconstrainedShapeSymbolProvider.toSymbol(listAShape).rustType().render() shouldBe "::std::vec::Vec::<crate::model::StructureB>"
}
}

0 comments on commit 08a533f

Please sign in to comment.