Skip to content

Commit f12bd30

Browse files
committed
Fix code style
1 parent 29535b2 commit f12bd30

File tree

6 files changed

+71
-52
lines changed

6 files changed

+71
-52
lines changed

Sources/XML/XML+Convenience.swift

+20-26
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,13 @@ extension XML.Element {
4141
}
4242

4343
public subscript(_ name: String) -> XML.Node? {
44-
get {
45-
return self.children.first(where: { node in
46-
guard let element = XML.Element(node),
47-
element.name == name else {
48-
return false
49-
}
50-
return true
51-
})
52-
}
44+
self.children.first(where: { node in
45+
guard let element = XML.Element(node),
46+
element.name == name else {
47+
return false
48+
}
49+
return true
50+
})
5351
}
5452

5553
public init?(_ node: XML.Node) {
@@ -66,7 +64,7 @@ extension XML.Element {
6664
self.init(node)
6765
}
6866

69-
public init(name: String, attributes: [String : String], value: String) {
67+
public init(name: String, attributes: [String: String], value: String) {
7068
self.name = name
7169
self.attributes = attributes
7270
self.children = [.text(value)]
@@ -89,18 +87,16 @@ extension XML.Node {
8987
}
9088

9189
public subscript(_ name: String) -> XML.Node? {
92-
get {
93-
guard let element = XML.Element(self) else {
94-
return nil
95-
}
96-
return element.children.first(where: { node in
97-
guard let element = XML.Element(node),
98-
element.name == name else {
99-
return false
100-
}
101-
return true
102-
})
90+
guard let element = XML.Element(self) else {
91+
return nil
10392
}
93+
return element.children.first(where: { node in
94+
guard let element = XML.Element(node),
95+
element.name == name else {
96+
return false
97+
}
98+
return true
99+
})
104100
}
105101
}
106102

@@ -120,12 +116,10 @@ extension Optional where Wrapped == XML.Node {
120116
}
121117

122118
public subscript(_ name: String) -> XML.Node? {
123-
get {
124-
guard let node = self else {
125-
return nil
126-
}
127-
return node[name]
119+
guard let node = self else {
120+
return nil
128121
}
122+
return node[name]
129123
}
130124
}
131125

Sources/XML/XML+Decode.swift

+40-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import Stream
22

33
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 {
57
var document = XML.Document()
68
try await stream.consumeWhitespaces(includingNewLine: true)
79

8-
guard try await stream.consume(sequence: Constants.xmlHeaderStart) else {
10+
guard
11+
try await stream.consume(sequence: Constants.xmlHeaderStart)
12+
else {
913
throw XML.Error.invalidXmlHeader
1014
}
1115

1216
try await stream.consumeWhitespaces(includingNewLine: true)
1317

1418
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)
1621
try await stream.consumeWhitespaces(includingNewLine: true)
1722
}
1823

@@ -26,18 +31,26 @@ extension XML.Document {
2631
return document
2732
}
2833

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 {
3038
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)
3445
default: break
3546
}
3647
}
3748
}
3849

3950
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 {
4154
switch try await stream.peek() {
4255
case .angleBracketOpen: return .element(try await .decode(from: stream))
4356
default: return .text(try await XML.Node.readText(from: stream))
@@ -55,7 +68,9 @@ extension XML.Element {
5568
struct Name: Equatable {
5669
let value: String
5770

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? {
5974
guard let value = try await Name.read(from: stream) else {
6075
return nil
6176
}
@@ -72,7 +87,9 @@ extension XML.Element {
7287
}
7388
}
7489

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 {
7693
guard try await stream.consume(.angleBracketOpen) else {
7794
throw XML.Error.invalidOpeningTag
7895
}
@@ -88,7 +105,10 @@ extension XML.Element {
88105
guard try await stream.consume(.angleBracketClose) else {
89106
throw XML.Error.invalidSelfClosingTag
90107
}
91-
return .init(name: name.value, attributes: attributes.values, children: [])
108+
return .init(
109+
name: name.value,
110+
attributes: attributes.values,
111+
children: [])
92112
}
93113

94114
// closing bracket
@@ -99,7 +119,9 @@ extension XML.Element {
99119
// read children
100120
var children = [XML.Node]()
101121
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+
) {
103125
children.append(try await XML.Node.decode(from: stream))
104126
try await stream.consumeWhitespaces(includingNewLine: true)
105127
}
@@ -115,7 +137,10 @@ extension XML.Element {
115137
throw XML.Error.invalidClosingTagNameMismatch
116138
}
117139

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)
119144
}
120145
}
121146

@@ -140,7 +165,7 @@ extension XML.Standalone {
140165
}
141166

142167
struct Attributes {
143-
var values: [String : String]
168+
var values: [String: String]
144169

145170
subscript(_ name: String) -> String? {
146171
get { return values[name] }
@@ -154,7 +179,7 @@ struct Attributes {
154179
default: return false
155180
}
156181
}
157-
var attributes = [String : String]()
182+
var attributes = [String: String]()
158183
while !(try await isClosingTag()) {
159184
let attribute = try await Attribute.decode(from: stream)
160185
guard attributes[attribute.name] == nil else {

Sources/XML/XML.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public struct XML {
1313
}
1414

1515
public struct Document {
16-
public var version: String? = nil
17-
public var encoding: Encoding? = nil
18-
public var standalone: Standalone? = nil
16+
public var version: String?
17+
public var encoding: Encoding?
18+
public var standalone: Standalone?
1919

20-
public var root: Element? = nil
20+
public var root: Element?
2121

2222
public init(
2323
root: Element? = nil,
@@ -34,12 +34,12 @@ public struct XML {
3434

3535
public struct Element {
3636
public var name: String
37-
public var attributes: [String : String]
37+
public var attributes: [String: String]
3838
public var children: [Node]
3939

4040
public init(
4141
name: String,
42-
attributes: [String : String] = [:],
42+
attributes: [String: String] = [:],
4343
children: [Node] = []
4444
) {
4545
self.name = name

Tests/XML/Decode/main.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ test("SelfElementAttributes") {
119119
let element = try await XML.Element.decode(from: stream)
120120
expect(element == XML.Element(
121121
name: "element",
122-
attributes: ["name" : "value"]
122+
attributes: ["name": "value"]
123123
))
124124
}
125125

@@ -129,7 +129,7 @@ test("TextElementAttributes") {
129129
let element = try await XML.Element.decode(from: stream)
130130
expect(element == XML.Element(
131131
name: "element",
132-
attributes: ["name" : "value"],
132+
attributes: ["name": "value"],
133133
children: [.text("text")]
134134
))
135135
}

Tests/XML/Encode/main.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ test("ElementChildren") {
9292
test("SelfElementAttributes") {
9393
let element = XML.Element(
9494
name: "element",
95-
attributes: ["name" : "value"])
95+
attributes: ["name": "value"])
9696

9797
let xml = "<element name=\"value\"/>"
9898

@@ -104,7 +104,7 @@ test("SelfElementAttributes") {
104104
test("TextElementAttributes") {
105105
let element = XML.Element(
106106
name: "element",
107-
attributes: ["name" : "value"],
107+
attributes: ["name": "value"],
108108
children: [.text("text")])
109109

110110
let xml = "<element name=\"value\">text</element>"

Tests/XML/String/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test("Element") {
2121
test("Attributes") {
2222
let element = XML.Element(
2323
name: "element",
24-
attributes: ["name" : "value"])
24+
attributes: ["name": "value"])
2525

2626
expect(element.xml == "<element name=\"value\"/>\n")
2727
expect(element.xmlCompact == "<element name=\"value\"/>")

0 commit comments

Comments
 (0)