Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.codahale.jerkson.util.scalax.rules.Result.scala Maven / Gradle / Ivy
package com.codahale.jerkson.util
package scalax
package rules
/**Represents the combined value of two rules applied in sequence.
*
* @see the Scala parser combinator
*/
case class ~[+A, +B](_1: A, _2: B) {
override def toString = "(" + _1 + " ~ " + _2 + ")"
}
sealed abstract class Result[+Out, +A, +X] {
def out: Out
def value: A
def error: X
implicit def toOption: Option[A]
def map[B](f: A => B): Result[Out, B, X]
def mapOut[Out2](f: Out => Out2): Result[Out2, A, X]
def map[Out2, B](f: (Out, A) => (Out2, B)): Result[Out2, B, X]
def flatMap[Out2, B](f: (Out, A) => Result[Out2, B, Nothing]): Result[Out2, B, X]
def orElse[Out2 >: Out, B >: A](other: => Result[Out2, B, Nothing]): Result[Out2, B, X]
}
case class Success[+Out, +A](out: Out,
value: A) extends Result[Out, A, Nothing] {
def error = throw new ScalaSigParserError("No error")
def toOption = Some(value)
def map[B](f: A => B): Result[Out, B, Nothing] = Success(out, f(value))
def mapOut[Out2](f: Out => Out2): Result[Out2, A, Nothing] = Success(f(out), value)
def map[Out2, B](f: (Out, A) => (Out2, B)): Success[Out2, B] = f(out, value) match {case (out2, b) => Success(out2, b)}
def flatMap[Out2, B](f: (Out, A) => Result[Out2, B, Nothing]): Result[Out2, B, Nothing] = f(out, value)
def orElse[Out2 >: Out, B >: A](other: => Result[Out2, B, Nothing]): Result[Out2, B, Nothing] = this
}
sealed abstract class NoSuccess[+X] extends Result[Nothing, Nothing, X] {
def out = throw new ScalaSigParserError("No output")
def value = throw new ScalaSigParserError("No value")
def toOption = None
def map[B](f: Nothing => B) = this
def mapOut[Out2](f: Nothing => Out2) = this
def map[Out2, B](f: (Nothing, Nothing) => (Out2, B)) = this
def flatMap[Out2, B](f: (Nothing, Nothing) => Result[Out2, B, Nothing]) = this
def orElse[Out2, B](other: => Result[Out2, B, Nothing]) = other
}
case object Failure extends NoSuccess[Nothing] {
def error = throw new ScalaSigParserError("No error")
}
case class ScalaSigParserError(msg: String) extends RuntimeException(msg)
case class Error[+X](error: X) extends NoSuccess[X] {
}