1
1
mirror of https://github.com/github/semantic.git synced 2024-11-29 02:44:36 +03:00

Compute the before-state of diffs.

This is `Optional`, as some diffs have unrecoverable before states. For
example, the insertion of a term has no before state, but can still
produce a valid before state if it occurs within the context of an
`Indexed` or `Keyed` node.

However, certain malformed diffs (such as `.Pure(.Insert(…))` in
isolation) may lack a valid before state, and thus the public API has
to reflect this.
This commit is contained in:
Rob Rix 2015-10-06 21:35:19 -04:00
parent 75aaebe834
commit b04d71ede3

View File

@ -90,6 +90,19 @@ public enum Free<A, B>: CustomDebugStringConvertible {
extension Free where B: PatchConvertible, B.Info == A {
public typealias Term = Fix<A>
public var before: Term? {
return map { $0.patch.state.before }.iterate { syntax -> Term? in
switch syntax {
case let .Leaf(a):
return .In(.Leaf(a))
case let .Indexed(a):
return .In(.Indexed(a.flatMap(id)))
case let .Keyed(a):
return .In(.Keyed(Dictionary(elements: a.flatMap { k, v in v.map { (k, $0) } })))
}
}
}
}