Created
May 23, 2017 03:42
-
-
Save Qata/846a394eff543ca1458713148ac82438 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum Zone { | |
indirect case node(component: String, leaves: [Zone]) | |
case leaf(component: String, path: String) | |
} | |
func recurseCreateZones(path: [String], separated: [String], root: Zone) -> Zone { | |
guard case .node(let component, let leaves) = root else { fatalError() } | |
switch (separated.first, separated.count) { | |
case (let value?, 1): | |
return .node(component: component, leaves: leaves + [.leaf(component: value, path: path.joined(separator: "/"))]) | |
case (let value?, _): | |
let zoneIndex = leaves.index { | |
switch $0 { | |
case .node(component: value, leaves: _): | |
return true | |
default: | |
return false | |
} | |
} | |
if let index = zoneIndex, case .node(component: let c, leaves: let l) = leaves[index] { | |
var leaves = leaves | |
leaves[index] = recurseCreateZones(path: path, separated: Array(separated.dropFirst()), root: .node(component: c, leaves: l)) | |
return .node(component: component, leaves: leaves) | |
} else { | |
return .node(component: component, leaves: leaves + [recurseCreateZones(path: path, separated: Array(separated.dropFirst()), root: .node(component: value, leaves: []))]) | |
} | |
default: | |
fatalError() | |
} | |
} | |
func createZones(values: [[String]]) -> Zone { | |
var zone = Zone.node(component: "KnownZones", leaves: []) | |
values.forEach { path in | |
zone = recurseCreateZones(path: path, separated: path, root: zone) | |
} | |
return zone | |
} | |
func print(_ string: String, precedingTabs depth: Int) { | |
(0..<depth).map { _ in "\t" }.forEach { print($0, separator: "", terminator: "") } | |
print(string) | |
} | |
func recurseZones(zone: Zone, depth: Int) { | |
switch zone { | |
case .node(component: let component, leaves: let leaves): | |
let contains = leaves.contains { | |
switch $0 { | |
case .leaf: | |
return true | |
default: | |
return false | |
} | |
} | |
if contains { | |
print("enum \(component.replacingOccurrences(of: "-", with: "_")): String {", precedingTabs: depth) | |
} else { | |
print("enum \(component.replacingOccurrences(of: "-", with: "_")) {", precedingTabs: depth) | |
} | |
leaves.sorted { | |
switch $0 { | |
case (.leaf, .node): | |
return true | |
case (.leaf(component: let c1, path: _), .leaf(component: let c2, path: _)): | |
return c1 < c2 | |
default: | |
return false | |
} | |
}.forEach { recurseZones(zone: $0, depth: depth + 1) } | |
print("}", precedingTabs: depth) | |
case .leaf(component: let component, path: let path): | |
print("case \(component.lowercased().replacingOccurrences(of: "-", with: "_")) = \"\(path)\"", precedingTabs: depth) | |
} | |
} | |
print("extension TimeZone {") | |
recurseZones(zone: createZones(values: TimeZone.knownTimeZoneIdentifiers.map { $0.components(separatedBy: "/") }), depth: 1) | |
print("}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment