import Foundation extension Section { static let defaultSectionOverviewItemCount = 6 struct Metadata { let thumbnailStyle: ThumbnailStyle let sortByMostRecent: Bool let sortIndex: Int? let date: Date? let languages: [LocalizedMetadata] let sectionOverviewItemCount: Int } } extension Section.Metadata: Metadata { static let fileName = "section.json" static var initial: Section.Metadata { .init(thumbnailStyle: .large, sortByMostRecent: true, sortIndex: nil, date: nil, languages: [.initial], sectionOverviewItemCount: 6) } } extension Section.Metadata: LanguageContainer { } extension Section.Metadata: Codable { enum CodingKeys: CodingKey { case thumbnailStyle case sortByMostRecent case sortIndex case date case languages case sectionOverviewItemCount } func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(thumbnailStyle, forKey: .thumbnailStyle) try container.encode(sortByMostRecent, forKey: .sortByMostRecent) try container.encodeIfPresent(sortIndex, forKey: .sortIndex) try container.encode(languages, forKey: .languages) if let date = date { let dateString = Page.metadataDateFormatter.string(from: date) try container.encode(dateString, forKey: .date) } } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) self.thumbnailStyle = try container.decode(ThumbnailStyle.self, forKey: .thumbnailStyle) self.sortByMostRecent = try container.decode(Bool.self, forKey: .sortByMostRecent) self.sortIndex = try container.decodeIfPresent(Int.self, forKey: .sortIndex) self.languages = try container.decode([Section.LocalizedMetadata].self, forKey: .languages) if let dateString = try container.decodeIfPresent(String.self, forKey: .date) { self.date = try Page.metadataDateFormatter.date(from: dateString) .unwrap(or: .invalidDateInPageMetadata(dateString)) } else { self.date = nil } self.sectionOverviewItemCount = try container .decodeIfPresent(Int.self, forKey: .sectionOverviewItemCount) ?? Section.defaultSectionOverviewItemCount } }