Skip to content

Add primary associated type for MarkupVisitor #135

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

Merged
merged 3 commits into from
May 20, 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7
/*
This source file is part of the Swift.org open source project

Expand Down
4 changes: 2 additions & 2 deletions Sources/Markdown/Visitor/MarkupVisitor.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2021 Apple Inc. and the Swift project authors
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
Expand All @@ -13,7 +13,7 @@
/// - note: This interface only provides requirements for visiting each kind of element. It does not require each visit method to descend into child elements.
///
/// Generally, ``MarkupWalker`` is best for walking a ``Markup`` tree if the ``Result`` type is `Void` or is built up some other way, or ``MarkupRewriter`` for recursively changing a tree's structure. This type serves as a common interface to both. However, for building up other structured result types you can implement ``MarkupVisitor`` directly.
public protocol MarkupVisitor {
public protocol MarkupVisitor<Result> {

/**
The result type returned when visiting a element.
Expand Down
56 changes: 56 additions & 0 deletions Tests/MarkdownTests/Visitors/MarkupVisitorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
This source file is part of the Swift.org open source project

Copyright (c) 2023 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import XCTest
import Markdown

class MarkupVisitorTests: XCTestCase {
struct IntegerConverter: MarkupVisitor {
var value: Int

mutating func defaultVisit(_: Markdown.Markup) -> Int {
defer { value += 1 }
return value
}
}


// A compile time check for PAT support
func testMarkupVisitorPrimaryAssociatedType() {
var visitor: some MarkupVisitor<Int> = IntegerConverter(value: 1)
let markup = Text("")
XCTAssertEqual(visitor.visit(markup), 1)
XCTAssertEqual(visitor.visit(markup), 2)
var mappedVisitor: some MarkupVisitor<Int> = visitor.map { $0 * $0 }
XCTAssertEqual(mappedVisitor.visit(markup), 9)
XCTAssertEqual(mappedVisitor.visit(markup), 16)
XCTAssertEqual(visitor.visit(markup), 3)
}
}

struct _MappVisitor<A: MarkupVisitor, B>: MarkupVisitor {
typealias Result = B
init(visitor: A, _ transform: @escaping (A.Result) -> B) {
self.visitor = visitor
self.transform = transform
}
private var visitor: A
private let transform: (A.Result) -> B

mutating func defaultVisit(_ markup: Markdown.Markup) -> B {
transform(visitor.defaultVisit(markup))
}
}

extension MarkupVisitor {
func map<U>(_ transform: @escaping (Self.Result) -> U) -> some MarkupVisitor<U> {
_MappVisitor(visitor: self, transform)
}
}