@@ -43,7 +43,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
43
43
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWords
44
44
import software.amazon.smithy.rust.codegen.core.rustlang.RustType
45
45
import software.amazon.smithy.rust.codegen.core.rustlang.Visibility
46
- import software.amazon.smithy.rust.codegen.core.rustlang.stripOuter
47
46
import software.amazon.smithy.rust.codegen.core.smithy.traits.RustBoxTrait
48
47
import software.amazon.smithy.rust.codegen.core.util.PANIC
49
48
import software.amazon.smithy.rust.codegen.core.util.hasTrait
@@ -86,83 +85,6 @@ data class SymbolVisitorConfig(
86
85
val moduleProvider: ModuleProvider,
87
86
)
88
87
89
- /**
90
- * Make the Rust type of a symbol optional (hold `Option<T>`)
91
- *
92
- * This is idempotent and will have no change if the type is already optional.
93
- */
94
- fun Symbol.makeOptional(): Symbol =
95
- if (isOptional()) {
96
- this
97
- } else {
98
- val rustType = RustType.Option(this.rustType())
99
- Symbol.builder()
100
- .rustType(rustType)
101
- .addReference(this)
102
- .name(rustType.name)
103
- .build()
104
- }
105
-
106
- /**
107
- * Make the Rust type of a symbol boxed (hold `Box<T>`).
108
- *
109
- * This is idempotent and will have no change if the type is already boxed.
110
- */
111
- fun Symbol.makeRustBoxed(): Symbol =
112
- if (isRustBoxed()) {
113
- this
114
- } else {
115
- val rustType = RustType.Box(this.rustType())
116
- Symbol.builder()
117
- .rustType(rustType)
118
- .addReference(this)
119
- .name(rustType.name)
120
- .build()
121
- }
122
-
123
- /**
124
- * Make the Rust type of a symbol wrapped in `MaybeConstrained`. (hold `MaybeConstrained<T>`).
125
- *
126
- * This is idempotent and will have no change if the type is already `MaybeConstrained<T>`.
127
- */
128
- fun Symbol.makeMaybeConstrained(): Symbol =
129
- if (this.rustType() is RustType.MaybeConstrained) {
130
- this
131
- } else {
132
- val rustType = RustType.MaybeConstrained(this.rustType())
133
- Symbol.builder()
134
- .rustType(rustType)
135
- .addReference(this)
136
- .name(rustType.name)
137
- .build()
138
- }
139
-
140
- /**
141
- * Map the [RustType] of a symbol with [f].
142
- *
143
- * WARNING: This function does not set any `SymbolReference`s on the returned symbol. You will have to add those
144
- * yourself if your logic relies on them.
145
- **/
146
- fun Symbol.mapRustType(f: (RustType) -> RustType): Symbol {
147
- val newType = f(this.rustType())
148
- return Symbol.builder().rustType(newType)
149
- .name(newType.name)
150
- .build()
151
- }
152
-
153
- /** Set the symbolLocation for this symbol builder */
154
- fun Symbol.Builder.locatedIn(rustModule: RustModule.LeafModule): Symbol.Builder {
155
- val currentRustType = this.build().rustType()
156
- check(currentRustType is RustType.Opaque) {
157
- "Only `Opaque` can have their namespace updated"
158
- }
159
- val newRustType = currentRustType.copy(namespace = rustModule.fullyQualifiedPath())
160
- return this.definitionFile(rustModule.definitionFile())
161
- .namespace(rustModule.fullyQualifiedPath(), "::")
162
- .rustType(newRustType)
163
- .module(rustModule)
164
- }
165
-
166
88
/**
167
89
* Track both the past and current name of a symbol
168
90
*
@@ -401,28 +323,16 @@ fun handleRustBoxing(symbol: Symbol, shape: MemberShape): Symbol =
401
323
symbol.makeRustBoxed()
402
324
} else symbol
403
325
404
- fun symbolBuilder(shape: Shape?, rustType: RustType): Symbol.Builder {
405
- val builder = Symbol.builder().putProperty(SHAPE_KEY, shape)
406
- return builder.rustType(rustType)
326
+ fun symbolBuilder(shape: Shape?, rustType: RustType): Symbol.Builder =
327
+ Symbol.builder().shape(shape).rustType(rustType)
407
328
.name(rustType.name)
408
329
// Every symbol that actually gets defined somewhere should set a definition file
409
330
// If we ever generate a `thisisabug.rs`, there is a bug in our symbol generation
410
331
.definitionFile("thisisabug.rs")
411
- }
412
332
413
333
fun handleOptionality(symbol: Symbol, member: MemberShape, nullableIndex: NullableIndex, nullabilityCheckMode: CheckMode): Symbol =
414
334
symbol.letIf(nullableIndex.isMemberNullable(member, nullabilityCheckMode)) { symbol.makeOptional() }
415
335
416
- private const val RUST_TYPE_KEY = "rusttype"
417
- private const val RUST_MODULE_KEY = "rustmodule"
418
- private const val SHAPE_KEY = "shape"
419
- private const val SYMBOL_DEFAULT = "symboldefault"
420
- private const val RENAMED_FROM_KEY = "renamedfrom"
421
-
422
- fun Symbol.Builder.rustType(rustType: RustType): Symbol.Builder = this.putProperty(RUST_TYPE_KEY, rustType)
423
- fun Symbol.Builder.module(module: RustModule.LeafModule): Symbol.Builder = this.putProperty(RUST_MODULE_KEY, module)
424
- fun Symbol.module(): RustModule.LeafModule = this.expectProperty(RUST_MODULE_KEY, RustModule.LeafModule::class.java)
425
-
426
336
/**
427
337
* Creates a test module for this symbol.
428
338
* For example if the symbol represents the name for the struct `struct MyStruct { ... }`,
@@ -445,49 +355,6 @@ fun SymbolProvider.testModuleForShape(shape: Shape): RustModule.LeafModule {
445
355
)
446
356
}
447
357
448
- fun Symbol.Builder.renamedFrom(name: String): Symbol.Builder {
449
- return this.putProperty(RENAMED_FROM_KEY, name)
450
- }
451
-
452
- fun Symbol.renamedFrom(): String? = this.getProperty(RENAMED_FROM_KEY, String::class.java).orNull()
453
-
454
- fun Symbol.defaultValue(): Default = this.getProperty(SYMBOL_DEFAULT, Default::class.java).orElse(Default.NoDefault)
455
- fun Symbol.Builder.setDefault(default: Default): Symbol.Builder = this.putProperty(SYMBOL_DEFAULT, default)
456
-
457
- /**
458
- * Type representing the default value for a given type. (eg. for Strings, this is `""`)
459
- */
460
- sealed class Default {
461
- /**
462
- * This symbol has no default value. If the symbol is not optional, this will be an error during builder construction
463
- */
464
- object NoDefault : Default()
465
-
466
- /**
467
- * This symbol should use the Rust `std::default::Default` when unset
468
- */
469
- object RustDefault : Default()
470
- }
471
-
472
- /**
473
- * True when it is valid to use the default/0 value for [this] symbol during construction.
474
- */
475
- fun Symbol.canUseDefault(): Boolean = this.defaultValue() != Default.NoDefault
476
-
477
- /**
478
- * True when [this] is will be represented by Option<T> in Rust
479
- */
480
- fun Symbol.isOptional(): Boolean = when (this.rustType()) {
481
- is RustType.Option -> true
482
- else -> false
483
- }
484
-
485
- fun Symbol.isRustBoxed(): Boolean = rustType().stripOuter<RustType.Option>() is RustType.Box
486
-
487
- // Symbols should _always_ be created with a Rust type & shape attached
488
- fun Symbol.rustType(): RustType = this.expectProperty(RUST_TYPE_KEY, RustType::class.java)
489
- fun Symbol.shape(): Shape = this.expectProperty(SHAPE_KEY, Shape::class.java)
490
-
491
358
/**
492
359
* You should rarely need this function, rust names in general should be symbol-aware,
493
360
* this is "automatic" if you use things like [software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate].
0 commit comments