Refined integration

Atto benefits from an integration with the excellent refined library.


The refined integration lives in the atto-refined module:

libraryDependencies += "org.tpolecat" %% "atto-refined" % "0.7.0"


We’ll need the usual imports as well as a specific atto-refined import:

import atto._, Atto._, syntax.refined._
import eu.timepit.refined.numeric._

We can refine any Parser[T] with a predicate P to obtain a Parser[T Refined P], for example if we pick up our int parser and refine it to parse only positive integers:

scala> val positiveInt = int.refined[Positive]
positiveInt: atto.Parser[eu.timepit.refined.api.Refined[Int,eu.timepit.refined.numeric.Positive]] = (int) flatMap ...

It will be able to parse positive integers:

scala> positiveInt parseOnly "123"
res0: atto.ParseResult[eu.timepit.refined.api.Refined[Int,eu.timepit.refined.numeric.Positive]] = Done(,123)

And will fail on negative integers:

scala> positiveInt parseOnly "-123"
res1: atto.ParseResult[eu.timepit.refined.api.Refined[Int,eu.timepit.refined.numeric.Positive]] = Fail(,List(),Predicate failed: (-123 > 0).)

Check out the refined’s library README to see a list of all the provided predicates.