Emcee
64 строки · 1.9 Кб
1import Foundation
2
3public enum AutomaticTerminationPolicy: Codable, CustomStringConvertible, Equatable {
4/// Will trigger termination after being idle for the given amout of time.
5case afterBeingIdle(period: TimeInterval)
6
7/// Will not trigger automatic termination.
8case stayAlive
9
10public var period: TimeInterval {
11switch self {
12case .afterBeingIdle(let period):
13return period
14case .stayAlive:
15return .infinity
16}
17}
18
19public var description: String {
20var items = ["\(type(of: self))"]
21
22switch self {
23case .afterBeingIdle(let period):
24items += ["after being idle for \(period) sec"]
25case .stayAlive:
26items += ["staying alive infinitely"]
27}
28
29return "<" + items.joined(separator: ", ") + ">"
30}
31
32private enum CodingKeys: String, CodingKey {
33case caseId
34case period
35}
36
37private enum CaseId: String, Codable {
38case afterBeingIdle
39case stayAlive
40}
41
42public init(from decoder: Decoder) throws {
43let container = try decoder.container(keyedBy: CodingKeys.self)
44let caseId = try container.decode(CaseId.self, forKey: .caseId)
45
46switch caseId {
47case .afterBeingIdle:
48self = .afterBeingIdle(period: try container.decode(TimeInterval.self, forKey: .period))
49case .stayAlive:
50self = .stayAlive
51}
52}
53
54public func encode(to encoder: Encoder) throws {
55var container = encoder.container(keyedBy: CodingKeys.self)
56switch self {
57case .afterBeingIdle(let period):
58try container.encode(CaseId.afterBeingIdle, forKey: .caseId)
59try container.encode(period, forKey: .period)
60case .stayAlive:
61try container.encode(CaseId.stayAlive, forKey: .caseId)
62}
63}
64}
65