From 373a61835bccc5269e828d41b3634ab20c456a00 Mon Sep 17 00:00:00 2001 From: Ivan Smolin Date: Wed, 9 Mar 2022 12:27:51 +0300 Subject: [PATCH] refactor: change methods ordering according to protection level --- .../Sources/Codable+DateCoding.swift | 101 +++++++++--------- 1 file changed, 50 insertions(+), 51 deletions(-) diff --git a/TIFoundationUtils/DateFormatting/Sources/Codable+DateCoding.swift b/TIFoundationUtils/DateFormatting/Sources/Codable+DateCoding.swift index c4997805..76c575ce 100644 --- a/TIFoundationUtils/DateFormatting/Sources/Codable+DateCoding.swift +++ b/TIFoundationUtils/DateFormatting/Sources/Codable+DateCoding.swift @@ -23,27 +23,6 @@ import Foundation public extension KeyedDecodingContainer { - private func date(from string: String, - forKey key: Key, - using dateFormatter: DateFormatter) throws -> Date { - - guard let date = dateFormatter.date(from: string) else { - let failureReason: String - - if let dateFormat = dateFormatter.dateFormat, !dateFormat.isEmpty { - failureReason = "Unable to decode date from \(string) using dateFormat \(dateFormat)" - } else { - failureReason = "DateFormatter is not configured (dateFormat is nil or empty)" - } - - throw DecodingError.dataCorruptedError(forKey: key, - in: self, - debugDescription: failureReason) - } - - return date - } - func decodeDate(forKey key: Key, using dateFormatter: DateFormatter) throws -> Date { @@ -65,21 +44,6 @@ public extension KeyedDecodingContainer { using: dateFormatter) } - private func date(from string: String, - forKey key: Key, - using dateFormatter: ISO8601DateFormatter) throws -> Date { - - guard let date = dateFormatter.date(from: string) else { - let failureReason = "Unable to decode date from \(string) using ISO8601 options: \(dateFormatter.formatOptions)" - - throw DecodingError.dataCorruptedError(forKey: key, - in: self, - debugDescription: failureReason) - } - - return date - } - func decodeDate(forKey key: Key, using dateFormatter: ISO8601DateFormatter) throws -> Date { @@ -101,24 +65,44 @@ public extension KeyedDecodingContainer { using: dateFormatter) } + private func date(from string: String, + forKey key: Key, + using dateFormatter: DateFormatter) throws -> Date { + + guard let date = dateFormatter.date(from: string) else { + let failureReason: String + + if let dateFormat = dateFormatter.dateFormat, !dateFormat.isEmpty { + failureReason = "Unable to decode date from \(string) using dateFormat \(dateFormat)" + } else { + failureReason = "DateFormatter is not configured (dateFormat is nil or empty)" + } + + throw DecodingError.dataCorruptedError(forKey: key, + in: self, + debugDescription: failureReason) + } + + return date + } + + private func date(from string: String, + forKey key: Key, + using dateFormatter: ISO8601DateFormatter) throws -> Date { + + guard let date = dateFormatter.date(from: string) else { + let failureReason = "Unable to decode date from \(string) using ISO8601 options: \(dateFormatter.formatOptions)" + + throw DecodingError.dataCorruptedError(forKey: key, + in: self, + debugDescription: failureReason) + } + + return date + } } public extension KeyedEncodingContainer { - private func string(from date: Date, - forKey key: Key, - using dateFormatter: DateFormatter) throws -> String { - - guard let dateFormat = dateFormatter.dateFormat, !dateFormat.isEmpty else { - let context = EncodingError.Context(codingPath: codingPath, - debugDescription: "DateFormatter is not configured (dateFormat is nil or empty)", - underlyingError: nil) - - throw EncodingError.invalidValue(date, context) - } - - return dateFormatter.string(from: date) - } - mutating func encode(date: Date, forKey key: Key, using dateFormatter: DateFormatter) throws { @@ -162,4 +146,19 @@ public extension KeyedEncodingContainer { try encodeNil(forKey: key) } } + + private func string(from date: Date, + forKey key: Key, + using dateFormatter: DateFormatter) throws -> String { + + guard let dateFormat = dateFormatter.dateFormat, !dateFormat.isEmpty else { + let context = EncodingError.Context(codingPath: codingPath, + debugDescription: "DateFormatter is not configured (dateFormat is nil or empty)", + underlyingError: nil) + + throw EncodingError.invalidValue(date, context) + } + + return dateFormatter.string(from: date) + } }