Skip to content

Commit

Permalink
big code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ypopovych committed Nov 23, 2023
1 parent 5c75e3c commit dc140c7
Show file tree
Hide file tree
Showing 34 changed files with 336 additions and 244 deletions.
9 changes: 5 additions & 4 deletions Examples/Rust/DApp/AlertProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
//

import Foundation
import TesseractTransportsShared
import TesseractUtils
import CApp

public class AlertProvider: ObservableObject {
public struct Alert: Identifiable {
public class AlertProvider: ObservableObject, CoreConvertible {
struct Alert: Identifiable {
let message: String
public var id: String { message }
}
Expand All @@ -22,7 +23,7 @@ public class AlertProvider: ObservableObject {
self.alert = Alert(message: alert)
}

func toCore() -> CApp.AlertProvider {
public func toCore() -> CApp.AlertProvider {
var provider = CApp.AlertProvider(value: self)
provider.show_alert = alert_provider_show_alert
return provider
Expand All @@ -36,6 +37,6 @@ private func alert_provider_show_alert(this: UnsafePointer<CApp.AlertProvider>!,
}
}

extension CApp.AlertProvider: CSwiftDropPtr {
extension CApp.AlertProvider: CObjectPtr {
public typealias SObject = AlertProvider
}
6 changes: 3 additions & 3 deletions Examples/Rust/Extension/NativeUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protocol NativeUIDelegate: AnyObject {
func approveTx(tx: String) async -> Result<Bool, TesseractError>
}

extension CWallet.UI: CSwiftDropPtr {
extension CWallet.UI: CObjectPtr {
public typealias SObject = NativeUI
}

Expand All @@ -24,7 +24,7 @@ extension CWallet.UI {
}
}

public class NativeUI {
public class NativeUI: CoreConvertible {
weak var delegate: NativeUIDelegate!

init(delegate: NativeUIDelegate) {
Expand All @@ -35,7 +35,7 @@ public class NativeUI {
await self.delegate.approveTx(tx: tx)
}

func toCore() -> CWallet.UI {
public func toCore() -> CWallet.UI {
CWallet.UI(ui: self)
}
}
Expand Down
15 changes: 15 additions & 0 deletions Examples/Rust/Rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ members = [
"wallet"
]

[workspace.dependencies]
tesseract = { git = "https://github.com/tesseract-one/Tesseract.rs.git", branch = "dapp-fixes" }
tesseract-protocol-test = { git = "https://github.com/tesseract-one/Tesseract.rs.git", branch = "dapp-fixes" }
#tesseract = { path = "../../../../Tesseract.rs/tesseract", features = ["client"] }
#tesseract-protocol-test = { path = "../../../../Tesseract.rs/protocols/test", features = ["client"] }

tesseract-swift-utils = { path = "../../../Rust/utils" }
tesseract-swift-transports = { path = "../../../Rust/transports" }
async-trait = "0.1"
errorcon = "0.1"
log = "0.4"
log-panics = { version = "2", features = ["with-backtrace"]}
stderrlog = "0.5"
cbindgen = "0.26"

[profile.release]
strip = true
lto = true
23 changes: 11 additions & 12 deletions Examples/Rust/Rust/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ keywords = [ "Tesseract", "Cardano", "C" ]
edition = "2021"

[dependencies]
tesseract-swift-utils = { path = "../../../../Rust/utils" }
tesseract-swift-transports = { path = "../../../../Rust/transports", features = ["client"]}
#tesseract = { path = "../../../../../Tesseract.rs/tesseract", features = ["client"] }
#tesseract-protocol-test = { path = "../../../../../Tesseract.rs/protocols/test", features = ["client"] }
tesseract = { git = "https://github.com/tesseract-one/Tesseract.rs", branch = "master", features = ["client"] }
tesseract-protocol-test = { git = "https://github.com/tesseract-one/Tesseract.rs", branch = "master", features = ["client"] }
async-trait = "0.1"
errorcon = "0.1"
log = "0.4"
log-panics = { version = "2", features = ["with-backtrace"]}
stderrlog = "0.5"
tesseract-swift-utils.workspace = true
tesseract-swift-transports = { workspace = true, features = ["client"]}

tesseract = { workspace = true, features = ["client"] }
tesseract-protocol-test = { workspace = true, features = ["client"] }
async-trait.workspace = true
errorcon.workspace = true
log.workspace = true
log-panics.workspace = true
stderrlog.workspace = true

[build-dependencies]
cbindgen = "0.26"
cbindgen.workspace = true

[lib]
name = "app"
Expand Down
22 changes: 10 additions & 12 deletions Examples/Rust/Rust/wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ keywords = [ "Tesseract", "Cardano", "C" ]
edition = "2021"

[dependencies]
tesseract-swift-utils = { path = "../../../../Rust/utils" }
tesseract-swift-transports = { path = "../../../../Rust/transports", features = ["service"]}
#tesseract = { path = "../../../../../Tesseract.rs/tesseract", features = ["service"] }
#tesseract-protocol-test = { path = "../../../../../Tesseract.rs/protocols/test", features = ["service"] }
tesseract = { git = "https://github.com/tesseract-one/Tesseract.rs", branch = "master", features = ["service"] }
tesseract-protocol-test = { git = "https://github.com/tesseract-one/Tesseract.rs", branch = "master", features = ["service"] }
async-trait = "0.1"
errorcon = "0.1"
log = "0.4"
log-panics = { version = "2", features = ["with-backtrace"]}
stderrlog = "0.5"
tesseract-swift-utils.workspace = true
tesseract-swift-transports = { workspace = true, features = ["service"]}
tesseract = { workspace = true, features = ["service"] }
tesseract-protocol-test = { workspace = true, features = ["service"] }
async-trait.workspace = true
errorcon.workspace = true
log.workspace = true
log-panics.workspace = true
stderrlog.workspace = true

[build-dependencies]
cbindgen = "0.26"
cbindgen.workspace = true

[lib]
name = "wallet"
Expand Down
2 changes: 0 additions & 2 deletions Examples/Swift/DApp/AlertProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
//

import Foundation
import TesseractUtils
import CApp

final class AlertProvider: ObservableObject {
public struct Alert: Identifiable {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Swift/DApp/AppCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class TesseractTransportDelegate: TesseractDelegate {
self.alerts = alerts
}

func select(transports: Dictionary<String, TesseractTransportsClient.Status>) async -> String? {
func select(transports: Dictionary<String, Status>) async -> String? {
assert(transports.count == 1, "How the heck do we have more than one transport here?")
let transport = transports.first!
switch transport.value {
Expand Down
11 changes: 5 additions & 6 deletions Sources/TesseractClient/Protocols/SubstrateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public final class SubstrateService: ServiceBase<CTesseract.SubstrateService>,
public func getAccount(
type: TesseractShared.SubstrateAccountType
) async throws -> TesseractShared.SubstrateGetAccountResponse {
try await withUnsafePointer(to: &service) {
$0.pointee.get_account($0, type.asCValue)
}.result.castError(TesseractError.self).get()
try await service.get_account(&service, type.asCValue)
.result
.castError(TesseractError.self).get()
}

public func signTransaction(
Expand All @@ -27,9 +27,8 @@ public final class SubstrateService: ServiceBase<CTesseract.SubstrateService>,
try await extrinsic.withPtrRef { ext in
metadata.withPtrRef { meta in
types.withPtrRef { types in
withUnsafePointer(to: &service) {
$0.pointee.sign_transaction($0, type.asCValue, path, ext, meta, types)
}
service.sign_transaction(&service, type.asCValue,
path, ext, meta, types)
}
}
}.result.castError(TesseractError.self).get()
Expand Down
6 changes: 3 additions & 3 deletions Sources/TesseractClient/Protocols/TestService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public final class TestService: ServiceBase<CTesseract.TestService>,
TesseractShared.TestService
{
public func signTransaction(req: String) async throws -> String {
try await withUnsafePointer(to: &service) {
$0.pointee.sign_transaction($0, req)
}.result.castError(TesseractError.self).get()
try await service.sign_transaction(&service, req)
.result
.castError(TesseractError.self).get()
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/TesseractClient/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol Service: AnyObject {
init(tesseract: UnsafePointer<ClientTesseract>)
}

public protocol CoreService: CSwiftAnyDropPtr {
public protocol CoreService: CAnyObjectPtr {
static func get(from tesseract: UnsafePointer<ClientTesseract>) -> Self
}

Expand All @@ -25,6 +25,6 @@ open class ServiceBase<S: CoreService>: Service {
}

deinit {
try! service.free().get()
service.free()
}
}
8 changes: 4 additions & 4 deletions Sources/TesseractClient/Tesseract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import CTesseract
public final class Tesseract: TesseractBase {
public private(set) var tesseract: ClientTesseract!

public init(delegate: TesseractDelegate, serializer: Serializer = .default) throws {
public init(delegate: any TesseractDelegate, serializer: Serializer = .default) throws {
try super.init()
tesseract = tesseract_client_new(delegate.toCore(), serializer.toCore())
}

public func service<S: Service>(_ service: S.Type) -> S {
withUnsafePointer(to: &tesseract) { service.init(tesseract: $0) }
service.init(tesseract: &tesseract)
}

public func transport<T: CoreTransportConvertible>(_ transport: T) -> Self {
public func transport<T: CoreConvertible<ClientTransport>>(_ transport: T) -> Self {
tesseract = tesseract_client_add_transport(&tesseract, transport.toCore())
return self
}
Expand All @@ -38,7 +38,7 @@ public final class Tesseract: TesseractBase {
}

public static func `default`(
delegate: TesseractDelegate = SingleTransportDelegate(),
delegate: any TesseractDelegate = SingleTransportDelegate(),
serializer: Serializer = .default
) throws -> Self {
#if os(iOS)
Expand Down
6 changes: 3 additions & 3 deletions Sources/TesseractClient/TesseractDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import TesseractShared
@_exported import TesseractTransportsClient
#endif

public protocol TesseractDelegate: AnyObject {
public protocol TesseractDelegate: AnyObject, CoreConvertible<ClientTesseractDelegate> {
func select(transports: Dictionary<String, Status>) async -> String?
}

Expand All @@ -24,7 +24,7 @@ public extension TesseractDelegate {
}
}

extension ClientTesseractDelegate: CSwiftAnyDropPtr {}
extension ClientTesseractDelegate: CAnyObjectPtr {}

extension CKeyValue_CString__ClientStatus: CKeyValue, CPtr {
public typealias CKey = CString
Expand Down Expand Up @@ -56,7 +56,7 @@ private func delegate_select_transport(
) -> CFutureString {
let trans: Dictionary<String, Status> = transports.copiedDictionary()
return CFutureString {
await this.unowned(TesseractDelegate.self).castError().asyncFlatMap {
await this.unowned((any TesseractDelegate).self).castError().asyncFlatMap {
guard let result = await $0.select(transports: trans) else {
return .failure(TesseractError.cancelled)
}
Expand Down
8 changes: 2 additions & 6 deletions Sources/TesseractService/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import Foundation
import CTesseract
import TesseractShared

public protocol Service: AnyObject {
associatedtype Core: CoreService

func toCore() -> Core
}
public protocol Service: AnyObject, CoreConvertible where Core: CoreService {}

public protocol CoreService: CSwiftAnyDropPtr {
public protocol CoreService: CAnyObjectPtr {
func register(in tesseract: UnsafeMutablePointer<ServiceTesseract>) -> ServiceTesseract
}
2 changes: 1 addition & 1 deletion Sources/TesseractService/Tesseract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class Tesseract: TesseractBase {
return self
}

public func transport<T: CoreTransportConvertible>(_ transport: T) -> Self {
public func transport<T: CoreConvertible<ServiceTransport>>(_ transport: T) -> Self {
tesseract = tesseract_service_add_transport(&tesseract, transport.toCore())
return self
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TesseractShared/Protocols/SubstrateService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public extension SubstrateService {
}
}

extension CTesseract.SubstrateService: CSwiftAnyDropPtr {}
extension CTesseract.SubstrateService: CAnyObjectPtr {}

extension CTesseract.SubstrateAccountType: CType {
public init() { self.init(0) }
Expand Down
2 changes: 1 addition & 1 deletion Sources/TesseractShared/Protocols/TestService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public extension TestService {
}
}

extension CTesseract.TestService: CSwiftAnyDropPtr {}
extension CTesseract.TestService: CAnyObjectPtr {}
Loading

0 comments on commit dc140c7

Please sign in to comment.