Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Show type class #412

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package eu.timepit.refined.api

import eu.timepit.refined.internal.Resources

trait Show[T, P] extends Serializable {

type R

final type Res = Result[R]

def showExpr(t: T): String

def showResult(t: T, r: Res): String =
Resources.predicateResultDetailDot(r, showExpr(t))
}

object Show extends LowPriorityShowInstances {

type Aux[T, P, R0] = Show[T, P] { type R = R0 }

def apply[T, P](implicit s: Show[T, P]): Aux[T, P, s.R] = s

def instance[T, P, R0](showExprF: T => String): Aux[T, P, R0] =
new Show[T, P] {
override type R = R0

override def showExpr(t: T): String = showExprF(t)
}
}

trait LowPriorityShowInstances {

implicit def showFromValidate[T, P, R0](implicit v: Validate.Aux[T, P, R0]): Show.Aux[T, P, R0] =
new Show[T, P] {
override type R = R0

override def showExpr(t: T): String = v.showExpr(t)

override def showResult(t: T, r: Res): String = v.showResult(t, r)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.timepit.refined.internal

import eu.timepit.refined.api.{RefType, Validate}
import eu.timepit.refined.api.{RefType, Show, Validate}

/**
* Helper class that allows the type `T` to be inferred from calls like
Expand All @@ -17,6 +17,13 @@ final class RefinePartiallyApplied[F[_, _], P](rt: RefType[F]) {
else Left(v.showResult(t, res))
}

def apply2[T, R](t: T)(implicit v: Validate.Aux[T, P, R],
s: Show.Aux[T, P, R]): Either[String, F[T, P]] = {
val res = v.validate(t)
if (res.isPassed) Right(rt.unsafeWrap(t))
else Left(s.showResult(t, res))
}

def unsafeFrom[T](t: T)(implicit v: Validate[T, P]): F[T, P] =
apply(t).fold(err => throw new IllegalArgumentException(err), identity)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.timepit.refined

import eu.timepit.refined.api.{Inference, Validate}
import eu.timepit.refined.api.{Inference, Result, Show, Validate}
import eu.timepit.refined.api.Inference.==>
import eu.timepit.refined.boolean.{And, Not}
import eu.timepit.refined.numeric._
Expand Down Expand Up @@ -29,7 +29,7 @@ import shapeless.ops.nat.ToInt
*
* Note: `[[generic.Equal]]` can also be used for numeric types.
*/
object numeric extends NumericValidate with NumericInference {
object numeric extends NumericValidate with NumericShow with NumericInference {

/** Predicate that checks if a numeric value is less than `N`. */
final case class Less[N](n: N)
Expand Down Expand Up @@ -163,6 +163,19 @@ private[refined] trait NumericValidate {
)
}

private[refined] trait NumericShow {

implicit def lessEqualShowNat[N <: Nat, T](
implicit tn: ToInt[N]
): Show.Aux[T, LessEqual[N], Not[Result[Greater[N]]]] =
Show.instance(t => s"($t <= ${tn()})")

implicit def greaterEqualShowNat[N <: Nat, T](
implicit tn: ToInt[N]
): Show.Aux[T, GreaterEqual[N], Not[Result[Less[N]]]] =
Show.instance(t => s"($t >= ${tn()})")
}

private[refined] trait NumericInference {

implicit def lessInferenceWit[C, A <: C, B <: C](
Expand Down
19 changes: 17 additions & 2 deletions modules/core/shared/src/main/scala/eu/timepit/refined/string.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package eu.timepit.refined

import eu.timepit.refined.api.{Inference, Validate}
import eu.timepit.refined.api.{Inference, Result, Show, Validate}
import eu.timepit.refined.api.Inference.==>
import eu.timepit.refined.boolean.Not
import eu.timepit.refined.collection.{Empty, NonEmpty}
import eu.timepit.refined.string._
import shapeless.Witness

Expand All @@ -10,7 +12,7 @@ import shapeless.Witness
* in `[[collection]]` also work for `String`s by treating them as sequences
* of `Char`s.
*/
object string extends StringValidate with StringInference {
object string extends StringValidate with StringShow with StringInference {

/** Predicate that checks if a `String` ends with the suffix `S`. */
final case class EndsWith[S](s: S)
Expand Down Expand Up @@ -132,6 +134,19 @@ private[refined] trait StringValidate {
.fromPartial(javax.xml.xpath.XPathFactory.newInstance().newXPath().compile, "XPath", XPath())
}

private[refined] trait StringShow {

implicit def nonEmptyShow: Show.Aux[String, NonEmpty, Not[Result[Empty]]] =
new Show[String, NonEmpty] {
override type R = Not[Result[Empty]]

override def showExpr(t: String): String = s"isEmpty($t)"

override def showResult(t: String, r: Res): String =
"Cannot create a NonEmptyString with the empty string"
}
}

private[refined] trait StringInference {

implicit def endsWithInference[A <: String, B <: String](
Expand Down