Skip to content

Commit

Permalink
Added DecodeResult#option
Browse files Browse the repository at this point in the history
  • Loading branch information
tonymorris committed Jul 18, 2012
1 parent 663aca7 commit 5213a56
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
39 changes: 15 additions & 24 deletions src/main/scala/com/ephox/argonaut/CursorHistory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,25 @@ import scalaz._, Scalaz._
* @author Tony Morris
*/
sealed trait CursorHistory {
/**
* Convert to a list.
*/
val ops: DList[CursorOp]

/**
* Convert cursor history operations to a list (O(n)).
*/
def toList: List[CursorOp] =
ops.toList
val toList: List[CursorOp]

def head: Option[CursorOp] =
toList.headOption

/**
* Append two lists of cursor history.
*/
def ++(h: CursorHistory): CursorHistory =
CursorHistory.build(ops ++ h.ops)
CursorHistory.build(toList ++ h.toList)

/**
* Prepend a cursor operation to the history.
*/
def +:(o: CursorOp): CursorHistory =
CursorHistory.build(o +: ops)

/**
* Append a cursor operation to the history.
*/
def :+(o: CursorOp): CursorHistory =
CursorHistory.build(ops :+ o)
CursorHistory.build(o +: toList)

def failedACursor(c: Cursor): ACursor =
ACursor.failedACursor(HCursor(c, this))
Expand All @@ -48,31 +39,31 @@ sealed trait CursorHistory {
def acursorElement(c: Store[Cursor, Option[Cursor]], e: CursorOpElement): ACursor = {
val x = c.pos
c.copoint match {
case None => :+(CursorOp(e)).failedACursor(x)
case Some(q) => :+(CursorOp.failedOp(e)).acursor(q)
case None => +:(CursorOp(e)).failedACursor(x)
case Some(q) => +:(CursorOp.failedOp(e)).acursor(q)
}
}

}

object CursorHistory extends CursorHistorys {
private[argonaut] def apply(e: CursorOp) =
build(DList(e))
build(List(e))
}

trait CursorHistorys {
private[argonaut] def build(l: DList[CursorOp]): CursorHistory =
private[argonaut] def build(l: List[CursorOp]): CursorHistory =
new CursorHistory {
val ops = l
val toList = l
}

implicit val CursorHistoryInstances: Show[CursorHistory] with Equal[CursorHistory] with Monoid[CursorHistory] =
new Show[CursorHistory] with Equal[CursorHistory] with Monoid[CursorHistory] {
def show(h: CursorHistory) = Show[List[CursorOp]].show(h.ops.toList)
def show(h: CursorHistory) = Show[List[CursorOp]].show(h.toList)
def equal(h1: CursorHistory, h2: CursorHistory) =
h1.ops === h2.ops
def zero = CursorHistory.build(DList())
h1.toList === h2.toList
def zero = CursorHistory.build(List())
def append(h1: CursorHistory, h2: => CursorHistory) =
h1 ++ h2
CursorHistory.build(h1.toList ::: h2.toList)
}
}
2 changes: 1 addition & 1 deletion src/main/scala/com/ephox/argonaut/CursorOp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package argonaut
import scalaz._, Scalaz._

sealed trait CursorOp {
def isRettempt: Boolean =
def isReattempt: Boolean =
this == Reattempt

def isNotReattempt: Boolean =
Expand Down
10 changes: 10 additions & 0 deletions src/main/scala/com/ephox/argonaut/DecodeResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ sealed trait DecodeResult[+A] {
def value: Option[A] =
result.right.toOption

def option: DecodeResult[Option[A]] =
result match {
case Left((s, h)) => h.head filter (_.succeeded) match {
case None => DecodeResult(None)
case Some(o) => DecodeResult.failedResult(s, h)
}
case Right(a) => DecodeResult(Some(a))
}


def |||[AA >: A](r: => DecodeResult[AA]): DecodeResult[AA] =
DecodeResult.build(result match {
case Left(_) => r.result
Expand Down

0 comments on commit 5213a56

Please sign in to comment.