Skip to content

Commit

Permalink
Apply SwiftFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbzurovski committed Nov 29, 2024
1 parent 838e5e9 commit ff7aa1c
Show file tree
Hide file tree
Showing 652 changed files with 5,532 additions and 7,538 deletions.
22 changes: 11 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import PackageDescription

var swiftSettings: [SwiftSetting] = [
.enableExperimentalFeature("StrictConcurrency")
.enableExperimentalFeature("StrictConcurrency"),
]

let sargonBinaryTargetName = "SargonCoreRS"
Expand All @@ -24,7 +24,7 @@ if useLocalFramework {
binaryTarget = .binaryTarget(
name: sargonBinaryTargetName,
url:
"https://github.com/radixdlt/sargon/releases/download/\(releaseTag)/libsargon-rs.xcframework.zip",
"https://github.com/radixdlt/sargon/releases/download/\(releaseTag)/libsargon-rs.xcframework.zip",
checksum: releaseChecksum
)
}
Expand All @@ -38,22 +38,22 @@ let package = Package(
.library(
name: "Sargon",
targets: ["Sargon"]
)
),
],
dependencies: [
// We use XCTestDynamicOverlay to have different `description` of e.g. Decimal192
// We use XCTestDynamicOverlay to have different `description` of e.g. Decimal192
// for tests vs not tests (we use a .test `Locale`)
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.1.2"),

// `XCTAssertNoDifference` used in test
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.0"),

// Hopefully only temporary! We use `SwiftJSON` to be able to mark some Sargon models
// as `Swift.Codable`. See the SargonObjectCodable protocol for details.
// In the future hopefully no JSON coding happens in wallets,
// i.e. Sargon does ALL JSON coding, then we can remove this.
// Hopefully only temporary! We use `SwiftJSON` to be able to mark some Sargon models
// as `Swift.Codable`. See the SargonObjectCodable protocol for details.
// In the future hopefully no JSON coding happens in wallets,
// i.e. Sargon does ALL JSON coding, then we can remove this.
.package(url: "https://github.com/SwiftyJSON/SwiftyJSON", from: "5.0.2"),

// Multicast / Share of notifications in EventBus
.package(url: "https://github.com/sideeffect-io/AsyncExtensions", exact: "0.5.3"),
],
Expand All @@ -70,7 +70,7 @@ let package = Package(
.target(name: "SargonUniFFI"),
"SwiftyJSON",
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
"AsyncExtensions"
"AsyncExtensions",
],
path: "apple/Sources/Sargon",
swiftSettings: swiftSettings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-05-03.
//

import Foundation
import SargonUniFFI

Expand All @@ -16,19 +9,21 @@ extension EntropyProviderDriver where Self == EntropyProvider {
public static var shared: Self { Self.shared }
}

// MARK: - EntropyProvider
/// An `EntropyProviderDriver` actor which uses CSRNG `SystemRandomNumberGenerator`
/// to generate 32 bytes.
public final actor EntropyProvider {
internal init() {}
init() {}

/// Singleton `EntropyProviderDriver` of type `EntropyProvider`,
/// being an `actor` that uses CSRNG `SystemRandomNumberGenerator`
public static let shared = EntropyProvider()
}

// MARK: EntropyProviderDriver
extension EntropyProvider: EntropyProviderDriver {
/// Generates 32 bytes using CSRNG `SystemRandomNumberGenerator`
nonisolated public func generateSecureRandomBytes() -> Entropy32Bytes {
public nonisolated func generateSecureRandomBytes() -> Entropy32Bytes {
Entropy32Bytes.generate()
}
}
27 changes: 10 additions & 17 deletions apple/Sources/Sargon/Drivers/EventBus/EventBus.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-05-05.
//

import AsyncExtensions
import Foundation
import SargonUniFFI
import AsyncExtensions

// Makes it possible to type `.shared` on an initalizer/func taking
// `some EventBusDriver` as parameter.
extension EventBusDriver where Self == EventBus {

/// Singleton `EventBusDriver` of type `EventBus` being an `actor` which forwards `EventNotification`s
/// originally emitted by `SargonOS` (Rust side).
public static var shared: Self { Self.shared }
}

// MARK: - EventBus
/// An `EventBusDriver` actor which handles incoming
/// `EventNotifications` and forwards them to any
/// subscriber of `notifications()`, being a multicasted
Expand All @@ -26,35 +19,35 @@ public final actor EventBus {
/// A stream we multicast on.
private let stream = AsyncThrowingPassthroughSubject<Element, any Error>()
private let subject: Subject
#if DEBUG
#if DEBUG
public init() {
subject = .init()
}
#else
#else
private init() {
subject = .init()
}
#endif
#endif
}

extension EventBus {

public typealias Element = EventNotification
public typealias Subject = AsyncPassthroughSubject<Element>

/// Singleton `EventBusDriver` of type `EventBus` being an `actor` which forwards `EventNotification`s
/// originally emitted by `SargonOS` (Rust side).
public static let shared = EventBus()

/// A multicasted async sequence of `EventNotification` values
/// over time, originally emitted by `SargonOS` (Rust side).
public func notifications() -> AsyncMulticastSequence<EventBus.Subject, AsyncThrowingPassthroughSubject<EventBus.Element, any Error>> {
subject
.multicast(stream)
.autoconnect()
.multicast(stream)
.autoconnect()
}
}

// MARK: EventBusDriver
extension EventBus: EventBusDriver {
/// This method is called by `SargonOS` (Rust side) and we should
/// "forward" the events to subscribers (Swift swide), i.e. `@SharedReader`s of profile values,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-05-15.
//

import Foundation
import SargonUniFFI

Expand All @@ -18,15 +11,17 @@ extension EventProfileModified {
extension Event {
public var profileModified: EventProfileModified? {
switch self {
case let .profileModified(change): return change
default: return nil
case let .profileModified(change): change
default: nil
}
}

public var addressOfNewAccount: AccountAddress? {
profileModified?.addedAccount
}
}

// MARK: - EventNotification + Comparable
extension EventNotification: Comparable {
/// `EventNotification` are made `Comparable` by
/// sorting on `timestamp`.
Expand All @@ -36,7 +31,6 @@ extension EventNotification: Comparable {
}

extension Event {

/// Discriminant of the `Event`.
public var kind: EventKind {
eventKind(event: self)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-05-05.
//

import Foundation
import SargonUniFFI

// MARK: - FileManager + @unchecked Sendable
extension FileManager: @unchecked Sendable {}

// Makes it possible to type `.shared` on an initalizer/func taking
// `some FileSystemDriver` as parameter.
extension FileSystemDriver where Self == FileSystem {

/// Singleton `FileSystemDriver` of type `FileSystem`being an `actor` which
/// uses a `FileManager` for File I/O CRUD operations.
public static var shared: Self { Self.shared }
}

// MARK: - FileSystem
/// `FileSystemDriver` being an `actor` which
/// uses a `FileManager` for File I/O CRUD operations.
public final actor FileSystem {
private let fileManager: FileManager
public init(fileManager: FileManager = .default) {
self.fileManager = fileManager
}

/// Singleton `FileSystemDriver` of type `FileSystem`being an `actor` which
/// uses a `FileManager` for File I/O CRUD operations.
public static let shared = FileSystem(fileManager: .default)
Expand All @@ -44,28 +38,28 @@ extension FileSystem {
_ io: @Sendable (URL) throws -> T
) throws -> T {
let url = URL(file: path)
#if os(macOS)
guard url.startAccessingSecurityScopedResource() else {
throw CommonError.NotPermissionToAccessFile(path: path)
}
defer { url.stopAccessingSecurityScopedResource() }
#endif
#if os(macOS)
guard url.startAccessingSecurityScopedResource() else {
throw CommonError.NotPermissionToAccessFile(path: path)
}
defer { url.stopAccessingSecurityScopedResource() }
#endif
return try io(url)
}
}

extension FileSystem {
private static func appDirPathNotNecessarilyExisting(fileManager: FileManager) throws -> String {
#if os(iOS)
#if os(iOS)
return try fileManager.urls(
for: .cachesDirectory,
in: .userDomainMask
).first!.path()
#elseif os(macOS)
#elseif os(macOS)
URL.temporaryDirectory.path()
#else
#else
fatalError("Unsupported OS")
#endif
#endif
}
}

Expand All @@ -80,39 +74,39 @@ extension FileSystem {
}
#endif

// MARK: - FileSystem + FileSystemDriver
extension FileSystem: FileSystemDriver {

public func writableAppDirPath() async throws -> String {
public func writableAppDirPath() async throws -> String {
try with(path: Self.appDirPathNotNecessarilyExisting(fileManager: fileManager)) {
let directoryExists = fileManager.fileExists(atPath: $0.path())
if !directoryExists {
try fileManager.createDirectory(at: $0, withIntermediateDirectories: true)
}
return $0.path()
}
}
}

public func loadFromFile(path: String) async throws -> BagOfBytes? {
return try with(path: path) {
let fileExists = fileManager.fileExists(atPath: $0.path())
do {
return try Data(contentsOf: $0)
} catch {
if fileExists {
throw error
} else {
return nil
}
}
try with(path: path) {
let fileExists = fileManager.fileExists(atPath: $0.path())
do {
return try Data(contentsOf: $0)
} catch {
if fileExists {
throw error
} else {
return nil
}
}
}
}
public func saveToFile(path: String, data: BagOfBytes, isAllowedToOverwrite: Bool) async throws {
try with(path: path) {

public func saveToFile(path: String, data: BagOfBytes, isAllowedToOverwrite: Bool) async throws {
try with(path: path) {
try data.write(to: $0, options: isAllowedToOverwrite ? [] : [.withoutOverwriting])
}
}
}
}

public func deleteFile(path: String) async throws {
try with(path: path) {
try fileManager.removeItem(at: $0)
Expand Down
Loading

0 comments on commit ff7aa1c

Please sign in to comment.