mirror of
https://github.com/github/semantic.git
synced 2024-11-28 18:23:44 +03:00
67 lines
1.3 KiB
Swift
67 lines
1.3 KiB
Swift
//
|
|
// JSONLeaf.swift
|
|
// Doubt
|
|
//
|
|
// Created by Josh Vera on 10/16/15.
|
|
// Copyright © 2015 GitHub. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
typealias Term = Cofree<JSONLeaf, Range<String.Index>>
|
|
typealias Diff = Free<JSONLeaf, Term.Annotation, Patch<Term>>
|
|
|
|
public enum JSONLeaf: CustomJSONConvertible, CustomStringConvertible, Equatable {
|
|
case Number(Double)
|
|
case Boolean(Bool)
|
|
case String(Swift.String)
|
|
case Null
|
|
|
|
|
|
// MARK: CustomJSONConvertible
|
|
|
|
public var JSON: Doubt.JSON {
|
|
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
|
|
|
|
public var description: Swift.String {
|
|
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"
|
|
}
|
|
}
|
|
}
|
|
|
|
public func == (left: JSONLeaf, right: JSONLeaf) -> Bool {
|
|
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
|
|
}
|
|
}
|