Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jun 15, 2024
1 parent 3d70d59 commit 36dad48
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Sources/CustomDump/Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
)

default:
if let lhs = stringFromStringProtocol(lhs),
let rhs = stringFromStringProtocol(rhs),
if let lhs = String(stringProtocol: lhs),
let rhs = String(stringProtocol: rhs),
lhs.contains(where: \.isNewline) || rhs.contains(where: \.isNewline)
{
let lhsMirror = Mirror(
Expand Down
2 changes: 1 addition & 1 deletion Sources/CustomDump/Dump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func _customDump<T, TargetStream>(
)

default:
if let value = stringFromStringProtocol(value) {
if let value = String(stringProtocol: value) {
if value.contains(where: \.isNewline) {
if maxDepth <= 0 {
out.write("\"\"")
Expand Down
40 changes: 0 additions & 40 deletions Sources/CustomDump/Internal/Box.swift

This file was deleted.

8 changes: 8 additions & 0 deletions Sources/CustomDump/Internal/Identifiable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func isIdentityEqual(_ lhs: Any, _ rhs: Any) -> Bool {
guard let lhs = lhs as? any Identifiable else { return false }
func open<LHS: Identifiable>(_ lhs: LHS) -> Bool {
guard let rhs = rhs as? LHS else { return false }
return lhs.id == rhs.id
}
return open(lhs)
}
27 changes: 27 additions & 0 deletions Sources/CustomDump/Internal/Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,30 @@ extension Mirror {
}
}
}

func isMirrorEqual(_ lhs: Any, _ rhs: Any) -> Bool {
guard let lhs = lhs as? any Equatable else {
let lhsMirror = Mirror(customDumpReflecting: lhs)
let rhsMirror = Mirror(customDumpReflecting: rhs)
guard
lhsMirror.subjectType == rhsMirror.subjectType,
lhsMirror.children.count == rhsMirror.children.count
else { return false }
guard !lhsMirror.children.isEmpty, !rhsMirror.children.isEmpty
else {
return String(describing: lhs) == String(describing: rhs)
}
for (lhsChild, rhsChild) in zip(lhsMirror.children, rhsMirror.children) {
guard
lhsChild.label == rhsChild.label,
isMirrorEqual(lhsChild.value, rhsChild.value)
else { return false }
}
return true
}
func open<T: Equatable>(_ lhs: T) -> Bool {
guard let rhs = rhs as? T else { return false }
return lhs == rhs
}
return open(lhs)
}
5 changes: 5 additions & 0 deletions Sources/CustomDump/Internal/String.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Foundation

extension String {
init?(stringProtocol value: Any) {
guard let value = value as? any StringProtocol else { return nil }
self.init(value)
}

func indenting(by count: Int) -> String {
self.indenting(with: String(repeating: " ", count: count))
}
Expand Down

0 comments on commit 36dad48

Please sign in to comment.