All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scodec.codecs.RecoverCodec.scala Maven / Gradle / Ivy

There is a newer version: 1.11.3
Show newest version
package scodec
package codecs

import scodec.bits.BitVector

private[codecs] final class RecoverCodec(target: Codec[Unit], lookahead: Boolean) extends Codec[Boolean] {

  def sizeBound = target.sizeBound

  def encode(a: Boolean)= if(a) target.encode(()) else Attempt.successful(BitVector.empty)

  def decode(buffer: BitVector) =
    target.decode(buffer) match {
      case Attempt.Successful(DecodeResult(_, rest)) => Attempt.successful(DecodeResult(true, if (lookahead) buffer else rest))
      case f: Attempt.Failure => Attempt.successful(DecodeResult(false, buffer))
    }

  override def toString = if (lookahead) s"lookahead($target)" else s"recover($target)"
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy