Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SwiftFormat #284

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,25 @@ jobs:
- name: Run rust tests
run: cargo nextest run --all

# Swift formatting
check-swift-format:
name: "Swift format"
if: >
( github.event.action == 'opened' && github.event.pull_request.draft == false ) ||
( github.event.pull_request.draft == false && github.event.action == 'synchronize' ) ||
( github.event.action == 'ready_for_review' ) ||
( github.event_name == 'push' && github.ref == 'refs/heads/main' )
runs-on: macos-15-xlarge

steps:
- uses: RDXWorks-actions/checkout@main

- name: "Run Lint"
run: |
brew update
brew upgrade swiftformat
swiftformat . --lint

# Swift testing on macOS (Apple Silicon)
test-swift:
runs-on: macos-15-xlarge
Expand Down
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ repos:
entry: cargo fmt -- --check
pass_filenames: false

- id: SwiftFormat
name: SwiftFormat
language: system
types: [file, swift]
entry: swiftformat .
pass_filenames: false

- id: clippy
name: clippy
language: system
Expand Down
5 changes: 0 additions & 5 deletions .swift-format

This file was deleted.

13 changes: 13 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--swiftversion 5.9

# format options
--allman false # Prefer `K&R` https://en.wikipedia.org/wiki/Indentation_style#K&R_style
--indent tab
--tabwidth 4

--stripunusedargs closure-only
--enable marktypes
--disable redundantNilInit,redundantSelf,extensionAccessControl
--lineaftermarks false
--ifdef no-indent
--header strip
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading
Loading