Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Decouple from UniFFI code-gen (minus the standard error) (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe authored Sep 5, 2024
1 parent bb46a93 commit e48b64f
Show file tree
Hide file tree
Showing 20 changed files with 507 additions and 107 deletions.
2 changes: 1 addition & 1 deletion bindings/tbdex_uniffi/src/tbdex.udl
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ dictionary CancelData {
};

dictionary CancelDataData {
string reason;
string? reason;
};

interface OrderStatus {
Expand Down
2 changes: 1 addition & 1 deletion bound/kt/src/main/kotlin/tbdex/sdk/Json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const val dateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
* val node = Json.parse(payload)
* ```
*/
object Json {
internal object Json {
/**
* The Jackson object mapper instance, shared across the lib.
*
Expand Down
2 changes: 1 addition & 1 deletion bound/kt/src/main/kotlin/tbdex/sdk/http/Balances.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tbdex.sdk.http
import tbdex.sdk.resources.Balance
import tbdex.sdk.rust.GetBalancesResponseBody as RustCoreGetBalancesResponseBody

class GetBalancesResponseBody private constructor(
data class GetBalancesResponseBody private constructor(
val data: List<Balance>,
internal val rustCoreGetBalancesResponseBody: RustCoreGetBalancesResponseBody
) {
Expand Down
26 changes: 22 additions & 4 deletions bound/kt/src/main/kotlin/tbdex/sdk/http/Errors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,42 @@ package tbdex.sdk.http
import tbdex.sdk.rust.ErrorDetailData as RustCoreErrorDetail
import tbdex.sdk.rust.ErrorResponseBody as RustCoreErrorResponseBody

typealias ErrorDetail = RustCoreErrorDetail
data class ErrorDetail(
val id: String? = null,
val message: String? = null,
val path: String? = null,
) {
companion object {
internal fun fromRustCore(rustCore: RustCoreErrorDetail): ErrorDetail {
return ErrorDetail(rustCore.id, rustCore.message, rustCore.path)
}
}

internal fun toRustCore(): RustCoreErrorDetail {
return RustCoreErrorDetail(id, message, path)
}
}

class ErrorResponseBody private constructor(
data class ErrorResponseBody private constructor(
val message: String,
val details: List<ErrorDetail>?,
internal val rustCoreErrorResponseBody: RustCoreErrorResponseBody
) {
constructor(message: String, details: List<ErrorDetail>? = null) : this(
message,
details,
RustCoreErrorResponseBody(message, details),
RustCoreErrorResponseBody(message, details?.map { it.toRustCore() }),
)

companion object {
fun fromJsonString(json: String): ErrorResponseBody {
val rustCoreErrorResponseBody = RustCoreErrorResponseBody.fromJsonString(json)
val data = rustCoreErrorResponseBody.getData()
return ErrorResponseBody(data.message, data.details, rustCoreErrorResponseBody)
return ErrorResponseBody(
data.message,
data.details?.map { ErrorDetail.fromRustCore(it) },
rustCoreErrorResponseBody
)
}
}

Expand Down
68 changes: 34 additions & 34 deletions bound/kt/src/main/kotlin/tbdex/sdk/http/Exchanges.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package tbdex.sdk.http

import tbdex.sdk.messages.*
import tbdex.sdk.rust.GetExchangeResponseBodyDataSerializedMessage
import tbdex.sdk.rust.MessageKind
import tbdex.sdk.rust.GetExchangeResponseBodyDataSerializedMessage as RustCoreGetExchangeResponseBodyDataSerializedMessage
import tbdex.sdk.rust.MessageKind as RustCoreMessageKind
import tbdex.sdk.rust.CreateExchangeRequestBody as RustCoreCreateExchangeRequestBody
import tbdex.sdk.rust.GetExchangeResponseBody as RustCoreGetExchangeResponseBody
import tbdex.sdk.rust.GetExchangeResponseBodyData as RustCoreGetExchangeResponseBodyData
import tbdex.sdk.rust.GetExchangesResponseBody as RustCoreGetExchangesResponseBody
import tbdex.sdk.rust.UpdateExchangeRequestBody as RustCoreUpdateExchangeRequestBody
import tbdex.sdk.rust.ReplyToRequestBody as RustCoreReplyToRequestBody

class GetExchangeResponseBody private constructor(
data class GetExchangeResponseBody private constructor(
val data: List<Message>,
internal val rustCoreGetExchangeResponseBody: RustCoreGetExchangeResponseBody
) {
Expand All @@ -20,17 +20,17 @@ class GetExchangeResponseBody private constructor(
RustCoreGetExchangeResponseBodyData(
data.map {
val (kind, jsonSerialized) = when (it) {
is Rfq -> Pair(MessageKind.RFQ, it.toJsonString())
is Quote -> Pair(MessageKind.QUOTE, it.toJsonString())
is Order -> Pair(MessageKind.ORDER, it.toJsonString())
is OrderInstructions -> Pair(MessageKind.ORDER_INSTRUCTIONS, it.toJsonString())
is Cancel -> Pair(MessageKind.CANCEL, it.toJsonString())
is OrderStatus -> Pair(MessageKind.ORDER_STATUS, it.toJsonString())
is Close -> Pair(MessageKind.CLOSE, it.toJsonString())
is Rfq -> Pair(RustCoreMessageKind.RFQ, it.toJsonString())
is Quote -> Pair(RustCoreMessageKind.QUOTE, it.toJsonString())
is Order -> Pair(RustCoreMessageKind.ORDER, it.toJsonString())
is OrderInstructions -> Pair(RustCoreMessageKind.ORDER_INSTRUCTIONS, it.toJsonString())
is Cancel -> Pair(RustCoreMessageKind.CANCEL, it.toJsonString())
is OrderStatus -> Pair(RustCoreMessageKind.ORDER_STATUS, it.toJsonString())
is Close -> Pair(RustCoreMessageKind.CLOSE, it.toJsonString())
else -> throw Exception("unknown type $it")
}

GetExchangeResponseBodyDataSerializedMessage(kind, jsonSerialized)
RustCoreGetExchangeResponseBodyDataSerializedMessage(kind, jsonSerialized)
}
)
)
Expand All @@ -41,13 +41,13 @@ class GetExchangeResponseBody private constructor(
val rustCoreGetExchangeResponseBody = RustCoreGetExchangeResponseBody.fromJsonString(json)
val data = rustCoreGetExchangeResponseBody.getData().data.map {
when (it.kind) {
MessageKind.RFQ -> Rfq.fromJsonString(it.jsonSerialized)
MessageKind.QUOTE -> Quote.fromJsonString(it.jsonSerialized)
MessageKind.ORDER -> Order.fromJsonString(it.jsonSerialized)
MessageKind.ORDER_INSTRUCTIONS -> Order.fromJsonString(it.jsonSerialized)
MessageKind.CANCEL -> Cancel.fromJsonString(it.jsonSerialized)
MessageKind.ORDER_STATUS -> OrderStatus.fromJsonString(it.jsonSerialized)
MessageKind.CLOSE -> Close.fromJsonString(it.jsonSerialized)
RustCoreMessageKind.RFQ -> Rfq.fromJsonString(it.jsonSerialized)
RustCoreMessageKind.QUOTE -> Quote.fromJsonString(it.jsonSerialized)
RustCoreMessageKind.ORDER -> Order.fromJsonString(it.jsonSerialized)
RustCoreMessageKind.ORDER_INSTRUCTIONS -> Order.fromJsonString(it.jsonSerialized)
RustCoreMessageKind.CANCEL -> Cancel.fromJsonString(it.jsonSerialized)
RustCoreMessageKind.ORDER_STATUS -> OrderStatus.fromJsonString(it.jsonSerialized)
RustCoreMessageKind.CLOSE -> Close.fromJsonString(it.jsonSerialized)
}
}

Expand All @@ -60,7 +60,7 @@ class GetExchangeResponseBody private constructor(
}
}

class GetExchangesResponseBody private constructor(
data class GetExchangesResponseBody private constructor(
val data: List<String>,
internal val rustCoreGetExchangesResponseBody: RustCoreGetExchangesResponseBody
) {
Expand All @@ -84,7 +84,7 @@ class GetExchangesResponseBody private constructor(
}
}

class CreateExchangeRequestBody private constructor(
data class CreateExchangeRequestBody private constructor(
val message: Rfq,
val replyTo: String? = null,
internal val rustCoreCreateExchangeRequestBody: RustCoreCreateExchangeRequestBody
Expand Down Expand Up @@ -114,15 +114,15 @@ class CreateExchangeRequestBody private constructor(

interface WalletUpdateMessage {}

class UpdateExchangeRequestBody private constructor(
data class UpdateExchangeRequestBody private constructor(
val message: WalletUpdateMessage,
internal val rustCoreUpdateExchangeRequestBody: RustCoreUpdateExchangeRequestBody
) {
constructor(message: WalletUpdateMessage) : this(
message,
when (message) {
is Order -> RustCoreUpdateExchangeRequestBody(MessageKind.ORDER_STATUS, message.toJsonString())
is Cancel -> RustCoreUpdateExchangeRequestBody(MessageKind.CANCEL, message.toJsonString())
is Order -> RustCoreUpdateExchangeRequestBody(RustCoreMessageKind.ORDER_STATUS, message.toJsonString())
is Cancel -> RustCoreUpdateExchangeRequestBody(RustCoreMessageKind.CANCEL, message.toJsonString())
else -> throw Exception("unknown type")
}
)
Expand All @@ -133,8 +133,8 @@ class UpdateExchangeRequestBody private constructor(
val data = rustCoreUpdateExchangeRequestBody.getData()

val message = when (data.kind) {
MessageKind.ORDER -> Order.fromJsonString(data.jsonSerializedMessage)
MessageKind.CANCEL -> Cancel.fromJsonString(data.jsonSerializedMessage)
RustCoreMessageKind.ORDER -> Order.fromJsonString(data.jsonSerializedMessage)
RustCoreMessageKind.CANCEL -> Cancel.fromJsonString(data.jsonSerializedMessage)
else -> throw Exception("Unsupported message kind ${data.kind}")
}

Expand All @@ -149,17 +149,17 @@ class UpdateExchangeRequestBody private constructor(

interface ReplyToMessage {}

class ReplyToRequestBody private constructor(
data class ReplyToRequestBody private constructor(
val message: ReplyToMessage,
internal val rustCoreReplyToRequestBody: RustCoreReplyToRequestBody
) {
constructor(message: ReplyToMessage) : this(
message,
when (message) {
is Quote -> RustCoreReplyToRequestBody(MessageKind.QUOTE, message.toJsonString())
is OrderInstructions -> RustCoreReplyToRequestBody(MessageKind.ORDER_INSTRUCTIONS, message.toJsonString())
is OrderStatus -> RustCoreReplyToRequestBody(MessageKind.ORDER_STATUS, message.toJsonString())
is Close -> RustCoreReplyToRequestBody(MessageKind.CLOSE, message.toJsonString())
is Quote -> RustCoreReplyToRequestBody(RustCoreMessageKind.QUOTE, message.toJsonString())
is OrderInstructions -> RustCoreReplyToRequestBody(RustCoreMessageKind.ORDER_INSTRUCTIONS, message.toJsonString())
is OrderStatus -> RustCoreReplyToRequestBody(RustCoreMessageKind.ORDER_STATUS, message.toJsonString())
is Close -> RustCoreReplyToRequestBody(RustCoreMessageKind.CLOSE, message.toJsonString())
else -> throw Exception("unknown type")
}
)
Expand All @@ -170,10 +170,10 @@ class ReplyToRequestBody private constructor(
val data = rustCoreReplyToRequestBody.getData()

val message: ReplyToMessage = when (data.kind) {
MessageKind.QUOTE -> Quote.fromJsonString(data.jsonSerializedMessage)
MessageKind.ORDER_INSTRUCTIONS -> OrderInstructions.fromJsonString(data.jsonSerializedMessage)
MessageKind.ORDER_STATUS -> OrderStatus.fromJsonString(data.jsonSerializedMessage)
MessageKind.CLOSE -> Close.fromJsonString(data.jsonSerializedMessage)
RustCoreMessageKind.QUOTE -> Quote.fromJsonString(data.jsonSerializedMessage)
RustCoreMessageKind.ORDER_INSTRUCTIONS -> OrderInstructions.fromJsonString(data.jsonSerializedMessage)
RustCoreMessageKind.ORDER_STATUS -> OrderStatus.fromJsonString(data.jsonSerializedMessage)
RustCoreMessageKind.CLOSE -> Close.fromJsonString(data.jsonSerializedMessage)
else -> throw Exception("Unsupported message kind ${data.kind}")
}

Expand Down
2 changes: 1 addition & 1 deletion bound/kt/src/main/kotlin/tbdex/sdk/http/Offerings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tbdex.sdk.http
import tbdex.sdk.resources.Offering
import tbdex.sdk.rust.GetOfferingsResponseBody as RustCoreGetOfferingsResponseBody

class GetOfferingsResponseBody private constructor(
data class GetOfferingsResponseBody private constructor(
val data: List<Offering>,
internal val rustCoreGetOfferingsResponseBody: RustCoreGetOfferingsResponseBody
) {
Expand Down
11 changes: 9 additions & 2 deletions bound/kt/src/main/kotlin/tbdex/sdk/httpclient/Exchanges.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ fun getExchange(pfiDidUri: String, bearerDid: BearerDid, exchangeId: String): Ex
return Exchange.fromRustCore(rustCoreExchange)
}

typealias GetExchangeIdsQueryParams = RustCoreGetExchangeIdsQueryParams
data class GetExchangeIdsQueryParams (
val paginationOffset: Long?,
val paginationLimit: Long?
) {
internal fun toRustCore(): RustCoreGetExchangeIdsQueryParams {
return RustCoreGetExchangeIdsQueryParams(paginationOffset, paginationLimit)
}
}

fun getExchangeIds(pfiDidUri: String, bearerDid: BearerDid, queryParams: GetExchangeIdsQueryParams? = null): List<String> {
return rustCoreGetExchangeIds(pfiDidUri, RustCoreBearerDid.fromWeb5(bearerDid), queryParams)
return rustCoreGetExchangeIds(pfiDidUri, RustCoreBearerDid.fromWeb5(bearerDid), queryParams?.toRustCore())
}


Expand Down
41 changes: 33 additions & 8 deletions bound/kt/src/main/kotlin/tbdex/sdk/messages/Cancel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ import tbdex.sdk.rust.fromWeb5
import tbdex.sdk.rust.BearerDid as RustCoreBearerDid
import web5.sdk.dids.BearerDid

typealias CancelData = RustCoreCancelData
data class CancelData(val reason: String? = null) {
companion object {
internal fun fromRustCore(rustCore: RustCoreCancelData): CancelData {
return CancelData(rustCore.reason)
}
}

internal fun toRustCore(): RustCoreCancelData {
return RustCoreCancelData(reason)
}
}

class Cancel private constructor(
data class Cancel private constructor(
val metadata: MessageMetadata,
val data: RustCoreCancelData,
val data: CancelData,
val signature: String,
internal val rustCoreCancel: RustCoreCancel
): Message, WalletUpdateMessage {
Expand All @@ -20,24 +30,39 @@ class Cancel private constructor(
to: String,
from: String,
exchangeId: String,
data: RustCoreCancelData,
data: CancelData,
protocol: String? = null,
externalId: String? = null
): Cancel {
val rustCoreCancel = RustCoreCancel.create(to, from, exchangeId, data, protocol, externalId)
val rustCoreCancel = RustCoreCancel.create(to, from, exchangeId, data.toRustCore(), protocol, externalId)
val rustCoreData = rustCoreCancel.getData()
return Cancel(rustCoreData.metadata, rustCoreData.data, rustCoreData.signature, rustCoreCancel)
return Cancel(
MessageMetadata.fromRustCore(rustCoreData.metadata),
CancelData.fromRustCore(rustCoreData.data),
rustCoreData.signature,
rustCoreCancel
)
}

fun fromJsonString(json: String): Cancel {
val rustCoreCancel = RustCoreCancel.fromJsonString(json)
val rustCoreData = rustCoreCancel.getData()
return Cancel(rustCoreData.metadata, rustCoreData.data, rustCoreData.signature, rustCoreCancel)
return Cancel(
MessageMetadata.fromRustCore(rustCoreData.metadata),
CancelData.fromRustCore(rustCoreData.data),
rustCoreData.signature,
rustCoreCancel
)
}

internal fun fromRustCoreCancel(rustCoreCancel: RustCoreCancel): Cancel {
val rustCoreData = rustCoreCancel.getData()
return Cancel(rustCoreData.metadata, rustCoreData.data, rustCoreData.signature, rustCoreCancel)
return Cancel(
MessageMetadata.fromRustCore(rustCoreData.metadata),
CancelData.fromRustCore(rustCoreData.data),
rustCoreData.signature,
rustCoreCancel
)
}
}

Expand Down
40 changes: 34 additions & 6 deletions bound/kt/src/main/kotlin/tbdex/sdk/messages/Close.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,22 @@ import tbdex.sdk.rust.fromWeb5
import tbdex.sdk.rust.BearerDid as RustCoreBearerDid
import web5.sdk.dids.BearerDid

typealias CloseData = RustCoreCloseData
data class CloseData (
val reason: String?,
val success: Boolean?
) {
companion object {
internal fun fromRustCore(rustCore: RustCoreCloseData): CloseData {
return CloseData(rustCore.reason, rustCore.success)
}
}

internal fun toRustCore(): RustCoreCloseData {
return RustCoreCloseData(reason, success)
}
}

class Close private constructor(
data class Close private constructor(
val metadata: MessageMetadata,
val data: CloseData,
val signature: String,
Expand All @@ -24,20 +37,35 @@ class Close private constructor(
protocol: String? = null,
externalId: String? = null
): Close {
val rustCoreClose = RustCoreClose.create(to, from, exchangeId, data, protocol, externalId)
val rustCoreClose = RustCoreClose.create(to, from, exchangeId, data.toRustCore(), protocol, externalId)
val rustCoreData = rustCoreClose.getData()
return Close(rustCoreData.metadata, rustCoreData.data, rustCoreData.signature, rustCoreClose)
return Close(
MessageMetadata.fromRustCore(rustCoreData.metadata),
CloseData.fromRustCore(rustCoreData.data),
rustCoreData.signature,
rustCoreClose
)
}

fun fromJsonString(json: String): Close {
val rustCoreClose = RustCoreClose.fromJsonString(json)
val rustCoreData = rustCoreClose.getData()
return Close(rustCoreData.metadata, rustCoreData.data, rustCoreData.signature, rustCoreClose)
return Close(
MessageMetadata.fromRustCore(rustCoreData.metadata),
CloseData.fromRustCore(rustCoreData.data),
rustCoreData.signature,
rustCoreClose
)
}

internal fun fromRustCoreClose(rustCoreClose: RustCoreClose): Close {
val rustCoreData = rustCoreClose.getData()
return Close(rustCoreData.metadata, rustCoreData.data, rustCoreData.signature, rustCoreClose)
return Close(
MessageMetadata.fromRustCore(rustCoreData.metadata),
CloseData.fromRustCore(rustCoreData.data),
rustCoreData.signature,
rustCoreClose
)
}
}

Expand Down
Loading

0 comments on commit e48b64f

Please sign in to comment.