From aeda2183b70c1637f4ab1e85437b8d71a52a2f73 Mon Sep 17 00:00:00 2001 From: Leo Dion Date: Sat, 23 Sep 2023 12:31:42 -0400 Subject: [PATCH] refactoring Role --- .../Media/Podcast/PodcastPerson+Role.swift | 80 +++++++++++++++++++ .../Formats/Media/Podcast/PodcastPerson.swift | 58 +------------- 2 files changed, 82 insertions(+), 56 deletions(-) create mode 100644 Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson+Role.swift diff --git a/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson+Role.swift b/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson+Role.swift new file mode 100644 index 0000000..8a26db4 --- /dev/null +++ b/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson+Role.swift @@ -0,0 +1,80 @@ +// +// File.swift +// +// +// Created by Leo Dion on 9/23/23. +// + +import Foundation + +extension PodcastPerson { + private enum KnownRole : String { + case guest + case host + case editor + case writer + case designer + case composer + case producer + + init?(caseInsensitive: String) { + self.init(rawValue: caseInsensitive.lowercased()) + } + + init?(role: Role) { + switch role { + + case .guest: self = .guest + case .host: self = .host + case .editor: self = .editor + case .writer: self = .writer + case .designer: self = .designer + case .composer: self = .composer + case .producer: self = .producer + case .unknown: return nil + } + } + } + + public enum Role: Codable, Equatable, RawRepresentable { + public var rawValue: String { + if let knownRole = KnownRole(role: self) { + return knownRole.rawValue + } else if case let .unknown(string) = self { + return string + } else { + fatalError() + } + } + + public init(caseInsensitive: String) { + if let knownRole = KnownRole(caseInsensitive: caseInsensitive) { + self = .init(knownRole: knownRole) + } else { + self = .unknown(caseInsensitive) + } + } + + private init(knownRole : KnownRole) { + self.init(rawValue: knownRole.rawValue)! + } + + public init?(rawValue: String) { + if let knownRole = KnownRole(rawValue: rawValue) { + self = .init(knownRole: knownRole) + } else { + self = .unknown(rawValue) + } + } + + + case guest + case host + case editor + case writer + case designer + case composer + case producer + case unknown(String) + } +} diff --git a/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift b/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift index fd2e6a3..28a1b3e 100644 --- a/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift +++ b/Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift @@ -7,62 +7,8 @@ import Foundation /// img="http://example.com/images/alicebrown.jpg" /// >Alice Brown public struct PodcastPerson: Codable { - public enum PersonRole: Codable, Equatable { - case guest - case host - case editor - case writer - case designer - case composer - case producer - case unknown(String) - - public var rawValue: String { - switch self { - case .guest: return "guest" - case .host: return "host" - case .editor: return "editor" - case .writer: return "writer" - case .designer: return "designer" - case .composer: return "composer" - case .producer: return "producer" - case .unknown(let string): return "\(string)" - } - } - - public init(value: String) { - guard let role = PersonRole(rawValue: value.lowercased()) else { - self = Self.unknown(value) - return - } - - self = role - } - - public init?(rawValue: String) { - switch rawValue { - case Self.guest.rawValue: - self = .guest - case Self.host.rawValue: - self = .host - case Self.editor.rawValue: - self = .editor - case Self.writer.rawValue: - self = .writer - case Self.designer.rawValue: - self = .designer - case Self.composer.rawValue: - self = .composer - case Self.producer.rawValue: - self = .producer - default: - return nil - } - } - } - - public let role: PersonRole? + public let role: Role? public let group: String? public let href: URL? public let img: URL? @@ -83,7 +29,7 @@ public struct PodcastPerson: Codable { self.fullname = try container.decode(String.self, forKey: .fullname) if let roleStr = try container.decodeIfPresent(String.self, forKey: .role) { - self.role = PersonRole(value: roleStr) + self.role = Role(caseInsensitive: roleStr) } else { self.role = nil }