Skip to content

Commit

Permalink
Kotlin Throwable primitives and non-opaque enums (if marked as `att…
Browse files Browse the repository at this point in the history
…r(auto, error)`) (#758)

* working for primitives

* works for enums!

* adding tests

* disabling the Result<x, primitive> example for backends without custom_errors (results in errors in js)

---------

Co-authored-by: Ellen Arteca <[email protected]>
  • Loading branch information
emarteca and Ellen Arteca authored Jan 5, 2025
1 parent b9aab0c commit 689db3d
Show file tree
Hide file tree
Showing 16 changed files with 443 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DataProvider internal constructor (
if (returnVal.isOk == 1.toByte()) {
return Unit.ok()
} else {
return Unit.primitive_err()
return UnitError().err()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class FixedDecimal internal constructor (
val returnString = DW.writeToString(write)
return returnString.ok()
} else {
return Unit.primitive_err()
return UnitError().err()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FixedDecimalFormatter internal constructor (
CLEANER.register(returnOpaque, FixedDecimalFormatter.FixedDecimalFormatterCleaner(handle, FixedDecimalFormatter.lib));
return returnOpaque.ok()
} else {
return Unit.primitive_err()
return UnitError().err()
}
}
}
Expand Down
106 changes: 102 additions & 4 deletions example/kotlin/somelib/src/main/kotlin/dev/diplomattest/somelib/Lib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,112 @@ internal fun <T> T.ok(): Result<T> {
return Result.success(this)
}

internal fun <T, E> E.primitive_err(): Result<T> {
return Result.failure(RuntimeException("Received error $this"))
}

internal fun <T> Throwable.err(): Result<T> {
return Result.failure(this)
}

class UByteError internal constructor(internal val value: UByte): Exception("Rust error result for UByte") {
override fun toString(): String {
return "UByte error with value " + value
}

fun getValue(): UByte = value
}

class ByteError internal constructor(internal val value: Byte): Exception("Rust error result for Byte") {
override fun toString(): String {
return "Byte error with value " + value
}

fun getValue(): Byte = value
}

class UShortError internal constructor(internal val value: UShort): Exception("Rust error result for UShort") {
override fun toString(): String {
return "UShort error with value " + value
}

fun getValue(): UShort = value
}

class ShortError internal constructor(internal val value: Short): Exception("Rust error result for Short") {
override fun toString(): String {
return "Short error with value " + value
}

fun getValue(): Short = value
}

class UIntError internal constructor(internal val value: UInt): Exception("Rust error result for UInt") {
override fun toString(): String {
return "UInt error with value " + value
}

fun getValue(): UInt = value
}

class IntError internal constructor(internal val value: Int): Exception("Rust error result for Int") {
override fun toString(): String {
return "Int error with value " + value
}

fun getValue(): Int = value
}

class ULongError internal constructor(internal val value: ULong): Exception("Rust error result for ULong") {
override fun toString(): String {
return "ULong error with value " + value
}

fun getValue(): ULong = value
}

class LongError internal constructor(internal val value: Long): Exception("Rust error result for Long") {
override fun toString(): String {
return "Long error with value " + value
}

fun getValue(): Long = value
}

class FloatError internal constructor(internal val value: Float): Exception("Rust error result for Float") {
override fun toString(): String {
return "Float error with value " + value
}

fun getValue(): Float = value
}

class DoubleError internal constructor(internal val value: Double): Exception("Rust error result for Double") {
override fun toString(): String {
return "Double error with value " + value
}

fun getValue(): Double = value
}

class CharError internal constructor(internal val value: Char): Exception("Rust error result for Char") {
override fun toString(): String {
return "Char error with value " + value
}

fun getValue(): Char = value
}

class BooleanError internal constructor(internal val value: Boolean): Exception("Rust error result for Boolean") {
override fun toString(): String {
return "Boolean error with value " + value
}

fun getValue(): Boolean = value
}

class UnitError internal constructor(): Exception("Rust error result for Unit") {
override fun toString(): String {
return "Unit error"
}
}

internal class ResultPointerUnitUnion: Union() {
@JvmField
internal var ok: Pointer = Pointer(0)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 689db3d

Please sign in to comment.