1
1
/*
2
2
This source file is part of the Swift.org open source project
3
3
4
- Copyright (c) 2021 Apple Inc. and the Swift project authors
4
+ Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
5
5
Licensed under Apache License v2.0 with Runtime Library Exception
6
6
7
7
See https://swift.org/LICENSE.txt for license information
@@ -21,87 +21,116 @@ import Foundation
21
21
/// > different meaning, as it doesn't quote speech.
22
22
/// ```
23
23
public struct Aside {
24
- /// The kind of aside.
25
- public enum Kind : String , CaseIterable {
24
+ /// Describes the different kinds of aside.
25
+ public struct Kind : RawRepresentable , CaseIterable , Equatable {
26
26
/// A "note" aside.
27
- case note = " Note "
28
-
27
+ public static let note = Kind ( rawValue : " Note " ) !
28
+
29
29
/// A "tip" aside.
30
- case tip = " Tip "
31
-
30
+ public static let tip = Kind ( rawValue : " Tip " ) !
31
+
32
32
/// An "important" aside.
33
- case important = " Important "
34
-
33
+ public static let important = Kind ( rawValue : " Important " ) !
34
+
35
35
/// An "experiment" aside.
36
- case experiment = " Experiment "
37
-
36
+ public static let experiment = Kind ( rawValue : " Experiment " ) !
37
+
38
38
/// A "warning" aside.
39
- case warning = " Warning "
40
-
39
+ public static let warning = Kind ( rawValue : " Warning " ) !
40
+
41
41
/// An "attention" aside.
42
- case attention = " Attention "
43
-
42
+ public static let attention = Kind ( rawValue : " Attention " ) !
43
+
44
44
/// An "author" aside.
45
- case author = " Author "
46
-
45
+ public static let author = Kind ( rawValue : " Author " ) !
46
+
47
47
/// An "authors" aside.
48
- case authors = " Authors "
49
-
48
+ public static let authors = Kind ( rawValue : " Authors " ) !
49
+
50
50
/// A "bug" aside.
51
- case bug = " Bug "
52
-
51
+ public static let bug = Kind ( rawValue : " Bug " ) !
52
+
53
53
/// A "complexity" aside.
54
- case complexity = " Complexity "
55
-
54
+ public static let complexity = Kind ( rawValue : " Complexity " ) !
55
+
56
56
/// A "copyright" aside.
57
- case copyright = " Copyright "
58
-
57
+ public static let copyright = Kind ( rawValue : " Copyright " ) !
58
+
59
59
/// A "date" aside.
60
- case date = " Date "
61
-
60
+ public static let date = Kind ( rawValue : " Date " ) !
61
+
62
62
/// An "invariant" aside.
63
- case invariant = " Invariant "
64
-
63
+ public static let invariant = Kind ( rawValue : " Invariant " ) !
64
+
65
65
/// A "mutatingVariant" aside.
66
- case mutatingVariant = " MutatingVariant "
67
-
66
+ public static let mutatingVariant = Kind ( rawValue : " MutatingVariant " ) !
67
+
68
68
/// A "nonMutatingVariant" aside.
69
- case nonMutatingVariant = " NonMutatingVariant "
70
-
69
+ public static let nonMutatingVariant = Kind ( rawValue : " NonMutatingVariant " ) !
70
+
71
71
/// A "postcondition" aside.
72
- case postcondition = " Postcondition "
73
-
72
+ public static let postcondition = Kind ( rawValue : " Postcondition " ) !
73
+
74
74
/// A "precondition" aside.
75
- case precondition = " Precondition "
76
-
75
+ public static let precondition = Kind ( rawValue : " Precondition " ) !
76
+
77
77
/// A "remark" aside.
78
- case remark = " Remark "
79
-
78
+ public static let remark = Kind ( rawValue : " Remark " ) !
79
+
80
80
/// A "requires" aside.
81
- case requires = " Requires "
82
-
81
+ public static let requires = Kind ( rawValue : " Requires " ) !
82
+
83
83
/// A "since" aside.
84
- case since = " Since "
85
-
84
+ public static let since = Kind ( rawValue : " Since " ) !
85
+
86
86
/// A "todo" aside.
87
- case todo = " ToDo "
88
-
87
+ public static let todo = Kind ( rawValue : " ToDo " ) !
88
+
89
89
/// A "version" aside.
90
- case version = " Version "
91
-
90
+ public static let version = Kind ( rawValue : " Version " ) !
91
+
92
92
/// A "throws" aside.
93
- case `throws` = " Throws "
94
-
93
+ public static let `throws` = Kind ( rawValue: " Throws " ) !
94
+
95
+ /// A collection of preconfigured aside kinds.
96
+ public static var allCases : [ Aside . Kind ] {
97
+ [
98
+ note,
99
+ tip,
100
+ important,
101
+ experiment,
102
+ warning,
103
+ attention,
104
+ author,
105
+ authors,
106
+ bug,
107
+ complexity,
108
+ copyright,
109
+ date,
110
+ invariant,
111
+ mutatingVariant,
112
+ nonMutatingVariant,
113
+ postcondition,
114
+ precondition,
115
+ remark,
116
+ requires,
117
+ since,
118
+ todo,
119
+ version,
120
+ `throws`,
121
+ ]
122
+ }
123
+
124
+ /// The underlying raw string value.
125
+ public var rawValue : String
126
+
127
+ /// Creates an aside kind with the specified raw value.
128
+ /// - Parameter rawValue: The string the aside displays as its title.
95
129
public init ? ( rawValue: String ) {
96
- // Allow lowercase aside prefixes to match.
97
- let casesAndLowercasedRawValues = Kind . allCases. map { ( kind: $0, rawValue: $0. rawValue. lowercased ( ) ) }
98
- guard let matchingCaseAndRawValue = casesAndLowercasedRawValues. first ( where: { $0. rawValue == rawValue. lowercased ( ) } ) else {
99
- return nil
100
- }
101
- self = matchingCaseAndRawValue. kind
130
+ self . rawValue = rawValue
102
131
}
103
132
}
104
-
133
+
105
134
/// The kind of aside interpreted from the initial text of the ``BlockQuote``.
106
135
public var kind : Kind
107
136
@@ -116,14 +145,13 @@ public struct Aside {
116
145
( 0 , Paragraph . self) ,
117
146
( 0 , Text . self) ,
118
147
] ) as? Text ,
119
- let firstColonIndex = initialText. string. firstIndex ( where: { $0 == " : " } ) ,
120
- let kind = Kind ( rawValue: String ( initialText. string [ initialText. string. startIndex..< firstColonIndex] ) ) else {
148
+ let firstColonIndex = initialText. string. firstIndex ( where: { $0 == " : " } ) else {
121
149
// Otherwise, default to a note aside.
122
150
self . kind = . note
123
151
self . content = Array ( blockQuote. blockChildren)
124
152
return
125
153
}
126
- self . kind = kind
154
+ self . kind = Kind ( rawValue : String ( initialText . string [ ..< firstColonIndex ] ) ) !
127
155
128
156
// Trim off the aside tag prefix.
129
157
let trimmedText = initialText. string [ initialText. string. index ( after: firstColonIndex) ... ] . drop {
0 commit comments