-
Notifications
You must be signed in to change notification settings - Fork 2
/
Decoder.scala
39 lines (33 loc) · 1.08 KB
/
Decoder.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package freecli
package examples
package decoder
import cats.data.{Validated, ValidatedNel}
import freecli.argument.all._
import freecli.core.api.{StringDecoder, StringDecoderError}
object Decoder extends App {
sealed trait Fruit
case object Apple extends Fruit
case object Pear extends Fruit
case object Orange extends Fruit
implicit object fruitStringDecoder extends StringDecoder[Fruit] {
override def apply(value: String): ValidatedNel[StringDecoderError, Fruit] = {
value match {
case v if v.equalsIgnoreCase("Apple") => Validated.valid(Apple)
case v if v.equalsIgnoreCase("Pear") => Validated.valid(Pear)
case v if v.equalsIgnoreCase("Orange") => Validated.valid(Orange)
case v =>
Validated.invalidNel(StringDecoderError(
s"$v did not match any of (Apple, Pear, Orange)"))
}
}
override def toString(v: Fruit): String = {
v match {
case Apple => "Apple"
case Pear => "Pear"
case Orange => "Orange"
}
}
}
val res = runArgumentOrFail(arg[Fruit])(args)
println(res)
}