feat(emitting): add flow collection output
This commit is contained in:
parent
6197c23e6f
commit
5fe9c1b8d1
@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
conservative plain-string output.
|
||||
- Add opt-in literal block scalar emission for safe multiline strings, including
|
||||
`|-` parsing support for stripped final newlines.
|
||||
- Add opt-in flow collection emission for compact mapping and sequence output.
|
||||
- Add Yams and CYaml attribution without copying their implementation into
|
||||
`Sources/`.
|
||||
|
||||
|
||||
@ -8,7 +8,12 @@ public extension PureYAML.Emitting {
|
||||
}
|
||||
|
||||
public func dump(_ value: PureYAML.Model.Value) -> String {
|
||||
render(value, indent: 0).joined(separator: "\n") + "\n"
|
||||
switch options.collectionStyle {
|
||||
case .block:
|
||||
render(value, indent: 0).joined(separator: "\n") + "\n"
|
||||
case .flow:
|
||||
renderFlowDocument(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -58,6 +63,59 @@ extension PureYAML.Emitting.Dumper {
|
||||
}
|
||||
}
|
||||
|
||||
func renderFlowDocument(_ value: PureYAML.Model.Value) -> String {
|
||||
switch value {
|
||||
case .mapping, .sequence:
|
||||
renderFlow(value) + "\n"
|
||||
default:
|
||||
renderScalarNode(value, indent: 0).joined(separator: "\n") + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
func renderFlow(_ value: PureYAML.Model.Value) -> String {
|
||||
switch value {
|
||||
case let .mapping(mapping):
|
||||
renderFlowMapping(mapping)
|
||||
case let .sequence(values):
|
||||
renderFlowSequence(values)
|
||||
default:
|
||||
renderFlowScalar(value)
|
||||
}
|
||||
}
|
||||
|
||||
func renderFlowMapping(_ mapping: PureYAML.Model.Mapping) -> String {
|
||||
let pairs = mapping.pairs
|
||||
.map { pair in
|
||||
"\(quote(pair.key)): \(renderFlow(pair.value))"
|
||||
}
|
||||
.joined(separator: ", ")
|
||||
return "{\(pairs)}"
|
||||
}
|
||||
|
||||
func renderFlowSequence(_ values: [PureYAML.Model.Value]) -> String {
|
||||
let items = values
|
||||
.map(renderFlow)
|
||||
.joined(separator: ", ")
|
||||
return "[\(items)]"
|
||||
}
|
||||
|
||||
func renderFlowScalar(_ value: PureYAML.Model.Value) -> String {
|
||||
switch value {
|
||||
case .null:
|
||||
"null"
|
||||
case let .bool(value):
|
||||
value ? "true" : "false"
|
||||
case let .int(value):
|
||||
String(value)
|
||||
case let .double(value):
|
||||
String(value)
|
||||
case let .string(value):
|
||||
renderFlowString(value)
|
||||
case .sequence, .mapping:
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
func renderScalarNode(
|
||||
_ value: PureYAML.Model.Value,
|
||||
indent: Int,
|
||||
@ -120,6 +178,15 @@ extension PureYAML.Emitting.Dumper {
|
||||
}
|
||||
}
|
||||
|
||||
func renderFlowString(_ value: String) -> String {
|
||||
switch options.scalarStyle {
|
||||
case .plainWhenSafe:
|
||||
canRenderFlowPlainString(value) ? value : quote(value)
|
||||
case .quoted, .literalBlockWhenMultiline:
|
||||
quote(value)
|
||||
}
|
||||
}
|
||||
|
||||
func renderLiteralBlockString(
|
||||
_ value: PureYAML.Model.Value,
|
||||
contentIndent: Int,
|
||||
@ -188,6 +255,18 @@ extension PureYAML.Emitting.Dumper {
|
||||
return !containsColonSpace(value) && !value.contains("#")
|
||||
}
|
||||
|
||||
func canRenderFlowPlainString(_ value: String) -> Bool {
|
||||
canRenderPlainString(value) && !containsFlowDelimiter(value)
|
||||
}
|
||||
|
||||
func containsFlowDelimiter(_ value: String) -> Bool {
|
||||
value.contains(",")
|
||||
|| value.contains("[")
|
||||
|| value.contains("]")
|
||||
|| value.contains("{")
|
||||
|| value.contains("}")
|
||||
}
|
||||
|
||||
func containsColonSpace(_ value: String) -> Bool {
|
||||
var previousWasColon = false
|
||||
for character in value {
|
||||
|
||||
10
Sources/Emitting/Emitting.CollectionStyle.swift
Normal file
10
Sources/Emitting/Emitting.CollectionStyle.swift
Normal file
@ -0,0 +1,10 @@
|
||||
public extension PureYAML.Emitting {
|
||||
/// Collection rendering policy.
|
||||
enum CollectionStyle: Equatable, Sendable {
|
||||
/// Render mappings and sequences with block indentation.
|
||||
case block
|
||||
|
||||
/// Render mappings and sequences with flow delimiters.
|
||||
case flow
|
||||
}
|
||||
}
|
||||
@ -4,9 +4,14 @@ public extension PureYAML.Emitting {
|
||||
public static let `default` = Options()
|
||||
|
||||
public var scalarStyle: ScalarStyle
|
||||
public var collectionStyle: CollectionStyle
|
||||
|
||||
public init(scalarStyle: ScalarStyle = .quoted) {
|
||||
public init(
|
||||
scalarStyle: ScalarStyle = .quoted,
|
||||
collectionStyle: CollectionStyle = .block,
|
||||
) {
|
||||
self.scalarStyle = scalarStyle
|
||||
self.collectionStyle = collectionStyle
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,6 +95,10 @@ struct DumperTests {
|
||||
func test_emptyCollections() {
|
||||
#expect(PureYAML.dump(.mapping(.init())) == "\n")
|
||||
#expect(PureYAML.dump(.sequence([])) == "\n")
|
||||
|
||||
let options = PureYAML.Emitting.Options(collectionStyle: .flow)
|
||||
#expect(PureYAML.dump(.mapping(.init()), options: options) == "{}\n")
|
||||
#expect(PureYAML.dump(.sequence([]), options: options) == "[]\n")
|
||||
}
|
||||
|
||||
@Test("Dumps plain strings only when selected and safe")
|
||||
@ -193,6 +197,82 @@ struct DumperTests {
|
||||
#expect(try PureYAML.parse(yaml) == value)
|
||||
}
|
||||
|
||||
@Test("Dumps selected sequences as flow collections")
|
||||
func test_flowCollectionPolicySequence() throws {
|
||||
let value = PureYAML.Model.Value.sequence([
|
||||
.string("one"),
|
||||
.int(2),
|
||||
.bool(false),
|
||||
])
|
||||
let options = PureYAML.Emitting.Options(collectionStyle: .flow)
|
||||
let yaml = PureYAML.dump(value, options: options)
|
||||
|
||||
#expect(yaml == "[\"one\", 2, false]\n")
|
||||
#expect(!yaml.contains("\n- "))
|
||||
#expect(try PureYAML.parse(yaml) == value)
|
||||
}
|
||||
|
||||
@Test("Dumps selected mappings as flow collections")
|
||||
func test_flowCollectionPolicyMapping() throws {
|
||||
let value = PureYAML.Model.Value.mapping(.init([
|
||||
.init(key: "name", value: .string("Example")),
|
||||
.init(key: "tags", value: .sequence([
|
||||
.string("Swift"),
|
||||
.string("YAML"),
|
||||
])),
|
||||
.init(key: "meta", value: .mapping(.init([
|
||||
.init(key: "enabled", value: .bool(true)),
|
||||
]))),
|
||||
]))
|
||||
let options = PureYAML.Emitting.Options(collectionStyle: .flow)
|
||||
let yaml = PureYAML.dump(value, options: options)
|
||||
|
||||
#expect(yaml == """
|
||||
{"name": "Example", "tags": ["Swift", "YAML"], "meta": {"enabled": true}}
|
||||
|
||||
""")
|
||||
#expect(!yaml.contains("\n "))
|
||||
#expect(try PureYAML.parse(yaml) == value)
|
||||
}
|
||||
|
||||
@Test("Dumps flow scalars with escaping")
|
||||
func test_flowCollectionPolicyEscapesScalars() throws {
|
||||
let value = PureYAML.Model.Value.mapping(.init([
|
||||
.init(key: "a:b", value: .string("quote \" slash \\ comma, bracket [x]")),
|
||||
.init(key: "lines", value: .string("one\ntwo")),
|
||||
]))
|
||||
let options = PureYAML.Emitting.Options(collectionStyle: .flow)
|
||||
let yaml = PureYAML.dump(value, options: options)
|
||||
|
||||
#expect(yaml == """
|
||||
{"a:b": "quote \\" slash \\\\ comma, bracket [x]", "lines": "one\\ntwo"}
|
||||
|
||||
""")
|
||||
#expect(try PureYAML.parse(yaml) == value)
|
||||
}
|
||||
|
||||
@Test("Dumps flow plain strings only when selected and safe")
|
||||
func test_flowCollectionPolicyPlainScalarPolicy() throws {
|
||||
let value = PureYAML.Model.Value.sequence([
|
||||
.string("Example title"),
|
||||
.string("a, b"),
|
||||
.string("true"),
|
||||
.string("bracket [x]"),
|
||||
])
|
||||
let options = PureYAML.Emitting.Options(
|
||||
scalarStyle: .plainWhenSafe,
|
||||
collectionStyle: .flow,
|
||||
)
|
||||
let yaml = PureYAML.dump(value, options: options)
|
||||
|
||||
#expect(yaml == """
|
||||
[Example title, "a, b", "true", "bracket [x]"]
|
||||
|
||||
""")
|
||||
#expect(!yaml.contains("\"Example title\""))
|
||||
#expect(try PureYAML.parse(yaml) == value)
|
||||
}
|
||||
|
||||
@Test("Dumper accepts explicit default options")
|
||||
func test_explicitDefaultOptions() {
|
||||
let value = PureYAML.Model.Value.mapping(.init([
|
||||
|
||||
Loading…
Reference in New Issue
Block a user