Skip to content

Commit

Permalink
refactoring Role
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Sep 23, 2023
1 parent a0c23d0 commit aeda218
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 56 deletions.
80 changes: 80 additions & 0 deletions Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson+Role.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
58 changes: 2 additions & 56 deletions Sources/SyndiKit/Formats/Media/Podcast/PodcastPerson.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,8 @@ import Foundation
/// img="http://example.com/images/alicebrown.jpg"
/// >Alice Brown</podcast:person>
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?
Expand All @@ -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
}
Expand Down

0 comments on commit aeda218

Please sign in to comment.