1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 18:23:44 +03:00
semantic/prototype/Doubt/JSONLeaf.swift

67 lines
1.3 KiB
Swift
Raw Normal View History

2015-10-16 22:41:35 +03:00
//
// JSONLeaf.swift
// Doubt
//
// Created by Josh Vera on 10/16/15.
// Copyright © 2015 GitHub. All rights reserved.
//
import Foundation
2015-10-20 18:11:17 +03:00
typealias Term = Cofree<JSONLeaf, Range<String.Index>>
2015-10-22 17:30:38 +03:00
typealias Diff = Free<JSONLeaf, Term.Annotation, Patch<Term>>
2015-10-16 22:41:35 +03:00
2015-10-26 22:06:33 +03:00
public enum JSONLeaf: CustomJSONConvertible, CustomStringConvertible, Equatable {
2015-10-16 22:41:35 +03:00
case Number(Double)
case Boolean(Bool)
case String(Swift.String)
case Null
// 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
}
}