Skip to content

Commit

Permalink
address @t-bast comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pm47 committed Dec 19, 2024
1 parent 37b446a commit 34c1acc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@ object Serialization {
return fr.acinq.lightning.serialization.payment.v1.Serialization.serialize(payment)
}

fun deserialize(bin: ByteArray): DeserializationResult {
return when (bin.firstOrNull()?.toInt()) {
1 -> DeserializationResult.Success(fr.acinq.lightning.serialization.payment.v1.Deserialization.deserialize(bin))
else -> DeserializationResult.UnknownVersion(bin[0].toInt())
}
}

sealed class DeserializationResult {
data class Success(val data: WalletPayment) : DeserializationResult()
data class UnknownVersion(val version: Int) : DeserializationResult()

fun get(): WalletPayment = when(this) {
is Success -> data
is UnknownVersion -> error("unknown version ${this.version}")
fun deserialize(bin: ByteArray): Result<WalletPayment> {
return runCatching {
when (val version = bin.first().toInt()) {
1 -> fr.acinq.lightning.serialization.payment.v1.Deserialization.deserialize(bin)
else -> error("unknown version $version")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class PaymentSerializationTestsCommon {
)

payments.forEach {
assertEquals(it, Serialization.deserialize(Serialization.serialize(it)).get())
assertEquals(it, Serialization.deserialize(Serialization.serialize(it)).getOrThrow())
}
}
}

0 comments on commit 34c1acc

Please sign in to comment.