Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou committed Sep 24, 2023
1 parent ea590ff commit 6728342
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 24 deletions.
6 changes: 3 additions & 3 deletions cats/src/io/github/iltotore/iron/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ object cats extends IronCatsInstances:
* @see [[validatedNec]], [[validatedNel]].
*/
def validated(value: A): Validated[String, T] =
if ops.rtc.test(value) then Validated.valid(value.asInstanceOf[T]) else Validated.invalid(ops.rtc.message)
Validated.fromEither(ops.either(value))

/**
* Refine the given value applicatively at runtime, resulting in a [[ValidatedNec]].
Expand All @@ -156,7 +156,7 @@ object cats extends IronCatsInstances:
* @see [[validated]], [[validatedNel]].
*/
def validatedNec(value: A): ValidatedNec[String, T] =
if ops.rtc.test(value) then Validated.validNec(value.asInstanceOf[T]) else Validated.invalidNec(ops.rtc.message)
Validated.fromEither(ops.either(value)).toValidatedNec

/**
* Refine the given value applicatively at runtime, resulting in a [[ValidatedNel]].
Expand All @@ -166,7 +166,7 @@ object cats extends IronCatsInstances:
* @see [[validated]], [[validatedNec]].
*/
def validatedNel(value: A): ValidatedNel[String, T] =
if ops.rtc.test(value) then Validated.validNel(value.asInstanceOf[T]) else Validated.invalidNel(ops.rtc.message)
Validated.fromEither(ops.either(value)).toValidatedNel

/**
* Represent all Cats' typeclass instances for Iron.
Expand Down
8 changes: 2 additions & 6 deletions main/src/io/github/iltotore/iron/RefinedTypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ object RefinedTypeOps:
*/
type FinalType = T


trait RefinedTypeOpsImpl[A, C, T](using rtcAuto: RuntimeConstraint.AutoDerived[A, A :| C]):
given rtc: RuntimeConstraint[A, T] = rtcAuto.inner.asInstanceOf[RuntimeConstraint[A, T]]
trait RefinedTypeOpsImpl[A, C, T](using rtc: RuntimeConstraint[A, C]):
protected given RuntimeConstraint[A, C] = rtc

/**
* Implicitly refine at compile-time the given value.
*
* @param value the value to refine.
* @param constraint the implementation of `C` to check.
* @tparam A the refined type.
* @tparam C the constraint applied to the type.
* @return the given value typed as [[IronType]]
Expand All @@ -75,7 +73,6 @@ trait RefinedTypeOpsImpl[A, C, T](using rtcAuto: RuntimeConstraint.AutoDerived[A
/**
* Refine the given value at runtime, resulting in an [[Either]].
*
* @param constraint the constraint to test with the value to refine.
* @return a [[Right]] containing this value as [[T]] or a [[Left]] containing the constraint message.
* @see [[fromIronType]], [[option]], [[applyUnsafe]].
*/
Expand All @@ -95,7 +92,6 @@ trait RefinedTypeOpsImpl[A, C, T](using rtcAuto: RuntimeConstraint.AutoDerived[A
/**
* Refine the given value at runtime.
*
* @param constraint the constraint to test with the value to refine.
* @return this value as [[T]].
* @throws an [[IllegalArgumentException]] if the constraint is not satisfied.
* @see [[fromIronType]], [[either]], [[option]].
Expand Down
17 changes: 3 additions & 14 deletions main/src/io/github/iltotore/iron/RuntimeConstraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@ package io.github.iltotore.iron

import scala.util.NotGiven

final class RuntimeConstraint[A, T](_test: A => Boolean, val message: String):
final class RuntimeConstraint[A, C](_test: A => Boolean, val message: String):
inline def test(value: A): Boolean = _test(value)

object RuntimeConstraint:

inline def derived[A, C](using inline c: Constraint[A, C]): RuntimeConstraint[A, A :| C] =
new RuntimeConstraint[A, A :| C](c.test(_), c.message)

final class AutoDerived[A, T](val inner: RuntimeConstraint[A, T])
object AutoDerived:
inline given [A, C](using rtc: RuntimeConstraint[A, A :| C]): AutoDerived[A, A :| C] =
new AutoDerived[A, A :| C](rtc)

inline given [A, C](using inline c: Constraint[A, C], ng: NotGiven[RuntimeConstraint[A, A :| C]]): AutoDerived[A, A :| C] =
new AutoDerived[A, A :| C](RuntimeConstraint.derived[A, C])

end RuntimeConstraint
inline given derived[A, C](using inline c: Constraint[A, C]): RuntimeConstraint[A, C] =
new RuntimeConstraint[A, C](c.test(_), c.message)
2 changes: 1 addition & 1 deletion zio/src/io/github/iltotore/iron/zio.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object zio extends RefinedTypeOpsZio:
* @return a [[Valid]] containing this value as [[T]] or an [[Validation.Failure]] containing a [[NonEmptyChunk]] of error messages.
*/
def validation(value: A): Validation[String, T] =
Validation.fromPredicateWith(ops.rtc.message)(value)(ops.rtc.test(_)).asInstanceOf[Validation[String, T]]
Validation.fromEither(ops.either(value))

private trait RefinedTypeOpsZio extends RefinedTypeOpsZioLowPriority:

Expand Down

0 comments on commit 6728342

Please sign in to comment.