Skip to content

Commit 9281f8c

Browse files
authored
Merge pull request #450 from lynchsft/swift_6_language_mode
Swift 6 language mode
2 parents 3d6871d + 8da2672 commit 9281f8c

22 files changed

+128
-112
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
## Main
22

3+
## 6.0.0
4+
5+
##### Breaking
6+
7+
* YamlError.duplicatedKeysInMapping changed the types of its associated values
8+
to conform to Sendable.
9+
[Adora Lynch](https://github.com/lynchsft)
10+
11+
##### Enhancements
12+
13+
* Yams conforms to Swift 6 language mode with full concurrency checking
14+
[Adora Lynch](https://github.com/lynchsft)
15+
16+
##### Bug Fixes
17+
18+
* None.
19+
20+
## 5.3.1
21+
22+
##### Breaking
23+
24+
* None.
25+
326
## 5.4.0
427

528
##### Breaking

Package@swift-6.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// swift-tools-version:6.0
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "Yams",
6+
products: [
7+
.library(name: "Yams", targets: ["Yams"])
8+
],
9+
dependencies: [],
10+
targets: [
11+
.target(
12+
name: "CYaml",
13+
exclude: ["CMakeLists.txt"],
14+
cSettings: [.define("YAML_DECLARE_STATIC")]
15+
),
16+
.target(
17+
name: "Yams",
18+
dependencies: ["CYaml"],
19+
exclude: ["CMakeLists.txt"],
20+
cSettings: [.define("YAML_DECLARE_STATIC")]
21+
),
22+
.testTarget(
23+
name: "YamsTests",
24+
dependencies: ["Yams"],
25+
exclude: ["CMakeLists.txt"],
26+
resources: [
27+
.copy("Fixtures/SourceKitten#289/debug.yaml"),
28+
]
29+
)
30+
]
31+
)

Sources/Yams/Constructor.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public final class Constructor {
6868

6969
extension Constructor {
7070
/// The default `Constructor` to be used with APIs where none is explicitly provided.
71-
public static let `default` = Constructor()
71+
public static var `default`: Constructor { .init() }
7272

7373
/// The default `Tag.Name` to `Node.Scalar` map.
74-
public static let defaultScalarMap: ScalarMap = [
74+
public static var defaultScalarMap: ScalarMap { [
7575
// Failsafe Schema
7676
.str: String.construct,
7777
// JSON Schema
@@ -82,24 +82,24 @@ extension Constructor {
8282
// http://yaml.org/type/index.html
8383
.binary: Data.construct,
8484
.timestamp: Date.construct
85-
]
85+
] }
8686

8787
/// The default `Tag.Name` to `Node.Mapping` map.
88-
public static let defaultMappingMap: MappingMap = [
88+
public static var defaultMappingMap: MappingMap { [
8989
.map: [AnyHashable: Any].construct_mapping,
9090
// http://yaml.org/type/index.html
9191
.set: Set<AnyHashable>.construct_set
9292
// .merge is supported in `[AnyHashable: Any].construct_mapping`.
9393
// .value is supported in `String.construct` and `[AnyHashable: Any].construct_mapping`.
94-
]
94+
] }
9595

9696
/// The default `Tag.Name` to `Node.Sequence` map.
97-
public static let defaultSequenceMap: SequenceMap = [
97+
public static var defaultSequenceMap: SequenceMap { [
9898
.seq: [Any].construct_seq,
9999
// http://yaml.org/type/index.html
100100
.omap: [Any].construct_omap,
101101
.pairs: [Any].construct_pairs
102-
]
102+
] }
103103
}
104104

105105
// MARK: - ScalarConstructible

Sources/Yams/Encoder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,12 @@ struct _YAMLCodingKey: CodingKey { // swiftlint:disable:this type_name
344344
// MARK: -
345345

346346
private extension Node {
347-
static let null = Node("null", Tag(.null))
348-
static let unused = Node("", .unused)
347+
static var null: Node { Node("null", Tag(.null)) }
348+
static var unused: Node { Node("", .unused) }
349349
}
350350

351351
private extension Tag {
352-
static let unused = Tag(.unused)
352+
static var unused: Tag { Tag(.unused) }
353353
}
354354

355355
private extension Tag.Name {

Sources/Yams/Mark.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
/// The pointer position.
10-
public struct Mark {
10+
public struct Mark: Sendable {
1111
/// Line number starting from 1.
1212
public let line: Int
1313
/// Column number starting from 1. libYAML counts columns in `UnicodeScalar`.

Sources/Yams/Parser.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public final class Parser {
132132
/// The default encoding, determined at run time based on the String type's native encoding.
133133
/// This can be overridden by setting `YAMS_DEFAULT_ENCODING` to either `UTF8` or `UTF16`.
134134
/// This value is case insensitive.
135-
public static var `default`: Encoding = {
135+
public static var `default`: Encoding {
136136
let key = "YAMS_DEFAULT_ENCODING"
137137
if let yamsEncoding = ProcessInfo.processInfo.environment[key],
138138
let encoding = Encoding(rawValue: yamsEncoding.lowercased()) {
@@ -142,7 +142,7 @@ public final class Parser {
142142
return encoding
143143
}
144144
return key.utf8.withContiguousStorageIfAvailable({ _ in true }) != nil ? .utf8 : .utf16
145-
}()
145+
}
146146

147147
/// The equivalent `Swift.Encoding` value for `self`.
148148
public var swiftStringEncoding: String.Encoding {
@@ -397,7 +397,13 @@ private extension Parser {
397397
private func checkDuplicates(mappingKeys: [Node]) throws {
398398
let duplicates: [Node: [Node]] = Dictionary(grouping: mappingKeys) { $0 }.filter { $1.count > 1 }
399399
guard duplicates.isEmpty else {
400-
throw YamlError.duplicatedKeysInMapping(duplicates: duplicates, yaml: yaml)
400+
let sortedKeys = duplicates.keys.sorted()
401+
let firstKey = sortedKeys.first!
402+
let firstMark = firstKey.mark ?? .init(line: 0, column: 0)
403+
let duplicates = sortedKeys.map { $0.string ?? "<uncovertable>" }
404+
throw YamlError.duplicatedKeysInMapping(duplicates: duplicates,
405+
context: .init(text: yaml,
406+
mark: firstMark))
401407
}
402408
}
403409

Sources/Yams/Resolver.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import Foundation
1010

1111
/// Class used to resolve nodes to tags based on customizable rules.
12-
public final class Resolver {
12+
public final class Resolver: Sendable {
1313
/// Rule describing how to resolve tags from regex patterns.
14-
public struct Rule {
14+
public struct Rule: Sendable {
1515
/// The tag name this rule applies to.
1616
public let tag: Tag.Name
1717
fileprivate let regexp: NSRegularExpression

Sources/Yams/Tag.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// Tags describe the the _type_ of a Node.
1010
public final class Tag {
1111
/// Tag name.
12-
public struct Name: RawRepresentable, Hashable {
12+
public struct Name: RawRepresentable, Hashable, Sendable {
1313
/// This `Tag.Name`'s raw string value.
1414
public let rawValue: String
1515
/// Create a `Tag.Name` with a raw string value.

Sources/Yams/YamlAnchorProviding.swift

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,5 @@ public protocol YamlAnchorCoding: YamlAnchorProviding {
2222
}
2323

2424
internal extension Node {
25-
static let anchorKeyNode: Self = .scalar(.init(YamlAnchorFunctionNameProvider().getName()))
26-
}
27-
28-
private final class YamlAnchorFunctionNameProvider: YamlAnchorProviding {
29-
30-
fileprivate var functionName: StaticString?
31-
32-
var yamlAnchor: Anchor? {
33-
functionName = #function
34-
return nil
35-
}
36-
37-
func getName() -> StaticString {
38-
_ = yamlAnchor
39-
return functionName!
40-
}
41-
42-
func getName() -> String {
43-
String(describing: getName() as StaticString)
44-
}
25+
static var anchorKeyNode: Self { .scalar(.init("yamlAnchor")) }
4526
}

Sources/Yams/YamlError.swift

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public enum YamlError: Error {
8585
///
8686
/// - parameter duplicates: A dictionary keyed by the duplicated node value, with all nodes that duplicate the value
8787
/// - parameter yaml: YAML String which the problem occured while reading.
88-
case duplicatedKeysInMapping(duplicates: [Node: [Node]], yaml: String)
88+
case duplicatedKeysInMapping(duplicates: [String], context: Context)
8989

9090
/// The error context.
91-
public struct Context: CustomStringConvertible {
91+
public struct Context: CustomStringConvertible, Sendable {
9292
/// Context text.
9393
public let text: String
9494
/// Context position.
@@ -187,20 +187,14 @@ extension YamlError: CustomStringConvertible {
187187
return problem
188188
case .dataCouldNotBeDecoded(encoding: let encoding):
189189
return "String could not be decoded from data using '\(encoding)' encoding"
190-
case let .duplicatedKeysInMapping(duplicates, yaml):
191-
return duplicates.duplicatedKeyErrorDescription(yaml: yaml)
192-
}
193-
}
194-
}
190+
case let .duplicatedKeysInMapping(duplicates, context):
191+
let duplicateKeys = duplicates.sorted().map { "'\($0)'" }.joined(separator: ", ")
192+
return """
193+
Parser: expected all keys to be unique but found the following duplicated key(s): \(duplicateKeys).
194+
Context:
195+
\(context.description)
196+
"""
195197

196-
private extension Dictionary where Key == Node, Value == [Node] {
197-
func duplicatedKeyErrorDescription(yaml: String) -> String {
198-
var error = "error: parser: expected all keys to be unique but found the following duplicated key(s):"
199-
for key in self.keys.sorted() {
200-
let duplicatedNodes = self[key]!
201-
let marks = duplicatedNodes.compactMap { $0.mark }
202-
error += "\n\(key.any) (\(marks)):\n\(marks.map { $0.snippet(from: yaml) }.joined(separator: "\n"))"
203198
}
204-
return error
205199
}
206200
}

0 commit comments

Comments
 (0)