frenzy-ios/Fervor/Sources/Fervor/Item.swift

64 lines
2.1 KiB
Swift

//
// Item.swift
// Fervor
//
// Created by Shadowfacts on 10/29/21.
//
import Foundation
public struct Item: Decodable, Sendable {
private static let urlParseStrategy = URL.ParseStrategy()
.scheme(.defaultValue("https"))
.user(.optional)
.password(.optional)
.host(.required)
.port(.defaultValue(8080))
.path(.optional)
.query(.optional)
.fragment(.optional)
public let id: FervorID
public let feedID: FervorID
public let title: String?
public let author: String?
public let published: Date?
public let createdAt: Date?
public let content: String?
public let summary: String?
public let url: URL
public let serviceURL: URL?
public let read: Bool?
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(FervorID.self, forKey: .id)
self.feedID = try container.decode(FervorID.self, forKey: .feedID)
self.title = try container.decodeIfPresent(String.self, forKey: .title)
self.author = try container.decodeIfPresent(String.self, forKey: .author)
self.published = try container.decodeIfPresent(Date.self, forKey: .published)
self.createdAt = try container.decodeIfPresent(Date.self, forKey: .createdAt)
self.content = try container.decodeIfPresent(String.self, forKey: .content)
self.summary = try container.decodeIfPresent(String.self, forKey: .summary)
let urlStr = try container.decode(String.self, forKey: .url)
self.url = try Self.urlParseStrategy.parse(urlStr)
self.serviceURL = try container.decodeIfPresent(URL.self, forKey: .serviceURL)
self.read = try container.decodeIfPresent(Bool.self, forKey: .read)
}
private enum CodingKeys: String, CodingKey {
case id
case feedID = "feed_id"
case title
case author
case published
case createdAt = "created_at"
case content
case summary
case url
case serviceURL = "service_url"
case read
}
}