forked from swiftlang/swift-evolution-metadata-extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonExtractor.swift
More file actions
93 lines (78 loc) · 3.28 KB
/
Copy pathPersonExtractor.swift
File metadata and controls
93 lines (78 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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 the list of Swift project authors
import Markdown
import EvolutionMetadataModel
struct AuthorExtractor: ValueExtractor {
func extractValue(from src: HeaderFieldSource) -> ExtractionResult<[Proposal.Person]> {
var personExtractor = PersonExtractor(role: .author)
return personExtractor.personArray(from: src)
}
}
struct ReviewManagerExtractor: ValueExtractor {
func extractValue(from src: HeaderFieldSource) -> ExtractionResult<[Proposal.Person]> {
var personExtractor = PersonExtractor(role: .reviewManager)
return personExtractor.personArray(from: src)
}
}
struct PersonExtractor: MarkupWalker {
enum Role {
case author
case reviewManager
}
private var role: Role
private var warnings: [Proposal.Issue] = []
private var errors: [Proposal.Issue] = []
private var personList: [Proposal.Person] = []
init(role: Role) {
self.role = role
}
mutating func personArray(from source: HeaderFieldSource) -> ExtractionResult<[Proposal.Person]> {
let headerLabels = switch role {
case .author: ["Author", "Authors"]
// VALIDATION ENHANCEMENT: Normalize capitalization to 'Review Manager'
case .reviewManager: ["Review manager", "Review Manager", "Review managers", "Review Managers"]
}
if let (_, headerField) = source[headerLabels] {
visit(headerField)
}
// VALIDATION ENHANCEMENT: Log a warning if no review manager. It should be a CI validation error also
return ExtractionResult(value: personList, warnings: warnings, errors: errors)
}
mutating func visitLink(_ link: Link) -> () {
guard let linkInfo = LinkInfo(link: link) else {
return
}
// VALIDATION ENHANCEMENT: Add 'extra markup error' for review managers
if !linkInfo.containsTextElement && role == .author {
errors.append(.authorsHaveExtraMarkup)
}
let destination: String
if let validatedDestination = linkInfo.gitHubDestination {
destination = validatedDestination
} else {
switch role {
case .author: warnings.append(.invalidAuthorLink)
case .reviewManager: warnings.append(.invalidReviewManagerLink)
}
destination = ""
}
let person = Proposal.Person(name: linkInfo.text, link: destination)
personList.append(person)
}
// Find plain names after header label (e.g. Authors: ) or as comma-separated list items
mutating func visitText(_ text: Text) -> () {
let textString = text.string
if let labelMatch = textString.firstMatch(of: /(.*: |, )([^,]*),?/) {
if !labelMatch.2.isEmpty {
// print("'\(labelMatch.2)'")
let person = Proposal.Person(name: String(labelMatch.2))
personList.append(person)
}
}
}
}