|
| 1 | +// |
| 2 | +// ListableItem.swift |
| 3 | +// Created by Nikita Mikheev on 27.02.2022. |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import UIKit |
| 8 | + |
| 9 | +public struct ListItem { |
| 10 | + public let title: String |
| 11 | + public let value: ListValueRepresentable |
| 12 | + |
| 13 | + public init( |
| 14 | + title: String, |
| 15 | + value: ListValueRepresentable |
| 16 | + ) { |
| 17 | + self.title = title |
| 18 | + self.value = value |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +public protocol ListValueRepresentable { |
| 23 | + var valueDescription: String { get } |
| 24 | + var sublist: [ListItem]? { get } |
| 25 | +} |
| 26 | +public extension ListValueRepresentable where Self: CustomStringConvertible { |
| 27 | + var valueDescription: String { |
| 28 | + description |
| 29 | + } |
| 30 | +} |
| 31 | +public extension ListValueRepresentable { |
| 32 | + var sublist: [ListItem]? { nil } |
| 33 | +} |
| 34 | + |
| 35 | +// Optional |
| 36 | +extension Optional: ListValueRepresentable where Wrapped: ListValueRepresentable { |
| 37 | + public var valueDescription: String { |
| 38 | + switch self { |
| 39 | + case .none: |
| 40 | + return "nil" |
| 41 | + case .some(let wrapped): |
| 42 | + return wrapped.valueDescription |
| 43 | + } |
| 44 | + } |
| 45 | + public var sublist: [ListItem]? { |
| 46 | + switch self { |
| 47 | + case .none: |
| 48 | + return nil |
| 49 | + case .some(let wrapped): |
| 50 | + return wrapped.sublist |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Boolean |
| 56 | +extension Bool: ListValueRepresentable { } |
| 57 | + |
| 58 | +// Numeric |
| 59 | +extension Int: ListValueRepresentable { } |
| 60 | +extension Double: ListValueRepresentable { } |
| 61 | +extension Float: ListValueRepresentable { } |
| 62 | +extension CGFloat: ListValueRepresentable { } |
| 63 | +extension NSNumber: ListValueRepresentable { } |
| 64 | + |
| 65 | +// String |
| 66 | +extension String: ListValueRepresentable { |
| 67 | + public var valueDescription: String { |
| 68 | + "\"" + description + "\"" |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// Collections |
| 73 | +extension Array: ListValueRepresentable where Element: ListValueRepresentable { |
| 74 | + public var valueDescription: String { |
| 75 | + isEmpty ? "[∅]" : "[...]" |
| 76 | + } |
| 77 | + public var sublist: [ListItem]? { |
| 78 | + guard !isEmpty else { |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + var items = [ListItem]() |
| 83 | + items.reserveCapacity(count) |
| 84 | + |
| 85 | + for index in 0..<count { |
| 86 | + items.append( |
| 87 | + ListItem( |
| 88 | + title: index.description, |
| 89 | + value: self[index] |
| 90 | + ) |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + return items |
| 95 | + } |
| 96 | +} |
| 97 | +extension Set: ListValueRepresentable { |
| 98 | + public var valueDescription: String { |
| 99 | + isEmpty ? "[∅]" : "set [...]" |
| 100 | + } |
| 101 | + public var sublist: [ListItem]? { |
| 102 | + guard !isEmpty else { |
| 103 | + return nil |
| 104 | + } |
| 105 | + |
| 106 | + return map { |
| 107 | + ListItem( |
| 108 | + title: "", |
| 109 | + value: $0 as? ListValueRepresentable ?? "⌧" |
| 110 | + ) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +// Dictionary |
| 117 | +extension Dictionary: ListValueRepresentable { |
| 118 | + public var valueDescription: String { |
| 119 | + isEmpty ? "[∅:∅]" : "[ : ]" |
| 120 | + } |
| 121 | + public var sublist: [ListItem]? { |
| 122 | + guard !isEmpty else { |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + return keys.map { key in |
| 127 | + ListItem( |
| 128 | + title: (key as? CustomStringConvertible)?.description ?? "⌧", |
| 129 | + value: self[key] as? ListValueRepresentable ?? "⌧" |
| 130 | + ) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | +extension Dictionary where Key: Comparable { |
| 135 | + public var sublist: [ListItem]? { |
| 136 | + guard !isEmpty else { |
| 137 | + return nil |
| 138 | + } |
| 139 | + |
| 140 | + return keys.sorted(by: <).map { key in |
| 141 | + ListItem( |
| 142 | + title: (key as? CustomStringConvertible)?.description ?? "⌧", |
| 143 | + value: self[key] as? ListValueRepresentable ?? "⌧" |
| 144 | + ) |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// Data |
| 150 | +extension Data: ListValueRepresentable { |
| 151 | + public var valueDescription: String { |
| 152 | + "<Data>" |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// UUID |
| 157 | +extension UUID: ListValueRepresentable { |
| 158 | + public var valueDescription: String { |
| 159 | + uuidString |
| 160 | + } |
| 161 | +} |
0 commit comments