1
1
mirror of https://github.com/github/semantic.git synced 2024-11-29 11:02:26 +03:00
semantic/prototype/doubt-json/main.swift

50 lines
1.6 KiB
Swift
Raw Normal View History

import Cocoa
2015-10-14 21:17:56 +03:00
import Doubt
2015-10-16 00:40:57 +03:00
import Either
2015-10-14 21:17:56 +03:00
import Prelude
2015-10-16 00:40:57 +03:00
import Madness
2015-10-08 02:20:40 +03:00
let arguments = BoundsCheckedArray(array: Process.arguments)
2015-10-16 00:40:57 +03:00
2015-10-20 18:12:30 +03:00
let empty = "{}\n"
2015-10-16 21:43:23 +03:00
print(parse(json, input: empty))
2015-10-16 20:08:49 +03:00
let dict = "{\"hello\":\"world\"}"
print(parse(json, input: dict))
let dictWithSpaces = "{ \"hello\" : \"world\" }"
print(parse(json, input: dictWithSpaces))
2015-10-16 21:43:23 +03:00
let dictWithMembers = "{\"hello\":\"world\",\"sup\":\"cat\"}"
print(parse(json, input: dictWithMembers))
2015-10-16 21:52:03 +03:00
let dictWithArray = "{\"hello\": [\"world\"],\"sup\": [\"cat\", \"dog\", \"keith\"] }"
print(parse(json, input: dictWithArray))
2015-10-19 22:15:38 +03:00
func diffAndSerialize(a aString: String, b bString: String) -> String? {
2015-10-20 19:09:03 +03:00
let aParsed = curry(parse)(json)(aString)
guard let a = aParsed.right else {
2015-10-20 19:09:38 +03:00
_ = aParsed.left.map { print("error parsing a: ", $0) }
2015-10-20 19:09:03 +03:00
return nil
}
let bParsed = curry(parse)(json)(bString)
guard let b = bParsed.right else {
2015-10-20 19:09:38 +03:00
_ = bParsed.left.map { print("error parsing b: ", $0) }
2015-10-20 19:09:03 +03:00
return nil
}
2015-10-19 22:15:38 +03:00
let diff = Interpreter<CofreeJSON>(equal: CofreeJSON.equals(annotation: const(true), leaf: ==), comparable: const(true), cost: Free.sum(Patch.difference)).run(a, b)
guard let JSON = NSString(data: diff.JSON(ifPure: { $0.JSON(a: aString, b: bString) }, ifLeaf: { $0.JSON }).serialize(), encoding: NSUTF8StringEncoding) else { return nil }
return JSON as String
}
2015-10-20 18:12:30 +03:00
let readFile = { (path: String) -> String? in
guard let data = try? NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) else { return nil }
return data as String?
}
if let a = arguments[1].flatMap(readFile), b = arguments[2].flatMap(readFile), diff = diffAndSerialize(a: a, b: b) {
2015-10-20 18:12:30 +03:00
print(diff)
2015-10-16 22:41:35 +03:00
}