Skip to content

Commit

Permalink
Refactored DecodingError.dataCorrupted calls into a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitribouniol committed Jun 4, 2021
1 parent 5eba8e1 commit ed1aebf
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions Sources/DynamicCodable/DynamicCodableDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,7 @@ extension DynamicCodableDecoder.Decoder: Swift.Decoder {
@inline(__always)
func validate<T: BinaryFloatingPoint>(_ floatingPoint: T, originalValue: CustomStringConvertible) throws -> T {
guard floatingPoint.isFinite else {
throw DecodingError.dataCorrupted(
.init(
codingPath: codingPath,
debugDescription: "Represented number <\(floatingPoint)> does not fit in \(T.self)."
)
)
throw dataCorruptedError("Represented number <\(floatingPoint)> does not fit in \(T.self).")
}

return floatingPoint
Expand Down Expand Up @@ -260,12 +255,7 @@ extension DynamicCodableDecoder.Decoder: Swift.Decoder {
@inline(__always)
func validate<T: FixedWidthInteger>(_ fixedWidthInteger: T?, originalValue: CustomStringConvertible) throws -> T {
guard let fixedWidthInteger = fixedWidthInteger else {
throw DecodingError.dataCorrupted(
.init(
codingPath: codingPath,
debugDescription: "Represented number <\(originalValue)> does not fit in \(T.self)."
)
)
throw dataCorruptedError("Represented number <\(originalValue)> does not fit in \(T.self).")
}

return fixedWidthInteger
Expand Down Expand Up @@ -303,6 +293,15 @@ extension DynamicCodableDecoder.Decoder: Swift.Decoder {
)
)
}

private func dataCorruptedError(_ debugDescription: String) -> DecodingError {
DecodingError.dataCorrupted(
.init(
codingPath: codingPath,
debugDescription: debugDescription
)
)
}
}

extension DynamicCodableDecoder.Decoder {
Expand Down

0 comments on commit ed1aebf

Please sign in to comment.