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

Case key path support #323

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- test
# - benchmarks
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Select Xcode ${{ matrix.xcode }}
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app
- name: System
Expand All @@ -29,15 +29,15 @@ jobs:
run: make ${{ matrix.command }}

ubuntu_tests:
name: Ubuntu (Swift ${{ matrix.swift }})
runs-on: ubuntu-20.04
strategy:
matrix:
os: [ubuntu-20.04]

runs-on: ${{ matrix.os }}

swift:
- '5.9'
steps:
- uses: actions/checkout@v3
- name: Build
run: swift build
- name: Run tests
run: swift test
- uses: swift-actions/setup-swift@v1
with:
swift-version: ${{ matrix.swift }}
- uses: actions/checkout@v4
- run: swift test
13 changes: 11 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-case-paths",
"state" : {
"revision" : "5da6989aae464f324eef5c5b52bdb7974725ab81",
"version" : "1.0.0"
"branch" : "case-key-paths",
"revision" : "7112451690777c2145aa86310d7f205c40e40998"
}
},
{
Expand All @@ -36,6 +36,15 @@
"version" : "1.0.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036",
"version" : "509.0.2"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", from: "1.0.0"),
.package(url: "https://github.com/google/swift-benchmark", from: "0.1.1"),
.package(url: "https://github.com/pointfreeco/swift-case-paths", branch: "case-key-paths"),
],
targets: [
.target(
Expand Down
29 changes: 21 additions & 8 deletions Sources/Parsing/Conversions/Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ extension Conversion {
/// Useful for transforming the output of a ``ParserPrinter`` into an enum:
///
/// ```swift
/// @CasePathable
/// enum Expression {
/// case add(Int, Int)
/// ...
/// }
///
/// let add = ParsePrint(.case(Expression.add)) {
/// Int.parser()
/// "+"
/// Int.parser()
/// struct Add: ParserPrinter{
/// var body: some ParserPrinter<Substring, Expression> {
/// ParsePrint(.case(\Expression.Cases.add)) {
/// Int.parser()
/// "+"
/// Int.parser()
/// }
/// }
/// }
/// try add.parse("1+2") // Expression.add(1, 2)
///
/// try Add().parse("1+2") // Expression.add(1, 2)
/// ```
///
/// To transform the output of a ``ParserPrinter`` into a struct, see ``memberwise(_:)``.
Expand All @@ -27,22 +33,29 @@ extension Conversion {
/// synthesized for structs.
/// - Returns: A conversion that can embed the associated values of an enum case into the case,
/// and extract the associated values from the case.
@inlinable
public static func `case`<Values, Enum: CasePathable>(
_ keyPath: CaseKeyPath<Enum, Values>
) -> Self where Self == AnyCasePath<Enum, Values> {
AnyCasePath(keyPath)
}

@inlinable
public static func `case`<Values, Enum>(
_ initializer: @escaping (Values) -> Enum
) -> Self where Self == CasePath<Enum, Values> {
) -> Self where Self == AnyCasePath<Enum, Values> {
/initializer
}

@inlinable
public static func `case`<Enum>(
_ initializer: Enum
) -> Self where Self == CasePath<Enum, Void> {
) -> Self where Self == AnyCasePath<Enum, Void> {
/initializer
}
}

extension CasePath: Conversion {
extension AnyCasePath: Conversion {
@inlinable
public func apply(_ input: Value) -> Root {
self.embed(input)
Expand Down
Loading