Skip to content

Commit

Permalink
misc: Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltotore committed Aug 29, 2024
1 parent d320f4a commit a909b4d
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 67 deletions.
9 changes: 4 additions & 5 deletions cats/test/src/io/github/iltotore/iron/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ object CatsSuite extends TestSuite:
test("validatedNel"):
test - assert(valid.refineAllValidatedNel[Positive] == Valid(valid))
test - assert(invalid.refineAllValidatedNel[Positive] == Invalid(NonEmptyList.of(
InvalidValue(-2, "Should be strictly positive"),
InvalidValue(-3, "Should be strictly positive")
)))
InvalidValue(-2, "Should be strictly positive"),
InvalidValue(-3, "Should be strictly positive")
)))

test("further"):

Expand Down Expand Up @@ -231,6 +231,5 @@ object CatsSuite extends TestSuite:
InvalidValue(-2, "Should be strictly positive"),
InvalidValue(-3, "Should be strictly positive")
)))



}
2 changes: 1 addition & 1 deletion main/src/io/github/iltotore/iron/RefinedTypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ object RefinedTypeOps:
* //FinalType =/= IronType
* }}}
*/
type FinalType = T
type FinalType = T
6 changes: 3 additions & 3 deletions main/src/io/github/iltotore/iron/constraint/char.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object char:
private def check(expr: Expr[Char])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

expr.decode match
case Right(value) => Expr(value.isUpper)
case _ => '{ $expr.isUpper }
Expand All @@ -98,7 +98,7 @@ object char:
private def check(expr: Expr[Char])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

expr.decode match
case Right(value) => Expr(value.isDigit)
case _ => '{ $expr.isDigit }
Expand All @@ -114,7 +114,7 @@ object char:
private def check(expr: Expr[Char])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

expr.decode match
case Right(value) => Expr(value.isLetter)
case _ => '{ $expr.isLetter }
4 changes: 2 additions & 2 deletions main/src/io/github/iltotore/iron/constraint/collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ object collection:

expr.decode match
case Right(value) => applyConstraint(Expr(value.length), constraintExpr)
case _ => applyConstraint('{ $expr.length }, constraintExpr)
case _ => applyConstraint('{ $expr.length }, constraintExpr)

given [C1, C2](using C1 ==> C2): (Length[C1] ==> Length[C2]) = Implication()

Expand All @@ -140,7 +140,7 @@ object collection:

(expr.decode, partExpr.decode) match
case (Right(value), Right(part)) => Expr(value.contains(part))
case _ => '{ ${ expr }.contains($partExpr) }
case _ => '{ ${ expr }.contains($partExpr) }

object ForAll:

Expand Down
12 changes: 6 additions & 6 deletions main/src/io/github/iltotore/iron/constraint/string.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ object string:
private def check(expr: Expr[String], prefixExpr: Expr[String])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, prefixExpr.decode) match
case (Right(value), Right(prefix)) => Expr(value.startsWith(prefix))
case _ => '{ $expr.startsWith($prefixExpr) }
case _ => '{ $expr.startsWith($prefixExpr) }

object EndWith:

Expand All @@ -123,10 +123,10 @@ object string:
private def check(expr: Expr[String], prefixExpr: Expr[String])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, prefixExpr.decode) match
case (Right(value), Right(prefix)) => Expr(value.endsWith(prefix))
case _ => '{ $expr.endsWith($prefixExpr) }
case _ => '{ $expr.endsWith($prefixExpr) }

object Match:

Expand All @@ -139,7 +139,7 @@ object string:
private def check(valueExpr: Expr[String], regexExpr: Expr[String])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(valueExpr.decode, regexExpr.decode) match
case (Right(value), Right(regex)) => Expr(value.matches(regex))
case _ => '{ $valueExpr.matches($regexExpr) }
case _ => '{ $valueExpr.matches($regexExpr) }
4 changes: 2 additions & 2 deletions main/src/io/github/iltotore/iron/internal/IronConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.iltotore.iron.internal

/**
* The config or Iron at compile-time.
*
*
* @param color enable colored messages
* @param shortMessages use abbreviated messages, useful for error lenses and similar
*/
Expand All @@ -15,5 +15,5 @@ object IronConfig:
*/
val fromSystem: IronConfig = IronConfig(
color = sys.props.get("iron.color").orElse(sys.env.get("IRON_COLOR")).flatMap(_.toBooleanOption).getOrElse(true),
shortMessages = sys.props.get("iron.shortMessages").orElse(sys.env.get("IRON_SHORT_MESSAGES")).flatMap(_.toBooleanOption).getOrElse(false),
shortMessages = sys.props.get("iron.shortMessages").orElse(sys.env.get("IRON_SHORT_MESSAGES")).flatMap(_.toBooleanOption).getOrElse(false)
)
1 change: 0 additions & 1 deletion main/src/io/github/iltotore/iron/internal/package.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.iltotore.iron.internal

extension (text: String)

def colorized(color: String)(using config: IronConfig): String =
if config.color then s"$color$text${Console.RESET}"
else text
49 changes: 24 additions & 25 deletions main/src/io/github/iltotore/iron/macros/ReflectUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
import _quotes.reflect.*

extension [T: Type](expr: Expr[T])

/**
* Decode this expression.
*
Expand Down Expand Up @@ -113,7 +112,7 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
*/
def prettyPrint(bodyIdent: Int = 0, firstLineIdent: Int = 0)(using Printer[Tree]): String =
val unindented = this match
case NotInlined(term) => s"Term not inlined: ${term.show}"
case NotInlined(term) => s"Term not inlined: ${term.show}"
case DefinitionNotInlined(name) => s"Definition not inlined: $name. Only vals and zero-arg def can be inlined."
case HasBindings(defFailures) =>
val failures = defFailures
Expand Down Expand Up @@ -167,9 +166,9 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
case StringPartsNotInlined(parts) =>
val errors = parts
.zipWithIndex
.collect:
case (Left(failure), i) => s"Arg $i:\n${failure.prettyPrint(2, 2)}"
.mkString("\n\n")
.collect:
case (Left(failure), i) => s"Arg $i:\n${failure.prettyPrint(2, 2)}"
.mkString("\n\n")

s"String contatenation has non inlined arguments:\n$errors"

Expand All @@ -183,7 +182,7 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):

private val enhancedDecoders: Map[TypeRepr, (Term, Map[String, ?]) => Either[DecodingFailure, ?]] = Map(
TypeRepr.of[Boolean] -> decodeBoolean,
TypeRepr.of[String] -> decodeString
TypeRepr.of[String] -> decodeString
)

/**
Expand All @@ -203,7 +202,7 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):

specializedResult match
case Left(DecodingFailure.Unknown) => decodeUnspecializedTerm(tree, definitions)
case result => result.asInstanceOf[Either[DecodingFailure, T]]
case result => result.asInstanceOf[Either[DecodingFailure, T]]

/**
* Decode a term using only unspecialized cases.
Expand All @@ -215,24 +214,24 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
*/
def decodeUnspecializedTerm[T](tree: Term, definitions: Map[String, ?]): Either[DecodingFailure, T] =
tree match
case block@Block(stats, e) => if stats.isEmpty then decodeTerm(e, definitions) else Left(DecodingFailure.HasStatements(block))
case block @ Block(stats, e) => if stats.isEmpty then decodeTerm(e, definitions) else Left(DecodingFailure.HasStatements(block))

case Inlined(_, bindings, e) =>
val (failures, values) = bindings
.map[(String, Either[DecodingFailure, ?])](b => (b.name, decodeBinding(b, definitions)))
.partitionMap:
case (name, Right(value)) => Right((name, value))
case (name, Right(value)) => Right((name, value))
case (name, Left(failure)) => Left((name, failure))

(failures, decodeTerm[T](e, definitions ++ values.toMap)) match
case (_, Right(value)) =>
Right(value)
case (Nil, Left(failure)) => Left(failure)
case (failures, Left(_)) => Left(DecodingFailure.HasBindings(failures))
case (failures, Left(_)) => Left(DecodingFailure.HasBindings(failures))

case Apply(Select(left, "=="), List(right)) => (decodeTerm[Any](left, definitions), decodeTerm[Any](right, definitions)) match
case (Right(leftValue), Right(rightValue)) => Right((leftValue == rightValue).asInstanceOf[T])
case (leftResult, rightResult) => Left(DecodingFailure.ApplyNotInlined("==", List(leftResult, rightResult)))
case (Right(leftValue), Right(rightValue)) => Right((leftValue == rightValue).asInstanceOf[T])
case (leftResult, rightResult) => Left(DecodingFailure.ApplyNotInlined("==", List(leftResult, rightResult)))

case Apply(Select(leftOperand, name), operands) =>
val rightResults = operands.map(decodeTerm(_, definitions))
Expand All @@ -259,14 +258,14 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
case Typed(e, _) => decodeTerm(e, definitions)

case Ident(name) => definitions
.get(name)
.toRight(DecodingFailure.NotInlined(tree))
.asInstanceOf[Either[DecodingFailure, T]]
.get(name)
.toRight(DecodingFailure.NotInlined(tree))
.asInstanceOf[Either[DecodingFailure, T]]

case _ =>
tree.tpe.widenTermRefByName match
case ConstantType(c) => Right(c.value.asInstanceOf[T])
case _ => Left(DecodingFailure.NotInlined(tree))
case _ => Left(DecodingFailure.NotInlined(tree))

/**
* Decode a binding/definition.
Expand All @@ -277,9 +276,9 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
* @return the value of the given definition found at compile time or a [[DecodingFailure]]
*/
def decodeBinding[T](definition: Definition, definitions: Map[String, ?]): Either[DecodingFailure, T] = definition match
case ValDef(name, tpeTree, Some(term)) => decodeTerm(term, definitions)
case ValDef(name, tpeTree, Some(term)) => decodeTerm(term, definitions)
case DefDef(name, Nil, tpeTree, Some(term)) => decodeTerm(term, definitions)
case _ => Left(DecodingFailure.DefinitionNotInlined(definition.name))
case _ => Left(DecodingFailure.DefinitionNotInlined(definition.name))

/**
* Decode a [[Boolean]] term using only [[Boolean]]-specific cases.
Expand All @@ -291,17 +290,17 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
def decodeBoolean(term: Term, definitions: Map[String, ?]): Either[DecodingFailure, Boolean] = term match
case Apply(Select(left, "||"), List(right)) if left.tpe <:< TypeRepr.of[Boolean] && right.tpe <:< TypeRepr.of[Boolean] => // OR
(decodeTerm[Boolean](left, definitions), decodeTerm[Boolean](right, definitions)) match
case (Right(true), _) => Right(true)
case (_, Right(true)) => Right(true)
case (Right(true), _) => Right(true)
case (_, Right(true)) => Right(true)
case (Right(leftValue), Right(rightValue)) => Right(leftValue || rightValue)
case (leftResult, rightResult) => Left(DecodingFailure.OrNotInlined(leftResult, rightResult))
case (leftResult, rightResult) => Left(DecodingFailure.OrNotInlined(leftResult, rightResult))

case Apply(Select(left, "&&"), List(right)) if left.tpe <:< TypeRepr.of[Boolean] && right.tpe <:< TypeRepr.of[Boolean] => // AND
(decodeTerm[Boolean](left, definitions), decodeTerm[Boolean](right, definitions)) match
case (Right(false), _) => Right(false)
case (_, Right(false)) => Right(false)
case (Right(false), _) => Right(false)
case (_, Right(false)) => Right(false)
case (Right(leftValue), Right(rightValue)) => Right(leftValue && rightValue)
case (leftResult, rightResult) => Left(DecodingFailure.AndNotInlined(leftResult, rightResult))
case (leftResult, rightResult) => Left(DecodingFailure.AndNotInlined(leftResult, rightResult))

case _ => Left(DecodingFailure.Unknown)

Expand All @@ -324,4 +323,4 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
Left(DecodingFailure.StringPartsNotInlined(leftResult +: rparts))
case (leftResult, rightResult) => Left(DecodingFailure.StringPartsNotInlined(List(leftResult, rightResult)))

case _ => Left(DecodingFailure.Unknown)
case _ => Left(DecodingFailure.Unknown)
4 changes: 2 additions & 2 deletions main/src/io/github/iltotore/iron/macros/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def compileTimeError(msg: String)(using Quotes): Nothing =
|----------------------------------------------------------------------------""".stripMargin
)

inline def isIronType[T, C]: Boolean = ${isIronTypeImpl[T, C]}
def isIronTypeImpl[T : Type, C : Type](using Quotes): Expr[Boolean] =
inline def isIronType[T, C]: Boolean = ${ isIronTypeImpl[T, C] }
def isIronTypeImpl[T: Type, C: Type](using Quotes): Expr[Boolean] =
import quotes.reflect.*

val ironType = TypeRepr.of[IronType]
Expand Down
12 changes: 7 additions & 5 deletions zio/src/io/github/iltotore/iron/zio.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ object zio extends RefinedTypeOpsZio:
Validation.fromPredicateWith(constraint.message)(value.asInstanceOf[A :| C])(constraint.test(_))

extension [F[+_], A](wrapper: F[A])

inline def refineAllValidation[C](using forEach: ForEach[F], inline constraint: Constraint[A, C]): Validation[InvalidValue[A], F[A :| C]] =
forEach.forEach(wrapper): value =>
Validation.fromPredicateWith[InvalidValue[A], A :| C](InvalidValue(value, constraint.message))(value.assume[C])(constraint.test(_))
Expand All @@ -32,10 +31,14 @@ object zio extends RefinedTypeOpsZio:
(value: A).refineValidation[C2].map(_.assumeFurther[C1])

extension [F[+_], A, C1](wrapper: F[A :| C1])

inline def refineAllFurtherValidation[C2](using forEach: ForEach[F], inline constraint: Constraint[A, C2]): Validation[InvalidValue[A], F[A :| (C1 & C2)]] =
inline def refineAllFurtherValidation[C2](using
forEach: ForEach[F],
inline constraint: Constraint[A, C2]
): Validation[InvalidValue[A], F[A :| (C1 & C2)]] =
forEach.forEach(wrapper): value =>
Validation.fromPredicateWith[InvalidValue[A], A :| (C1 & C2)](InvalidValue(value, constraint.message))(value.assume[C1 & C2])(constraint.test(_))
Validation.fromPredicateWith[InvalidValue[A], A :| (C1 & C2)](InvalidValue(value, constraint.message))(value.assume[C1 & C2])(
constraint.test(_)
)

extension [A, C, T](ops: RefinedTypeOps[A, C, T])
/**
Expand All @@ -47,7 +50,6 @@ object zio extends RefinedTypeOpsZio:
Validation.fromPredicateWith(ops.rtc.message)(value)(ops.rtc.test(_)).asInstanceOf[Validation[String, T]]

extension [A, C, T](ops: RefinedTypeOps[A, C, T])

/**
* Refine the given values applicatively at runtime, resulting in a [[Validation]].
*
Expand Down
39 changes: 24 additions & 15 deletions zio/test/src/io/github/iltotore/iron/ZIOSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,33 @@ object ZIOSuite extends TestSuite:

test("validation"):
test - assert(valid.refineAllValidation[Positive] == ZValidation.Success(Chunk.empty, Chunk.from(valid)))
test - assert(invalid.refineAllValidation[Positive] == ZValidation.Failure(Chunk.empty, NonEmptyChunk(
InvalidValue(-2, "Should be strictly positive"),
InvalidValue(-3, "Should be strictly positive")
)))

test - assert(invalid.refineAllValidation[Positive] == ZValidation.Failure(
Chunk.empty,
NonEmptyChunk(
InvalidValue(-2, "Should be strictly positive"),
InvalidValue(-3, "Should be strictly positive")
)
))

test("newtype"):
test - assert(Temperature.validationAll(valid) == ZValidation.Success(Chunk.empty, Chunk.from(valid)))
test - assert(Temperature.validationAll(invalid) == ZValidation.Failure(Chunk.empty, NonEmptyChunk(
InvalidValue(-2, "Should be strictly positive"),
InvalidValue(-3, "Should be strictly positive")
)))

test - assert(Temperature.validationAll(invalid) == ZValidation.Failure(
Chunk.empty,
NonEmptyChunk(
InvalidValue(-2, "Should be strictly positive"),
InvalidValue(-3, "Should be strictly positive")
)
))

test("furtherValidation"):
val furtherValid = List(2, 4, 6).refineAllUnsafe[Positive]
val furtherInvalid = List(1, 2, 3).refineAllUnsafe[Positive]

test - assert(furtherValid.refineAllFurtherValidation[Even] == ZValidation.Success(Chunk.empty, Chunk.from(furtherValid)))
test - assert(furtherInvalid.refineAllFurtherValidation[Even] == ZValidation.Failure(Chunk.empty, NonEmptyChunk(
InvalidValue(1, "Should be a multiple of 2"),
InvalidValue(3, "Should be a multiple of 2")
)))
test - assert(furtherInvalid.refineAllFurtherValidation[Even] == ZValidation.Failure(
Chunk.empty,
NonEmptyChunk(
InvalidValue(1, "Should be a multiple of 2"),
InvalidValue(3, "Should be a multiple of 2")
)
))

0 comments on commit a909b4d

Please sign in to comment.