Skip to content

Commit

Permalink
Adress Swiftlint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Adora Lynch committed Oct 11, 2024
1 parent 8f1409b commit 27afe7f
Show file tree
Hide file tree
Showing 19 changed files with 515 additions and 445 deletions.
10 changes: 4 additions & 6 deletions Sources/Yams/Anchor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
import Foundation

public final class Anchor: RawRepresentable, ExpressibleByStringLiteral, Codable, Hashable {

public static let permittedCharacters = CharacterSet.lowercaseLetters
.union(.uppercaseLetters)
.union(.decimalDigits)
.union(.init(charactersIn: "-_"))

public static func is_cyamlAlpha(_ string: String) -> Bool {
Anchor.permittedCharacters.isSuperset(of: .init(charactersIn: string))
}


public let rawValue: String

public init(rawValue: String) {
self.rawValue = rawValue
}

public init(stringLiteral value: String) {
rawValue = value
}
Expand All @@ -33,4 +32,3 @@ public final class Anchor: RawRepresentable, ExpressibleByStringLiteral, Codable
extension Anchor: CustomStringConvertible {
public var description: String { rawValue }
}

18 changes: 9 additions & 9 deletions Sources/Yams/Constructor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public final class Constructor {
if let method = mappingMap[node.tag.name], let result = method(mapping) {
return result
}
return [AnyHashable: Any]._construct_mapping(from: mapping)
return [AnyHashable: Any].private_construct_mapping(from: mapping)
case .sequence(let sequence):
if let method = sequenceMap[node.tag.name], let result = method(sequence) {
return result
}
return [Any].construct_seq(from: sequence)
case .alias(_):
case .alias:
preconditionFailure("Aliases should be resolved before construction")
}
}
Expand Down Expand Up @@ -272,7 +272,7 @@ extension ScalarConstructible where Self: FloatingPoint & SexagesimalConvertible
}

private extension FixedWidthInteger where Self: SexagesimalConvertible {
static func _construct(from scalar: Node.Scalar) -> Self? {
static func private_construct(from scalar: Node.Scalar) -> Self? {
guard scalar.style == .any || scalar.style == .plain else {
return nil
}
Expand Down Expand Up @@ -317,7 +317,7 @@ extension Int: ScalarConstructible {
///
/// - returns: An instance of `Int`, if one was successfully extracted from the scalar.
public static func construct(from scalar: Node.Scalar) -> Int? {
return _construct(from: scalar)
return private_construct(from: scalar)
}
}

Expand All @@ -330,7 +330,7 @@ extension UInt: ScalarConstructible {
///
/// - returns: An instance of `UInt`, if one was successfully extracted from the scalar.
public static func construct(from scalar: Node.Scalar) -> UInt? {
return _construct(from: scalar)
return private_construct(from: scalar)
}
}

Expand All @@ -343,7 +343,7 @@ extension Int64: ScalarConstructible {
///
/// - returns: An instance of `Int64`, if one was successfully extracted from the scalar.
public static func construct(from scalar: Node.Scalar) -> Int64? {
return _construct(from: scalar)
return private_construct(from: scalar)
}
}

Expand All @@ -356,7 +356,7 @@ extension UInt64: ScalarConstructible {
///
/// - returns: An instance of `UInt64`, if one was successfully extracted from the scalar.
public static func construct(from scalar: Node.Scalar) -> UInt64? {
return _construct(from: scalar)
return private_construct(from: scalar)
}
}

Expand Down Expand Up @@ -420,12 +420,12 @@ extension Dictionary {
///
/// - returns: An instance of `[AnyHashable: Any]`, if one was successfully extracted from the mapping.
public static func construct_mapping(from mapping: Node.Mapping) -> [AnyHashable: Any]? {
return _construct_mapping(from: mapping)
return private_construct_mapping(from: mapping)
}
}

private extension Dictionary {
static func _construct_mapping(from mapping: Node.Mapping) -> [AnyHashable: Any] {
static func private_construct_mapping(from mapping: Node.Mapping) -> [AnyHashable: Any] {
let mapping = mapping.flatten()
// TODO: YAML supports keys other than str.
return [AnyHashable: Any](
Expand Down
14 changes: 8 additions & 6 deletions Sources/Yams/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class YAMLDecoder {
let parser = try Parser(yaml: yaml, resolver: Resolver([.merge]), encoding: encoding)
// ^ the parser holds the references to Anchors while parsing,
return try withExtendedLifetime(parser) {
//^ so we hold an explicit reference to the parser during decoding
// ^ so we hold an explicit reference to the parser during decoding
let node = try parser.singleRoot() ?? ""
// ^ nodes only have weak references to Anchors (the Anchors would disappear if not held by the parser)
return try self.decode(type, from: node, userInfo: userInfo)
Expand Down Expand Up @@ -151,24 +151,24 @@ private struct _KeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainerPr

init(decoder: _Decoder, wrapping mapping: Node.Mapping) {
self.decoder = decoder

let keys = mapping.keys

let decodeAnchor: Anchor?
let decodeTag: Tag?

if let anchor = mapping.anchor, keys.contains(.anchorKeyNode) == false {
decodeAnchor = anchor
} else {
decodeAnchor = nil
}

if mapping.tag.name != .implicit && keys.contains(.tagKeyNode) == false {
decodeTag = mapping.tag
} else {
decodeTag = nil
}

switch (decodeAnchor, decodeTag) {
case (nil, nil):
self.mapping = mapping
Expand Down Expand Up @@ -426,3 +426,5 @@ extension YAMLDecoder: TopLevelDecoder {
}
}
#endif

// swiftlint:disable:this file_length
18 changes: 7 additions & 11 deletions Sources/Yams/Emitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,10 @@ public final class Emitter {

/// Set the style for scalars that include newlines
public var newLineScalarStyle: Node.Scalar.Style = .any

/// Redundancy aliasing strategy to use when encoding. Defaults to nil
public var redundancyAliasingStrategy: RedundancyAliasingStrategy?

/// Create `Emitter.Options` with the specified values.
///
/// - parameter canonical: Set if the output should be in the "canonical" format described in the YAML
Expand All @@ -290,7 +290,8 @@ public final class Emitter {
/// - parameter sequenceStyle: Set the style for sequences (arrays / lists)
/// - parameter mappingStyle: Set the style for mappings (dictionaries)
/// - parameter newLineScalarStyle: Set the style for newline-containing scalars
/// - parameter redundancyAliasingStrategy: Set the strategy for identifying redundant structures and automatically aliasing them
/// - parameter redundancyAliasingStrategy: Set the strategy for identifying
/// redundant structures and automatically aliasing them
public init(canonical: Bool = false, indent: Int = 0, width: Int = 0, allowUnicode: Bool = false,
lineBreak: Emitter.LineBreak = .ln,
explicitStart: Bool = false,
Expand Down Expand Up @@ -338,7 +339,8 @@ public final class Emitter {
/// - parameter sequenceStyle: Set the style for sequences (arrays / lists)
/// - parameter mappingStyle: Set the style for mappings (dictionaries)
/// - parameter newLineScalarStyle: Set the style for newline-containing scalars
/// - parameter redundancyAliasingStrategy: Set the strategy for identifying redundant structures and automatically aliasing them
/// - parameter redundancyAliasingStrategy: Set the strategy for identifying redundant
/// structures and automatically aliasing them
public init(canonical: Bool = false,
indent: Int = 0,
width: Int = 0,
Expand Down Expand Up @@ -466,12 +468,6 @@ public final class Emitter {
}
}

//// MARK: - Options Initializer

extension Emitter.Options {

}

// MARK: Implementation Details

extension Emitter {
Expand All @@ -489,7 +485,7 @@ extension Emitter {
case .alias(let alias): try serializeAlias(alias)
}
}

private func serializeAlias(_ alias: Node.Alias) throws {
var event = yaml_event_t()
let anchor = alias.anchor.rawValue
Expand Down
5 changes: 3 additions & 2 deletions Sources/Yams/Encoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ extension _Encoder: SingleValueEncodingContainer {
assertCanEncodeNewValue()
try encode(yamlEncodable: value)
}

private func encode(yamlEncodable encodable: YAMLEncodable) throws {
func encodeNode() {
node = encodable.box()
Expand Down Expand Up @@ -270,7 +270,8 @@ extension _Encoder: SingleValueEncodingContainer {
if let encodable = value as? YAMLEncodable {
try encode(yamlEncodable: encodable)
} else {
if let redundancyAliasingStrategy = userInfo[.redundancyAliasingStrategyKey] as? RedundancyAliasingStrategy {
if let redundancyAliasingStrategy =
userInfo[.redundancyAliasingStrategyKey] as? RedundancyAliasingStrategy {
switch try redundancyAliasingStrategy.alias(for: value) {
case .none:
try value.encode(to: self)
Expand Down
4 changes: 1 addition & 3 deletions Sources/Yams/Node.Alias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// Copyright (c) 2024 Yams. All rights reserved.
//

import Foundation

// MARK: Node+Alias

extension Node {
Expand All @@ -19,7 +17,7 @@ extension Node {
public var tag: Tag
/// The location for this node.
public var mark: Mark?

/// Create a `Node.Alias` using the specified parameters.
///
/// - parameter tag: This scalar's `Tag`.
Expand Down
6 changes: 5 additions & 1 deletion Sources/Yams/Node.Mapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ extension Node {
/// - parameter tag: This mapping's `Tag`.
/// - parameter style: The style to use when emitting this `Mapping`.
/// - parameter mark: This mapping's `Mark`.
public init(_ pairs: [(Node, Node)], _ tag: Tag = .implicit, _ style: Style = .any, _ mark: Mark? = nil, _ anchor: Anchor? = nil) {
public init(_ pairs: [(Node, Node)],
_ tag: Tag = .implicit,
_ style: Style = .any,
_ mark: Mark? = nil,
_ anchor: Anchor? = nil) {
self.pairs = pairs.map { Pair($0.0, $0.1) }
self.tag = tag
self.style = style
Expand Down
6 changes: 5 additions & 1 deletion Sources/Yams/Node.Scalar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ extension Node {
/// - parameter tag: This scalar's `Tag`.
/// - parameter style: The style to use when emitting this `Scalar`.
/// - parameter mark: This scalar's `Mark`.
public init(_ string: String, _ tag: Tag = .implicit, _ style: Style = .any, _ mark: Mark? = nil, _ anchor: Anchor? = nil) {
public init(_ string: String,
_ tag: Tag = .implicit,
_ style: Style = .any,
_ mark: Mark? = nil,
_ anchor: Anchor? = nil) {
self.string = string
self.tag = tag
self.style = style
Expand Down
6 changes: 5 additions & 1 deletion Sources/Yams/Node.Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ extension Node {
/// - parameter tag: This sequence's `Tag`.
/// - parameter style: The style to use when emitting this `Sequence`.
/// - parameter mark: This sequence's `Mark`.
public init(_ nodes: [Node], _ tag: Tag = .implicit, _ style: Style = .any, _ mark: Mark? = nil, _ anchor: Anchor? = nil) {
public init(_ nodes: [Node],
_ tag: Tag = .implicit,
_ style: Style = .any,
_ mark: Mark? = nil,
_ anchor: Anchor? = nil) {
self.nodes = nodes
self.tag = tag
self.style = style
Expand Down
21 changes: 15 additions & 6 deletions Sources/Yams/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ extension Node {
/// - parameter string: String value for this node.
/// - parameter tag: Tag for this node.
/// - parameter style: Style to use when emitting this node.
public init(_ string: String, _ tag: Tag = .implicit, _ style: Scalar.Style = .any, _ anchor: Anchor? = nil) {
public init(_ string: String,
_ tag: Tag = .implicit,
_ style: Scalar.Style = .any,
_ anchor: Anchor? = nil) {
self = .scalar(.init(string, tag, style, nil, anchor))
}

Expand All @@ -35,7 +38,10 @@ extension Node {
/// - parameter pairs: Pairs of nodes to use for this node.
/// - parameter tag: Tag for this node.
/// - parameter style: Style to use when emitting this node.
public init(_ pairs: [(Node, Node)], _ tag: Tag = .implicit, _ style: Mapping.Style = .any, _ anchor: Anchor? = nil) {
public init(_ pairs: [(Node, Node)],
_ tag: Tag = .implicit,
_ style: Mapping.Style = .any,
_ anchor: Anchor? = nil) {
self = .mapping(.init(pairs, tag, style, nil, anchor))
}

Expand All @@ -44,7 +50,10 @@ extension Node {
/// - parameter nodes: Sequence of nodes to use for this node.
/// - parameter tag: Tag for this node.
/// - parameter style: Style to use when emitting this node.
public init(_ nodes: [Node], _ tag: Tag = .implicit, _ style: Sequence.Style = .any, _ anchor: Anchor? = nil) {
public init(_ nodes: [Node],
_ tag: Tag = .implicit,
_ style: Sequence.Style = .any,
_ anchor: Anchor? = nil) {
self = .sequence(.init(nodes, tag, style, nil, anchor))
}
}
Expand Down Expand Up @@ -73,7 +82,7 @@ extension Node {
case let .alias(alias): return alias.mark
}
}

/// The anchor for this node.
public var anchor: Anchor? {
switch self {
Expand Down Expand Up @@ -304,7 +313,7 @@ extension Node {
}
return false
}

func setting(anchor: Anchor) -> Self {
switch self {
case var .mapping(mapping):
Expand All @@ -321,7 +330,7 @@ extension Node {
return .alias(alias)
}
}

func setting(tag: Tag) -> Self {
switch self {
case var .mapping(mapping):
Expand Down
20 changes: 16 additions & 4 deletions Sources/Yams/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public final class Parser {
case utf16(Data)
}
private var buffer: Buffer

// MARK: – Pivate Mutators
private func register(anchor: Anchor?, to node: Node) {
if let anchor {
Expand Down Expand Up @@ -341,7 +341,11 @@ private extension Parser {

func loadScalar(from event: Event) throws -> Node {
let anchor = event.scalarAnchor
let node = Node.scalar(.init(event.scalarValue, tag(event.scalarTag), event.scalarStyle, event.startMark, anchor))
let node = Node.scalar(.init(event.scalarValue,
tag(event.scalarTag),
event.scalarStyle,
event.startMark,
anchor))
register(anchor: anchor, to: node)
return node
}
Expand All @@ -354,7 +358,11 @@ private extension Parser {
event = try parse()
}
let anchor = firstEvent.sequenceAnchor
let node = Node.sequence(.init(array, tag(firstEvent.sequenceTag), event.sequenceStyle, firstEvent.startMark, anchor))
let node = Node.sequence(.init(array,
tag(firstEvent.sequenceTag),
event.sequenceStyle,
firstEvent.startMark,
anchor))
register(anchor: anchor, to: node)
return node
}
Expand All @@ -370,7 +378,11 @@ private extension Parser {
event = try parse()
}
let anchor = firstEvent.mappingAnchor
let node = Node.mapping(.init(pairs, tag(firstEvent.mappingTag), event.mappingStyle, firstEvent.startMark, anchor))
let node = Node.mapping(.init(pairs,
tag(firstEvent.mappingTag),
event.mappingStyle,
firstEvent.startMark,
anchor))
register(anchor: anchor, to: node)
return node
}
Expand Down
Loading

0 comments on commit 27afe7f

Please sign in to comment.