bleep.internal.EnumCodec.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bleep-model_2.12 Show documentation
Show all versions of bleep-model_2.12 Show documentation
A bleeping fast scala build tool!
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