Skip to content

Commit 4adafcb

Browse files
committed
Fixed result builder
1 parent b87496c commit 4adafcb

File tree

3 files changed

+99
-8
lines changed

3 files changed

+99
-8
lines changed

Sources/RuleKit/Center.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ extension RuleKit {
118118
RuleKit.internal.rules.append((rule, trigger))
119119
}
120120

121+
@available(*, deprecated, message: "Use setRule(_:, callback:, options:, rule:) instead")
121122
public static func setRule(triggering callback: @escaping @Sendable () -> Void, name: String, options: [any RuleKitOption] = [], _ rule: Rule) {
123+
setRule(name, triggering: callback, options: options, rule)
124+
}
125+
126+
public static func setRule(_ name: String, triggering callback: @escaping @Sendable () -> Void, options: [any RuleKitOption] = [], _ rule: Rule) {
122127
let trigger = CallbackTrigger(rawValue: name, callback: callback)
123128
let rule = options.isEmpty ? rule : RuleWithOptions(options: options, trigger: trigger, rule: rule)
124129
RuleKit.internal.rules.append((rule, trigger))

Sources/RuleKit/Rule.swift

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,42 @@ extension Rule {
3737
}
3838
}
3939

40-
@resultBuilder struct RuleBuilder {
41-
static func buildBlock(_ components: Rule...) -> [any Rule] {
40+
@resultBuilder public struct RuleBuilder {
41+
public static func buildBlock(_ components: Rule...) -> [any Rule] {
4242
components
4343
}
44+
45+
public static func buildBlock(_ components: [Rule]...) -> [Rule] {
46+
components.flatMap { $0 }
47+
}
48+
49+
public static func buildExpression(_ expression: Rule) -> [Rule] {
50+
[expression]
51+
}
52+
53+
public static func buildExpression(_ expression: [Rule]) -> [Rule] {
54+
expression
55+
}
56+
57+
public static func buildOptional(_ component: [Rule]?) -> [Rule] {
58+
component ?? []
59+
}
60+
61+
public static func buildEither(first component: [Rule]) -> [Rule] {
62+
component
63+
}
64+
65+
public static func buildEither(second component: [Rule]) -> [Rule] {
66+
component
67+
}
68+
69+
public static func buildArray(_ components: [[Rule]]) -> [Rule] {
70+
components.flatMap { $0 }
71+
}
72+
73+
public static func buildLimitedAvailability(_ component: [Rule]) -> [Rule] {
74+
component
75+
}
4476
}
4577

4678
// MARK: Event rule
@@ -59,7 +91,7 @@ public struct EventRule: Rule {
5991
}
6092
}
6193

62-
init(event: RuleKit.Event, condition: @escaping (RuleKit.DonatedEvent) -> Bool) {
94+
public init(event: RuleKit.Event, condition: @escaping (RuleKit.DonatedEvent) -> Bool) {
6395
self.event = event
6496
self.condition = condition
6597
}
@@ -84,11 +116,23 @@ public struct AnyOfRule: Rule {
84116
return false
85117
}
86118
}
119+
120+
public init(rules: [any Rule]) {
121+
self.rules = rules
122+
}
123+
124+
public init(@RuleBuilder rules: () -> [any Rule]) {
125+
self.rules = rules()
126+
}
87127
}
88128

89129
extension Rule where Self == AnyOfRule {
130+
public static func anyOf(_ rules: [any Rule]) -> Rule {
131+
AnyOfRule(rules: rules)
132+
}
133+
90134
public static func anyOf(@RuleBuilder _ rules: () -> [any Rule]) -> Rule {
91-
AnyOfRule(rules: rules())
135+
AnyOfRule(rules: rules)
92136
}
93137
}
94138

@@ -105,11 +149,23 @@ public struct AllOfRule: Rule {
105149
return true
106150
}
107151
}
152+
153+
public init(rules: [any Rule]) {
154+
self.rules = rules
155+
}
156+
157+
public init(@RuleBuilder rules: () -> [any Rule]) {
158+
self.rules = rules()
159+
}
108160
}
109161

110162
extension Rule where Self == AllOfRule {
163+
public static func allOf(_ rules: [any Rule]) -> Rule {
164+
AllOfRule(rules: rules)
165+
}
166+
111167
public static func allOf(@RuleBuilder _ rules: () -> [any Rule]) -> Rule {
112-
AllOfRule(rules: rules())
168+
AllOfRule(rules: rules)
113169
}
114170
}
115171

Tests/RuleKitTests/RuleKitTests.swift

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//
2727

2828
import XCTest
29-
@testable import RuleKit
29+
import RuleKit
3030

3131
extension RuleKit.Event {
3232
static let testEvent: Self = "test.event"
@@ -44,6 +44,20 @@ final class RuleKitTests: XCTestCase {
4444
}
4545

4646
func testNotificationRuleTriggering() async throws {
47+
await RuleKit.Event.testEvent.reset()
48+
await RuleKit.setRule(triggering: Self.testNotification, .anyOf([
49+
EventRule(event: .testEvent) {
50+
$0.donations.count > 0
51+
}
52+
]))
53+
let expectation = expectation(forNotification: Self.testNotification, object: nil)
54+
await RuleKit.Event.testEvent.donate()
55+
await fulfillment(of: [expectation])
56+
let count = await RuleKit.Event.testEvent.donations.count
57+
XCTAssertEqual(1, count)
58+
}
59+
60+
func testNotificationRuleTriggeringResultBuilder() async throws {
4761
await RuleKit.Event.testEvent.reset()
4862
await RuleKit.setRule(triggering: Self.testNotification, .anyOf {
4963
EventRule(event: .testEvent) {
@@ -60,9 +74,25 @@ final class RuleKitTests: XCTestCase {
6074
func testCallbackRuleTriggering() async throws {
6175
await RuleKit.Event.testEvent.reset()
6276
let expectation = XCTestExpectation()
63-
await RuleKit.setRule(triggering: {
77+
await RuleKit.setRule(Self.testCallback, triggering: {
78+
expectation.fulfill()
79+
}, .anyOf([
80+
EventRule(event: .testEvent) {
81+
$0.donations.count > 0
82+
}
83+
]))
84+
await RuleKit.Event.testEvent.donate()
85+
await fulfillment(of: [expectation])
86+
let count = await RuleKit.Event.testEvent.donations.count
87+
XCTAssertEqual(1, count)
88+
}
89+
90+
func testCallbackRuleTriggeringResultBuilder() async throws {
91+
await RuleKit.Event.testEvent.reset()
92+
let expectation = XCTestExpectation()
93+
await RuleKit.setRule(Self.testCallback, triggering: {
6494
expectation.fulfill()
65-
}, rawValue: Self.testCallback, .anyOf {
95+
}, .anyOf {
6696
EventRule(event: .testEvent) {
6797
$0.donations.count > 0
6898
}

0 commit comments

Comments
 (0)