Skip to content

Commit

Permalink
fix broken Swift (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX authored Feb 21, 2024
1 parent 632f3c4 commit 03690ba
Show file tree
Hide file tree
Showing 21 changed files with 487 additions and 471 deletions.
91 changes: 91 additions & 0 deletions apple/Sources/Sargon/Address.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

#if DEBUG
/// TODO: Declare in Rust land? Make non-DEBUG?
public enum Address: Hashable, Equatable, Sendable {
case accesscontroller(AccessControllerAddress)
case account(AccountAddress)
case component(ComponentAddress)
case identity(IdentityAddress)
case package(PackageAddress)
case pool(PoolAddress)
case resource(ResourceAddress)
case validator(ValidatorAddress)
case vault(VaultAddress)
}
extension Address: AddressProtocol {
public init(validatingAddress bech32String: String) throws {
if let address = try? AccessControllerAddress(
validatingAddress: bech32String)
{
self = .accesscontroller(address)
} else if let address = try? AccountAddress(validatingAddress: bech32String)
{
self = .account(address)
} else if let address = try? ComponentAddress(
validatingAddress: bech32String)
{
self = .component(address)
} else if let address = try? IdentityAddress(
validatingAddress: bech32String)
{
self = .identity(address)
} else if let address = try? PackageAddress(validatingAddress: bech32String)
{
self = .package(address)
} else if let address = try? PoolAddress(validatingAddress: bech32String) {
self = .pool(address)
} else if let address = try? ResourceAddress(
validatingAddress: bech32String)
{
self = .resource(address)
} else if let address = try? ValidatorAddress(
validatingAddress: bech32String)
{
self = .validator(address)
} else if let address = try? VaultAddress(validatingAddress: bech32String) {
self = .vault(address)
} else {
struct UnknownAddressType: Swift.Error {}
throw UnknownAddressType()
}
}

public var networkID: NetworkID {
property(\.networkID)
}

public var address: String {
property(\.address)
}

private func property<Property>(_ keyPath: KeyPath<any AddressProtocol, Property>)
-> Property
{
switch self {
case let .accesscontroller(address): address[keyPath: keyPath]
case let .account(address): address[keyPath: keyPath]
case let .component(address): address[keyPath: keyPath]
case let .identity(address): address[keyPath: keyPath]
case let .package(address): address[keyPath: keyPath]
case let .pool(address): address[keyPath: keyPath]
case let .resource(address): address[keyPath: keyPath]
case let .validator(address): address[keyPath: keyPath]
case let .vault(address): address[keyPath: keyPath]
}
}
}
extension Address: CaseIterable {
public typealias AllCases = [Self]
public static var allCases: AllCases {
AccountAddress.allCases.map(Self.account)
+ AccessControllerAddress.allCases.map(Self.accesscontroller)
+ ComponentAddress.allCases.map(Self.component)
+ IdentityAddress.allCases.map(Self.identity)
+ PackageAddress.allCases.map(Self.package)
+ PoolAddress.allCases.map(Self.pool)
+ ResourceAddress.allCases.map(Self.resource)
+ ValidatorAddress.allCases.map(Self.validator)
+ VaultAddress.allCases.map(Self.vault)
}
}
#endif
Loading

0 comments on commit 03690ba

Please sign in to comment.