-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from strategyobject/feature/scale-unions
Adds tagged union (rust enum) support for scale codec implementation
- Loading branch information
Showing
82 changed files
with
4,397 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...src/main/java/com/strategyobject/substrateclient/scale/readers/union/BaseUnionReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.strategyobject.substrateclient.common.io.Streamer; | ||
import com.strategyobject.substrateclient.scale.ScaleReader; | ||
import com.strategyobject.substrateclient.types.union.Union; | ||
import lombok.NonNull; | ||
import lombok.val; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
import java.util.function.Function; | ||
|
||
public abstract class BaseUnionReader<T extends Union> implements ScaleReader<T> { | ||
private final int unionSize; | ||
@SuppressWarnings("rawtypes") | ||
private final Function[] createUnion; | ||
|
||
@SafeVarargs | ||
protected BaseUnionReader(int unionSize, Function<?, T>... createUnion) { | ||
Preconditions.checkArgument(createUnion.length == unionSize); | ||
|
||
this.unionSize = unionSize; | ||
this.createUnion = createUnion; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public T read(@NonNull InputStream stream, ScaleReader<?>... readers) throws IOException { | ||
Preconditions.checkArgument(readers.length == unionSize); | ||
Arrays.stream(readers).forEach(Preconditions::checkNotNull); | ||
|
||
val index = Streamer.readByte(stream); | ||
if (index >= unionSize) { | ||
throw new NoSuchElementException("Union index is out of bound."); | ||
} | ||
|
||
return (T) createUnion[index].apply(readers[index].read(stream)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...e/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union10Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union10; | ||
|
||
public class Union10Reader extends BaseUnionReader<Union10<?, ?, ?, ?, ?, ?, ?, ?, ?, ?>> { | ||
public Union10Reader() { | ||
super(10, | ||
Union10::withItem0, | ||
Union10::withItem1, | ||
Union10::withItem2, | ||
Union10::withItem3, | ||
Union10::withItem4, | ||
Union10::withItem5, | ||
Union10::withItem6, | ||
Union10::withItem7, | ||
Union10::withItem8, | ||
Union10::withItem9); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...e/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union11Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union11; | ||
|
||
public class Union11Reader extends BaseUnionReader<Union11<?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?>> { | ||
public Union11Reader() { | ||
super(11, | ||
Union11::withItem0, | ||
Union11::withItem1, | ||
Union11::withItem2, | ||
Union11::withItem3, | ||
Union11::withItem4, | ||
Union11::withItem5, | ||
Union11::withItem6, | ||
Union11::withItem7, | ||
Union11::withItem8, | ||
Union11::withItem9, | ||
Union11::withItem10); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...e/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union12Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union12; | ||
|
||
public class Union12Reader extends BaseUnionReader<Union12<?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?>> { | ||
public Union12Reader() { | ||
super(12, | ||
Union12::withItem0, | ||
Union12::withItem1, | ||
Union12::withItem2, | ||
Union12::withItem3, | ||
Union12::withItem4, | ||
Union12::withItem5, | ||
Union12::withItem6, | ||
Union12::withItem7, | ||
Union12::withItem8, | ||
Union12::withItem9, | ||
Union12::withItem10, | ||
Union12::withItem11); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
scale/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union1Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union1; | ||
|
||
public class Union1Reader extends BaseUnionReader<Union1<?>> { | ||
public Union1Reader() { | ||
super(1, Union1::withItem0); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
scale/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union2Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union2; | ||
|
||
public class Union2Reader extends BaseUnionReader<Union2<?, ?>> { | ||
public Union2Reader() { | ||
super(2, Union2::withItem0, Union2::withItem1); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
scale/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union3Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union3; | ||
|
||
public class Union3Reader extends BaseUnionReader<Union3<?, ?, ?>> { | ||
public Union3Reader() { | ||
super(3, | ||
Union3::withItem0, | ||
Union3::withItem1, | ||
Union3::withItem2); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
scale/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union4Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union4; | ||
|
||
public class Union4Reader extends BaseUnionReader<Union4<?, ?, ?, ?>> { | ||
public Union4Reader() { | ||
super(4, | ||
Union4::withItem0, | ||
Union4::withItem1, | ||
Union4::withItem2, | ||
Union4::withItem3); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
scale/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union5Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union5; | ||
|
||
public class Union5Reader extends BaseUnionReader<Union5<?, ?, ?, ?, ?>> { | ||
public Union5Reader() { | ||
super(5, | ||
Union5::withItem0, | ||
Union5::withItem1, | ||
Union5::withItem2, | ||
Union5::withItem3, | ||
Union5::withItem4); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
scale/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union6Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union6; | ||
|
||
public class Union6Reader extends BaseUnionReader<Union6<?, ?, ?, ?, ?, ?>> { | ||
public Union6Reader() { | ||
super(6, | ||
Union6::withItem0, | ||
Union6::withItem1, | ||
Union6::withItem2, | ||
Union6::withItem3, | ||
Union6::withItem4, | ||
Union6::withItem5); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
scale/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union7Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union7; | ||
|
||
public class Union7Reader extends BaseUnionReader<Union7<?, ?, ?, ?, ?, ?, ?>> { | ||
public Union7Reader() { | ||
super(7, | ||
Union7::withItem0, | ||
Union7::withItem1, | ||
Union7::withItem2, | ||
Union7::withItem3, | ||
Union7::withItem4, | ||
Union7::withItem5, | ||
Union7::withItem6); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
scale/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union8Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union8; | ||
|
||
public class Union8Reader extends BaseUnionReader<Union8<?, ?, ?, ?, ?, ?, ?, ?>> { | ||
public Union8Reader() { | ||
super(8, | ||
Union8::withItem0, | ||
Union8::withItem1, | ||
Union8::withItem2, | ||
Union8::withItem3, | ||
Union8::withItem4, | ||
Union8::withItem5, | ||
Union8::withItem6, | ||
Union8::withItem7); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
scale/src/main/java/com/strategyobject/substrateclient/scale/readers/union/Union9Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.strategyobject.substrateclient.scale.readers.union; | ||
|
||
import com.strategyobject.substrateclient.types.union.Union9; | ||
|
||
public class Union9Reader extends BaseUnionReader<Union9<?, ?, ?, ?, ?, ?, ?, ?, ?>> { | ||
public Union9Reader() { | ||
super(9, | ||
Union9::withItem0, | ||
Union9::withItem1, | ||
Union9::withItem2, | ||
Union9::withItem3, | ||
Union9::withItem4, | ||
Union9::withItem5, | ||
Union9::withItem6, | ||
Union9::withItem7, | ||
Union9::withItem8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.