diff --git a/cats/test/src/io/github/iltotore/iron/CatsSuite.scala b/cats/test/src/io/github/iltotore/iron/CatsSuite.scala index fe5c1035..ebb59c75 100644 --- a/cats/test/src/io/github/iltotore/iron/CatsSuite.scala +++ b/cats/test/src/io/github/iltotore/iron/CatsSuite.scala @@ -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"): @@ -231,6 +231,5 @@ object CatsSuite extends TestSuite: InvalidValue(-2, "Should be strictly positive"), InvalidValue(-3, "Should be strictly positive") ))) - - + } diff --git a/main/src/io/github/iltotore/iron/RefinedTypeOps.scala b/main/src/io/github/iltotore/iron/RefinedTypeOps.scala index 4004981d..1059fe2e 100644 --- a/main/src/io/github/iltotore/iron/RefinedTypeOps.scala +++ b/main/src/io/github/iltotore/iron/RefinedTypeOps.scala @@ -177,4 +177,4 @@ object RefinedTypeOps: * //FinalType =/= IronType * }}} */ - type FinalType = T \ No newline at end of file + type FinalType = T diff --git a/main/src/io/github/iltotore/iron/constraint/char.scala b/main/src/io/github/iltotore/iron/constraint/char.scala index e88a47ec..d66ec66c 100644 --- a/main/src/io/github/iltotore/iron/constraint/char.scala +++ b/main/src/io/github/iltotore/iron/constraint/char.scala @@ -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 } @@ -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 } @@ -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 } diff --git a/main/src/io/github/iltotore/iron/constraint/collection.scala b/main/src/io/github/iltotore/iron/constraint/collection.scala index d6c3fcbb..034ec2c4 100644 --- a/main/src/io/github/iltotore/iron/constraint/collection.scala +++ b/main/src/io/github/iltotore/iron/constraint/collection.scala @@ -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() @@ -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: diff --git a/main/src/io/github/iltotore/iron/constraint/string.scala b/main/src/io/github/iltotore/iron/constraint/string.scala index 8d0a713b..7a4ed2c1 100644 --- a/main/src/io/github/iltotore/iron/constraint/string.scala +++ b/main/src/io/github/iltotore/iron/constraint/string.scala @@ -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: @@ -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: @@ -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) } diff --git a/main/src/io/github/iltotore/iron/internal/IronConfig.scala b/main/src/io/github/iltotore/iron/internal/IronConfig.scala index e042cf92..3bbc9c7a 100644 --- a/main/src/io/github/iltotore/iron/internal/IronConfig.scala +++ b/main/src/io/github/iltotore/iron/internal/IronConfig.scala @@ -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 */ @@ -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) ) diff --git a/main/src/io/github/iltotore/iron/internal/package.scala b/main/src/io/github/iltotore/iron/internal/package.scala index 905bf32a..bc49409e 100644 --- a/main/src/io/github/iltotore/iron/internal/package.scala +++ b/main/src/io/github/iltotore/iron/internal/package.scala @@ -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 diff --git a/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala b/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala index 231addf2..3e157ac0 100644 --- a/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala +++ b/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala @@ -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. * @@ -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 @@ -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" @@ -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 ) /** @@ -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. @@ -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)) @@ -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. @@ -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. @@ -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) @@ -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) \ No newline at end of file + case _ => Left(DecodingFailure.Unknown) diff --git a/main/src/io/github/iltotore/iron/macros/package.scala b/main/src/io/github/iltotore/iron/macros/package.scala index 61a797e0..2effdd24 100644 --- a/main/src/io/github/iltotore/iron/macros/package.scala +++ b/main/src/io/github/iltotore/iron/macros/package.scala @@ -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] diff --git a/zio/src/io/github/iltotore/iron/zio.scala b/zio/src/io/github/iltotore/iron/zio.scala index 296002bd..f8a92ba2 100644 --- a/zio/src/io/github/iltotore/iron/zio.scala +++ b/zio/src/io/github/iltotore/iron/zio.scala @@ -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(_)) @@ -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]) /** @@ -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]]. * diff --git a/zio/test/src/io/github/iltotore/iron/ZIOSuite.scala b/zio/test/src/io/github/iltotore/iron/ZIOSuite.scala index e8459553..f90b68f4 100644 --- a/zio/test/src/io/github/iltotore/iron/ZIOSuite.scala +++ b/zio/test/src/io/github/iltotore/iron/ZIOSuite.scala @@ -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") - ))) \ No newline at end of file + 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") + ) + ))