1
1
import Stream
2
2
3
3
extension XML . Document {
4
- public static func decode( from stream: StreamReader ) async throws -> XML . Document {
4
+ public static func decode(
5
+ from stream: StreamReader
6
+ ) async throws -> XML . Document {
5
7
var document = XML . Document ( )
6
8
try await stream. consumeWhitespaces ( includingNewLine: true )
7
9
8
- guard try await stream. consume ( sequence: Constants . xmlHeaderStart) else {
10
+ guard
11
+ try await stream. consume ( sequence: Constants . xmlHeaderStart)
12
+ else {
9
13
throw XML . Error. invalidXmlHeader
10
14
}
11
15
12
16
try await stream. consumeWhitespaces ( includingNewLine: true )
13
17
14
18
while try await stream. peek ( ) != . questionMark {
15
- try await consumeAttribute ( try await Attribute . decode ( from: stream) , document: & document)
19
+ let attribute = try await Attribute . decode ( from: stream)
20
+ try await consumeAttribute ( attribute, document: & document)
16
21
try await stream. consumeWhitespaces ( includingNewLine: true )
17
22
}
18
23
@@ -26,18 +31,26 @@ extension XML.Document {
26
31
return document
27
32
}
28
33
29
- static func consumeAttribute( _ attribute: Attribute , document: inout XML . Document ) async throws {
34
+ static func consumeAttribute(
35
+ _ attribute: Attribute ,
36
+ document: inout XML . Document
37
+ ) async throws {
30
38
switch attribute. name {
31
- case " version " : document. version = attribute. value
32
- case " encoding " : document. encoding = try . init( from: attribute. value)
33
- case " standalone " : document. standalone = try . init( from: attribute. value)
39
+ case " version " :
40
+ document. version = attribute. value
41
+ case " encoding " :
42
+ document. encoding = try . init( from: attribute. value)
43
+ case " standalone " :
44
+ document. standalone = try . init( from: attribute. value)
34
45
default : break
35
46
}
36
47
}
37
48
}
38
49
39
50
extension XML . Node {
40
- public static func decode( from stream: StreamReader ) async throws -> XML . Node {
51
+ public static func decode(
52
+ from stream: StreamReader
53
+ ) async throws -> XML . Node {
41
54
switch try await stream. peek ( ) {
42
55
case . angleBracketOpen: return . element ( try await . decode( from: stream) )
43
56
default: return . text( try await XML . Node. readText ( from: stream) )
@@ -55,7 +68,9 @@ extension XML.Element {
55
68
struct Name : Equatable {
56
69
let value : String
57
70
58
- static func decode( from stream: StreamReader ) async throws -> XML . Element . Name ? {
71
+ static func decode(
72
+ from stream: StreamReader
73
+ ) async throws -> XML . Element . Name ? {
59
74
guard let value = try await Name . read ( from: stream) else {
60
75
return nil
61
76
}
@@ -72,7 +87,9 @@ extension XML.Element {
72
87
}
73
88
}
74
89
75
- public static func decode( from stream: StreamReader ) async throws -> XML . Element {
90
+ public static func decode(
91
+ from stream: StreamReader
92
+ ) async throws -> XML . Element {
76
93
guard try await stream. consume ( . angleBracketOpen) else {
77
94
throw XML . Error. invalidOpeningTag
78
95
}
@@ -88,7 +105,10 @@ extension XML.Element {
88
105
guard try await stream. consume ( . angleBracketClose) else {
89
106
throw XML . Error. invalidSelfClosingTag
90
107
}
91
- return . init( name: name. value, attributes: attributes. values, children: [ ] )
108
+ return . init(
109
+ name: name. value,
110
+ attributes: attributes. values,
111
+ children: [ ] )
92
112
}
93
113
94
114
// closing bracket
@@ -99,7 +119,9 @@ extension XML.Element {
99
119
// read children
100
120
var children = [ XML . Node] ( )
101
121
try await stream. consumeWhitespaces ( includingNewLine: true )
102
- while !( try await stream. consume ( sequence: [ . angleBracketOpen, . slash] ) ) {
122
+ while !(
123
+ try await stream. consume ( sequence: [ . angleBracketOpen, . slash] )
124
+ ) {
103
125
children. append ( try await XML . Node. decode ( from: stream) )
104
126
try await stream. consumeWhitespaces ( includingNewLine: true )
105
127
}
@@ -115,7 +137,10 @@ extension XML.Element {
115
137
throw XML . Error. invalidClosingTagNameMismatch
116
138
}
117
139
118
- return . init( name: name. value, attributes: attributes. values, children: children)
140
+ return . init(
141
+ name: name. value,
142
+ attributes: attributes. values,
143
+ children: children)
119
144
}
120
145
}
121
146
@@ -140,7 +165,7 @@ extension XML.Standalone {
140
165
}
141
166
142
167
struct Attributes {
143
- var values : [ String : String ]
168
+ var values : [ String : String ]
144
169
145
170
subscript( _ name: String ) -> String ? {
146
171
get { return values [ name] }
@@ -154,7 +179,7 @@ struct Attributes {
154
179
default: return false
155
180
}
156
181
}
157
- var attributes = [ String : String] ( )
182
+ var attributes = [ String: String] ( )
158
183
while !( try await isClosingTag ( ) ) {
159
184
let attribute = try await Attribute . decode ( from: stream)
160
185
guard attributes [ attribute. name] == nil else {
0 commit comments