1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 09:15:01 +03:00
semantic/prototype/Doubt/JSONLeaf.swift

70 lines
1.2 KiB
Swift
Raw Normal View History

2015-10-30 21:54:18 +03:00
public enum JSONLeaf: Categorizable, CustomJSONConvertible, CustomStringConvertible, Equatable {
2015-10-16 22:41:35 +03:00
case Number(Double)
case Boolean(Bool)
case String(Swift.String)
case Null
2015-10-30 21:54:18 +03:00
// MARK: Categorizable
public var categories: Set<Swift.String> {
switch self {
case .Number:
return [ "number" ]
case .Boolean:
return [ "boolean" ]
case .String:
return [ "string" ]
case .Null:
return [ "null" ]
}
}
2015-10-16 22:41:35 +03:00
// MARK: CustomJSONConvertible
2015-10-26 22:06:33 +03:00
public var JSON: Doubt.JSON {
2015-10-16 22:41:35 +03:00
switch self {
case let .Number(n):
return .Number(n)
case let .Boolean(b):
return .Boolean(b)
case let .String(s):
return .String(s)
case .Null:
return .Null
}
}
// MARK: CustomStringConvertible
2015-10-26 22:06:33 +03:00
public var description: Swift.String {
2015-10-16 22:41:35 +03:00
switch self {
case let .Number(n):
return Swift.String(n)
case let .Boolean(b):
return Swift.String(b)
case let .String(s):
return Swift.String(reflecting: s)
case .Null:
return "null"
}
}
}
2015-10-26 22:06:33 +03:00
public func == (left: JSONLeaf, right: JSONLeaf) -> Bool {
2015-10-16 22:41:35 +03:00
switch (left, right) {
case let (.Number(a), .Number(b)):
return a == b
case let (.Boolean(a), .Boolean(b)):
return a == b
case let (.String(a), .String(b)):
return a == b
case (.Null, .Null):
return true
default:
return false
}
}