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

bleep.internal.EnumCodec.scala Maven / Gradle / Ivy

The newest version!
package bleep.internal

import io.circe.{Codec, Decoder, Encoder}

object EnumCodec {
  def codec[T](all: Map[String, T]): Codec[T] = {
    val decoder: Decoder[T] =
      Decoder[String].emap(str => all.get(str).toRight(s"$str not among ${all.keys.mkString(", ")}"))

    val encoder: Encoder[T] =
      Encoder[String].contramap(t => all.collectFirst { case (str, `t`) => str }.get)

    Codec.from(decoder, encoder)
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy