Skip to content

Commit 19459fd

Browse files
committed
fix: no comm guess work in routine builder
1 parent dde81b9 commit 19459fd

2 files changed

Lines changed: 74 additions & 15 deletions

File tree

mmrpc-definition/src/commonMain/kotlin/builder/RoutineDefinitionBuilder.kt

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ class RoutineDefinitionBuilder :
2121
}
2222

2323
fun build(): RoutineDefinition {
24-
val comm = Comm.of(this.inputShape, this.outputShape)
24+
val inputShape = this.inputShape
25+
?: error("Input shape not specified")
26+
val outputShape = this.outputShape
27+
?: error("Output shape not specified")
28+
val comm = Comm.of(inputShape, outputShape)
2529
?: error("Cannot get Comm from input=$inputShape output=$outputShape")
2630

27-
check(comm.input != Comm.Shape.Void || input.isEmpty()) {
31+
check(comm.input != Comm.Shape.Void || this.input.isEmpty()) {
2832
"Injected input cannot be delivered when input shape is Void"
2933
}
30-
check(comm.output != Comm.Shape.Void || output.isEmpty()) {
34+
check(comm.output != Comm.Shape.Void || this.output.isEmpty()) {
3135
"Injected output cannot be delivered when output shape is Void"
3236
}
3337

@@ -36,8 +40,7 @@ class RoutineDefinitionBuilder :
3640
canonicalName = cn,
3741
description = this.description,
3842
metadata = this.metadata.toList(),
39-
comm = Comm.of(this.inputShape, this.outputShape)
40-
?: error("Cannot select Comm mode from input=$inputShape output=$outputShape"),
43+
comm = comm,
4144
faults = this.faults.mapIndexed { i, it ->
4245
it.get(cn, name = "fault$i")
4346
},
@@ -79,6 +82,68 @@ operator fun Comm.unaryPlus() {
7982

8083
////////////////////////////////////////
8184

85+
@Marker0
86+
context(ctx: RoutineDefinitionBuilder)
87+
fun voidInput() {
88+
check(ctx.inputShape == null || ctx.inputShape == Comm.Shape.Void) {
89+
"Input shape conflict: Shape was already set to ${ctx.inputShape}"
90+
}
91+
92+
ctx.inputShape = Comm.Shape.Void
93+
}
94+
95+
@Marker0
96+
context(ctx: RoutineDefinitionBuilder)
97+
fun voidOutput() {
98+
check(ctx.outputShape == null || ctx.outputShape == Comm.Shape.Void) {
99+
"Output shape conflict: Shape was already set to ${ctx.outputShape}"
100+
}
101+
102+
ctx.outputShape = Comm.Shape.Void
103+
}
104+
105+
@Marker0
106+
context(ctx: RoutineDefinitionBuilder)
107+
fun unaryInput() {
108+
check(ctx.inputShape == null || ctx.inputShape == Comm.Shape.Unary) {
109+
"Input shape conflict: Shape was already set to ${ctx.inputShape}"
110+
}
111+
112+
ctx.inputShape = Comm.Shape.Unary
113+
}
114+
115+
@Marker0
116+
context(ctx: RoutineDefinitionBuilder)
117+
fun unaryOutput() {
118+
check(ctx.outputShape == null || ctx.outputShape == Comm.Shape.Unary) {
119+
"Output shape conflict: Shape was already set to ${ctx.outputShape}"
120+
}
121+
122+
ctx.outputShape = Comm.Shape.Unary
123+
}
124+
125+
@Marker0
126+
context(ctx: RoutineDefinitionBuilder)
127+
fun streamInput() {
128+
check(ctx.inputShape == null || ctx.inputShape == Comm.Shape.Stream) {
129+
"Input shape conflict: Shape was already set to ${ctx.inputShape}"
130+
}
131+
132+
ctx.inputShape = Comm.Shape.Stream
133+
}
134+
135+
@Marker0
136+
context(ctx: RoutineDefinitionBuilder)
137+
fun streamOutput() {
138+
check(ctx.outputShape == null || ctx.outputShape == Comm.Shape.Stream) {
139+
"Output shape conflict: Shape was already set to ${ctx.outputShape}"
140+
}
141+
142+
ctx.outputShape = Comm.Shape.Stream
143+
}
144+
145+
////////////////////////////////////////
146+
82147
data object UnaryCommShapeKeyword
83148

84149
@Marker0

mmrpc-runtime/src/commonMain/kotlin/Comm.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,24 @@ enum class Comm(val input: Shape, val output: Shape) {
1919
}
2020

2121
companion object {
22-
fun of(input: Shape?, output: Shape?) = when (input) {
22+
fun of(input: Shape, output: Shape) = when (input) {
2323
Shape.Void -> when (output) {
24-
null, Shape.Void -> VoidVoid
24+
Shape.Void -> VoidVoid
2525
Shape.Unary -> VoidUnary
2626
Shape.Stream -> null
2727
}
2828

2929
Shape.Unary -> when (output) {
30-
null, Shape.Void -> UnaryVoid
30+
Shape.Void -> UnaryVoid
3131
Shape.Unary -> UnaryUnary
3232
Shape.Stream -> UnaryStream
3333
}
3434

3535
Shape.Stream -> when (output) {
36-
null, Shape.Unary -> StreamUnary
36+
Shape.Unary -> StreamUnary
3737
Shape.Stream -> StreamStream
3838
Shape.Void -> null
3939
}
40-
41-
null -> when (output) {
42-
null, Shape.Void -> VoidVoid
43-
Shape.Unary -> VoidUnary
44-
Shape.Stream -> UnaryStream
45-
}
4640
}
4741
}
4842
}

0 commit comments

Comments
 (0)