Skip to content

Commit

Permalink
misc: Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltotore committed Sep 13, 2024
1 parent 6b2f8b5 commit 2ea32c0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
6 changes: 4 additions & 2 deletions main/src/io/github/iltotore/iron/constraint/collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ object collection:

inline given lengthString[C, Impl <: Constraint[Int, C]](using inline impl: Impl): LengthString[C, Impl] = new LengthString

private def checkIterable[I <: Iterable[?] : Type, C, Impl <: Constraint[Int, C]](expr: Expr[I], constraintExpr: Expr[Impl])(using Quotes): Expr[Boolean] =
private def checkIterable[I <: Iterable[?]: Type, C, Impl <: Constraint[Int, C]](expr: Expr[I], constraintExpr: Expr[Impl])(using
Quotes
): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

Expand Down Expand Up @@ -142,7 +144,7 @@ object collection:

override inline def message: String = "Should contain the string " + constValue[V]

private def checkIterable[I <: Iterable[?] : Type, V : Type](expr: Expr[I], partExpr: Expr[V])(using Quotes): Expr[Boolean] =
private def checkIterable[I <: Iterable[?]: Type, V: Type](expr: Expr[I], partExpr: Expr[V])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

Expand Down
42 changes: 21 additions & 21 deletions main/src/io/github/iltotore/iron/constraint/numeric.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,38 +167,38 @@ object numeric:
override inline def test(inline value: Double): Boolean = value > doubleValue[V]

inline given bigDecimalDouble[V <: NumConstant]: GreaterConstraint[BigDecimal, V] with
override inline def test(inline value: BigDecimal): Boolean = ${checkBigDecimalDouble('value, '{doubleValue[V]})}
override inline def test(inline value: BigDecimal): Boolean = ${ checkBigDecimalDouble('value, '{ doubleValue[V] }) }

inline given bigDecimalLong[V <: Int | Long]: GreaterConstraint[BigDecimal, V] with
override inline def test(inline value: BigDecimal): Boolean = ${checkBigDecimalLong('value, '{longValue[V]})}
override inline def test(inline value: BigDecimal): Boolean = ${ checkBigDecimalLong('value, '{ longValue[V] }) }

inline given [V <: Int | Long]: GreaterConstraint[BigInt, V] with
override inline def test(inline value: BigInt): Boolean = ${checkBigInt('value, '{longValue[V]})}
override inline def test(inline value: BigInt): Boolean = ${ checkBigInt('value, '{ longValue[V] }) }

private def checkBigDecimalDouble(expr: Expr[BigDecimal], thanExpr: Expr[Double])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(value > BigDecimal(than))
case _ => '{$expr > BigDecimal($thanExpr)}
case _ => '{ $expr > BigDecimal($thanExpr) }

private def checkBigDecimalLong(expr: Expr[BigDecimal], thanExpr: Expr[Long])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(value > BigDecimal(than))
case _ => '{$expr > BigDecimal($thanExpr)}
case _ => '{ $expr > BigDecimal($thanExpr) }

private def checkBigInt(expr: Expr[BigInt], thanExpr: Expr[Long])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(value > BigInt(than))
case _ => '{$expr > BigInt($thanExpr)}
case _ => '{ $expr > BigInt($thanExpr) }

given [V1, V2](using V1 > V2 =:= true): (Greater[V1] ==> Greater[V2]) = Implication()

given [V1, V2](using V1 > V2 =:= true): (StrictEqual[V1] ==> Greater[V2]) = Implication()
Expand Down Expand Up @@ -226,37 +226,37 @@ object numeric:
override inline def test(inline value: Double): Boolean = value < doubleValue[V]

inline given bigDecimalDouble[V <: NumConstant]: LessConstraint[BigDecimal, V] with
override inline def test(inline value: BigDecimal): Boolean = ${checkBigDecimalDouble('value, '{doubleValue[V]})}
override inline def test(inline value: BigDecimal): Boolean = ${ checkBigDecimalDouble('value, '{ doubleValue[V] }) }

inline given bigDecimalLong[V <: Int | Long]: LessConstraint[BigDecimal, V] with
override inline def test(inline value: BigDecimal): Boolean = ${checkBigDecimalLong('value, '{longValue[V]})}
override inline def test(inline value: BigDecimal): Boolean = ${ checkBigDecimalLong('value, '{ longValue[V] }) }

inline given [V <: Int | Long]: LessConstraint[BigInt, V] with
override inline def test(inline value: BigInt): Boolean = ${checkBigInt('value, '{longValue[V]})}
override inline def test(inline value: BigInt): Boolean = ${ checkBigInt('value, '{ longValue[V] }) }

private def checkBigDecimalDouble(expr: Expr[BigDecimal], thanExpr: Expr[Double])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(value < BigDecimal(than))
case _ => '{$expr < BigDecimal($thanExpr)}
case _ => '{ $expr < BigDecimal($thanExpr) }

private def checkBigDecimalLong(expr: Expr[BigDecimal], thanExpr: Expr[Long])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(value < BigDecimal(than))
case _ => '{$expr < BigDecimal($thanExpr)}
case _ => '{ $expr < BigDecimal($thanExpr) }

private def checkBigInt(expr: Expr[BigInt], thanExpr: Expr[Long])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(value < BigInt(than))
case _ => '{$expr < BigInt($thanExpr)}
case _ => '{ $expr < BigInt($thanExpr) }

given [V1, V2](using V1 < V2 =:= true): (Less[V1] ==> Less[V2]) = Implication()

Expand Down Expand Up @@ -286,27 +286,27 @@ object numeric:

inline given [V <: NumConstant]: MultipleConstraint[BigDecimal, V] with

override inline def test(inline value: BigDecimal): Boolean = ${checkBigDecimal('value, '{doubleValue[V]})}
override inline def test(inline value: BigDecimal): Boolean = ${ checkBigDecimal('value, '{ doubleValue[V] }) }

inline given [V <: Int | Long]: MultipleConstraint[BigInt, V] with

override inline def test(inline value: BigInt): Boolean = ${checkBigInt('value, '{longValue[V]})}
override inline def test(inline value: BigInt): Boolean = ${ checkBigInt('value, '{ longValue[V] }) }

private def checkBigDecimal(expr: Expr[BigDecimal], thanExpr: Expr[Double])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(value % BigDecimal(than) == 0)
case _ => '{$expr % BigDecimal($thanExpr) == 0}
case _ => '{ $expr % BigDecimal($thanExpr) == 0 }

private def checkBigInt(expr: Expr[BigInt], thanExpr: Expr[Long])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(value % BigInt(than) == 0)
case _ => '{$expr % BigInt($thanExpr) == 0}
case _ => '{ $expr % BigInt($thanExpr) == 0 }

given [A, V1 <: A, V2 <: A](using V1 % V2 =:= Zero[A]): (Multiple[V1] ==> Multiple[V2]) = Implication()

Expand All @@ -327,26 +327,26 @@ object numeric:
override inline def test(inline value: Double): Boolean = doubleValue[V] % value == 0

inline given [V <: NumConstant]: DivideConstraint[BigDecimal, V] with
override inline def test(inline value: BigDecimal): Boolean = ${checkBigDecimal('value, '{doubleValue[V]})}
override inline def test(inline value: BigDecimal): Boolean = ${ checkBigDecimal('value, '{ doubleValue[V] }) }

inline given [V <: Int | Long]: DivideConstraint[BigInt, V] with
override inline def test(inline value: BigInt): Boolean = ${checkBigInt('value, '{longValue[V]})}
override inline def test(inline value: BigInt): Boolean = ${ checkBigInt('value, '{ longValue[V] }) }

private def checkBigDecimal(expr: Expr[BigDecimal], thanExpr: Expr[Double])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(BigDecimal(than) % value == 0)
case _ => '{BigDecimal($thanExpr) % $expr == 0}
case _ => '{ BigDecimal($thanExpr) % $expr == 0 }

private def checkBigInt(expr: Expr[BigInt], thanExpr: Expr[Long])(using Quotes): Expr[Boolean] =
val rflUtil = reflectUtil
import rflUtil.*

(expr.decode, thanExpr.decode) match
case (Right(value), Right(than)) => Expr(BigInt(than) % value == 0)
case _ => '{BigInt($thanExpr) % $expr == 0}
case _ => '{ BigInt($thanExpr) % $expr == 0 }

object NaN:
private trait NaNConstraint[A] extends Constraint[A, NaN]:
Expand Down
36 changes: 18 additions & 18 deletions main/src/io/github/iltotore/iron/macros/ReflectUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
import _quotes.reflect.*

type DecodingResult[+T] = Either[DecodingFailure, T]
extension [T](result: DecodingResult[T])
private def as[U]: DecodingResult[U] = result.asInstanceOf[DecodingResult[U]]
extension [T](result: DecodingResult[T])
private def as[U]: DecodingResult[U] = result.asInstanceOf[DecodingResult[U]]

extension [T: Type](expr: Expr[T])
/**
Expand Down Expand Up @@ -186,11 +186,11 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
object ExprDecoder:

private val enhancedDecoders: Map[TypeRepr, (Term, Map[String, ?]) => DecodingResult[?]] = Map(
TypeRepr.of[Boolean] -> decodeBoolean,
TypeRepr.of[Boolean] -> decodeBoolean,
TypeRepr.of[BigDecimal] -> decodeBigDecimal,
TypeRepr.of[BigInt] -> decodeBigInt,
TypeRepr.of[List[?]] -> decodeList,
TypeRepr.of[String] -> decodeString
TypeRepr.of[BigInt] -> decodeBigInt,
TypeRepr.of[List[?]] -> decodeList,
TypeRepr.of[String] -> decodeString
)

/**
Expand Down Expand Up @@ -267,12 +267,12 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
case _ =>
tree.tpe.widenTermRefByName match
case ConstantType(c) => Right(c.value)
case _ => tree match
case Ident(name) => definitions
.get(name)
.toRight(DecodingFailure.NotInlined(tree))
case _ => Left(DecodingFailure.NotInlined(tree))
case _ => tree match
case Ident(name) => definitions
.get(name)
.toRight(DecodingFailure.NotInlined(tree))

case _ => Left(DecodingFailure.NotInlined(tree))

/**
* Decode a binding/definition.
Expand Down Expand Up @@ -343,7 +343,7 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
term match
case Apply(Select(Ident("BigInt"), "apply"), List(value)) =>
decodeTerm(value, definitions).as[Int | Long].map:
case x: Int => BigInt(x)
case x: Int => BigInt(x)
case x: Long => BigInt(x)
case _ => Left(DecodingFailure.Unknown)

Expand All @@ -358,11 +358,11 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
term match
case Apply(Select(Ident("BigDecimal"), "apply"), List(value)) =>
decodeTerm(value, definitions).as[NumConstant].map:
case x: Int => BigDecimal(x)
case x: Long => BigDecimal(x)
case x: Float => BigDecimal(x)
case x: Int => BigDecimal(x)
case x: Long => BigDecimal(x)
case x: Float => BigDecimal(x)
case x: Double => BigDecimal(x)

case _ => Left(DecodingFailure.Unknown)

/**
Expand All @@ -376,4 +376,4 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q):
term match
case Apply(TypeApply(Select(Ident("List"), "apply"), _), List(values)) =>
decodeTerm(values, definitions).as[List[?]]
case _ => Left(DecodingFailure.Unknown)
case _ => Left(DecodingFailure.Unknown)

0 comments on commit 2ea32c0

Please sign in to comment.