refactor: change methods ordering according to protection level

This commit is contained in:
Ivan Smolin 2022-03-09 12:27:51 +03:00
parent aacd025382
commit 373a61835b
1 changed files with 50 additions and 51 deletions

View File

@ -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)
}
}