Skip to content

Commit

Permalink
Make RoleHeading RawRepresentable
Browse files Browse the repository at this point in the history
Makes sense and maintains the original API.
  • Loading branch information
helje5 committed Jun 6, 2024
1 parent 8133ab3 commit e717c40
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Sources/DocCArchive/Schema_0_1/MetaData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ extension DocCArchive.DocCSchema_0_1 {
public var description: String { return rawValue }
}

public enum RoleHeading: Codable, CustomStringConvertible, Equatable {
public enum RoleHeading: Codable, CustomStringConvertible, Equatable,
RawRepresentable
{
// or just a plain string?
public enum Known: String, Codable {
case structure = "Structure"
Expand All @@ -93,13 +95,17 @@ extension DocCArchive.DocCSchema_0_1 {

case known(Known)
case custom(String)

public init(rawValue: String) {
if let known = Known(rawValue: rawValue) { self = .known(known) }
else { self = .custom(rawValue) }
}

var rawValue: String {
@inlinable
public var rawValue: String {
switch self {
case let .known(value):
return value.rawValue
case let .custom(value):
return value
case let .known (value) : return value.rawValue
case let .custom(value) : return value
}
}

Expand All @@ -108,11 +114,7 @@ extension DocCArchive.DocCSchema_0_1 {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let key = try container.decode(String.self)
if let value = Known(rawValue: key) {
self = .known(value)
} else {
self = .custom(key)
}
self = .init(rawValue: key)
}

public func encode(to encoder: Encoder) throws {
Expand Down

0 comments on commit e717c40

Please sign in to comment.