CircleCI License: MIT

scala-iso

Scala library for ISO enums

Maven Central

libraryDependencies += "io.bartholomews" %% "scala-iso" % "0.1.3"

CountryCodeAlpha2 is a StringEnum (using enumeratum on the two-digits ISO string)

import io.bartholomews.iso.CountryCodeAlpha2
import io.circe._
import io.circe.syntax._

val italy: CountryCodeAlpha2 = CountryCodeAlpha2.ITALY

implicit val encoder: Encoder[CountryCodeAlpha2] = c => Json.fromString(c.value)
implicit val decoder: Decoder[CountryCodeAlpha2] = Decoder.decodeString.emap(
  str => CountryCodeAlpha2.values.find(_.value == str).toRight(s"Invalid ISO_3166-1 code: [$str]")
)

assert(italy.asJson == Json.fromString("IT"))
assert("IT".asJson.as[CountryCodeAlpha2].map(_.name) == Right("Italy"))

LanguageCode is a StringEnum (using enumeratum on the two-digits ISO string)

import io.bartholomews.iso.LanguageCode
import io.circe._
import io.circe.syntax._

val italy: LanguageCode = LanguageCode.ITALIAN

implicit val encoder: Encoder[LanguageCode] = c => Json.fromString(c.value)
implicit val decoder: Decoder[LanguageCode] = Decoder.decodeString.emap(
  str =>
    LanguageCode.values
      .find(_.value == str)
      .toRight(s"Invalid ISO_639 code: [$str]")
)

assert(italy.asJson == Json.fromString("it"))
assert("it".asJson.as[LanguageCode].map(_.name) == Right("Italian"))