Skip to content

Commit 4c21bf3

Browse files
committed
add static function
1 parent 7b7d10e commit 4c21bf3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Sources/VDFlow/StepsCollection.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ public protocol StepsCollection {
2222
///
2323
/// This type is typically an enum generated by the `@Steps` macro.
2424
associatedtype AllSteps: Hashable & Codable & Sendable
25+
2526
/// The currently selected step in the collection.
2627
///
2728
/// Setting this property navigates to the selected step.
2829
var selected: AllSteps { get set }
30+
2931
/// The ID of the last mutation made to this collection.
3032
///
3133
/// This property is used internally for tracking changes to the navigation state.
3234
var _lastMutateID: MutateID? { get }
35+
36+
/// Retrieves the step ID for a specific step key path.
37+
static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps?
3338
}
3439

3540
extension StepsCollection {

Sources/VDFlowMacros/StepsMacro.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,17 @@ public struct StepsMacro: MemberAttributeMacro, ExtensionMacro, MemberMacro, Acc
403403
funcString += "}"
404404
return DeclSyntax(stringLiteral: funcString)
405405
}
406+
let stepFunc: DeclSyntax =
407+
"""
408+
public static func step<T>(for keyPath: WritableKeyPath<Self, StepID<T>>) -> AllSteps? {
409+
switch keyPath {
410+
\(raw: cases.map { "case \\.$\($0), \\._\($0): return .\($0)" }.joined(separator: "\n"))
411+
default:
412+
return nil
413+
}
414+
}
415+
"""
416+
result.append(stepFunc)
406417
return result
407418
}
408419
}

0 commit comments

Comments
 (0)