Libra module

kantan.csv comes with a libra module that can be used by adding the following dependency to your build.sbt:

libraryDependencies += "com.nrinaudo" %% "kantan.csv-libra" % "0.7.0"

You then need to import the corresponding package:

import kantan.csv.libra._

And that’s pretty much it. You can now encode and decode refined types directly.

Let’s first set our types up:

import libra._
import kantan.csv._
import kantan.csv.ops._

type Distance = QuantityOf[Int, Length, Metre]
type Duration = QuantityOf[Int, Time, Second]

We can then simply write the following:

"1,2".readCsv[List, (Distance, Duration)](rfc)
// res0: List[ReadResult[(Distance, Duration)]] = List(
//   Right(value = (Quantity(value = 1), Quantity(value = 2)))
// )

Other tutorials: