Skip to content

Commit 49879f1

Browse files
committed
Add .count and .date properties to Calendar.RecurrenceRule.End
`Calendar.RecurrenceRule.End` is de-facto an enum, but has been implemented as a struct so we can more easily extend it in the future without breaking ABI. Sadly the current API only provides a way to construct the struct, and does not let us introspect the associated values. This change adds a couple read-only properties to the struct to address this. While at it, we also make the struct conform to `CustomStringConvertible`, so it doesn't leak implementation details when it's printed in the debugger.
1 parent aca65ac commit 49879f1

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ list(APPEND _SwiftFoundation_versions
9696
"0.1"
9797
"0.2"
9898
"0.3"
99-
"0.4")
99+
"0.4"
100+
"6.0.2"
101+
)
100102

101103
# Each availability name to define
102104
list(APPEND _SwiftFoundation_availability_names

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let availabilityTags: [_Availability] = [
1111
_Availability("FoundationPredicate", availability: .macOS14_0), // Predicate relies on pack parameter runtime support
1212
_Availability("FoundationPredicateRegex", availability: .macOS15_0) // Predicate regexes rely on new stdlib APIs
1313
]
14-
let versionNumbers = ["0.1", "0.2", "0.3", "0.4"]
14+
let versionNumbers = ["0.1", "0.2", "0.3", "0.4", "6.0.2"]
1515

1616
// Availability Macro Utilities
1717

Sources/FoundationEssentials/Calendar/Calendar_Recurrence.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ extension Calendar {
335335

336336
dates = dates.filter { $0 >= self.start }
337337

338-
if let limit = recurrence.end.until {
338+
if let limit = recurrence.end._date {
339339
let hadDates = !dates.isEmpty
340340
dates = dates.filter { $0 <= limit }
341341
if hadDates && dates.isEmpty {
@@ -363,15 +363,15 @@ extension Calendar {
363363

364364
mutating func next() -> Element? {
365365
guard !finished else { return nil }
366-
if let limit = recurrence.end.count, resultsFound >= limit {
366+
if let limit = recurrence.end._count, resultsFound >= limit {
367367
finished = true
368368
return nil
369369
}
370370

371371
while !finished {
372372
if let date = currentGroup.popLast() {
373373
resultsFound += 1
374-
if let limit = recurrence.end.until, date > limit {
374+
if let limit = recurrence.end._date, date > limit {
375375
finished = true
376376
return nil
377377
}

Sources/FoundationEssentials/Calendar/RecurrenceRule.swift

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,30 @@ extension Calendar {
135135
public static var never: Self {
136136
.init(_guts: .never)
137137
}
138-
139-
internal var until: Date? {
138+
139+
/// At most many times the event may occur
140+
/// This value is set when the struct was initialized with `.afterOccurrences()`
141+
@available(FoundationPreview 6.0.2, *)
142+
public var occurrences: Int? {
143+
self._count
144+
}
145+
internal var _count: Int? {
140146
switch _guts {
141-
case let .afterDate(date): date
147+
case let .afterOccurrences(count): count
142148
default: nil
143149
}
144150
}
145-
internal var count: Int? {
151+
152+
/// The latest date when the event may occur
153+
/// This value is set when the struct was initialized with `.afterDate()`
154+
@available(FoundationPreview 6.0.2, *)
155+
public var date: Date? {
156+
self._date
157+
}
158+
159+
internal var _date: Date? {
146160
switch _guts {
147-
case let .afterOccurrences(count): count
161+
case let .afterDate(date): date
148162
default: nil
149163
}
150164
}
@@ -440,3 +454,14 @@ extension Calendar.RecurrenceRule: Codable {
440454
try container.encode(setPositions, forKey: .setPositions)
441455
}
442456
}
457+
458+
@available(FoundationPreview 6.0.2, *)
459+
extension Calendar.RecurrenceRule.End: CustomStringConvertible {
460+
public var description: String {
461+
switch self._guts {
462+
case .never: "Never"
463+
case .afterDate(let date): "After \(date)"
464+
case .afterOccurrences(let n): "After \(n) occurrence(s)"
465+
}
466+
}
467+
}

0 commit comments

Comments
 (0)