From b0ed1ccfb8286e1866b6918d79ddaec471ddcbbc Mon Sep 17 00:00:00 2001 From: Iltotore Date: Sun, 15 Sep 2024 22:24:48 +0200 Subject: [PATCH] dev: Add expr list support --- .../io/github/iltotore/iron/compileTime.scala | 16 ++++++++++++++++ .../iltotore/iron/constraint/collection.scala | 14 ++++++++++++++ .../iltotore/iron/macros/ReflectUtil.scala | 3 +-- .../io/github/iltotore/iron/macros/package.scala | 2 +- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/main/src/io/github/iltotore/iron/compileTime.scala b/main/src/io/github/iltotore/iron/compileTime.scala index 7fcdd84f..489df679 100644 --- a/main/src/io/github/iltotore/iron/compileTime.scala +++ b/main/src/io/github/iltotore/iron/compileTime.scala @@ -4,6 +4,7 @@ import scala.compiletime.constValue import scala.compiletime.ops.* import scala.compiletime.ops.any.ToString import scala.quoted.* +import scala.annotation.targetName /** * Methods and types to ease compile-time operations. @@ -199,3 +200,18 @@ object compileTime: import quotes.reflect.* Apply(Select.unique(constraintExpr.asTerm, "test"), List(expr.asTerm)).asExprOf[Boolean] + + extension [T : Type](expr: Expr[List[T]]) + + def toExprList(using Quotes): Option[List[Expr[T]]] = expr match + case '{ scala.List[T](${Varargs(elems)}*) } => Some(elems.toList) + case '{ scala.List.empty[T] } => Some(Nil) + case '{ Nil } => Some(Nil) + case '{ scala.collection.immutable.List[T](${Varargs(elems)}*) } => Some(elems.toList) + case '{ scala.collection.immutable.List.empty[T] } => Some(Nil) + case '{ Set[T](${Varargs(elems)}*) } => Some(elems.toList) + case '{ Set.empty[T] } => Some(Nil) + case '{ scala.collection.immutable.List[T](${Varargs(elems)}*) } => Some(elems.toList) + case '{ scala.collection.immutable.List.empty[T] } => Some(Nil) + case _ => None + \ No newline at end of file diff --git a/main/src/io/github/iltotore/iron/constraint/collection.scala b/main/src/io/github/iltotore/iron/constraint/collection.scala index 06dccafc..cc2a6807 100644 --- a/main/src/io/github/iltotore/iron/constraint/collection.scala +++ b/main/src/io/github/iltotore/iron/constraint/collection.scala @@ -179,6 +179,20 @@ object collection: inline given forAllString[C, Impl <: Constraint[Char, C]](using inline impl: Impl): ForAllString[C, Impl] = new ForAllString + private def checkIterable[I <: Iterable[?], C, Impl <: Constraint[Char, C]](expr: Expr[I], constraintExpr: Expr[Impl])(using Quotes): Expr[Boolean] = + ??? + /*val rflUtil = reflectUtil + import rflUtil.* + + expr.decode match + case Right(value) => + value + .map(Expr.apply) + .map(applyConstraint(_, constraintExpr)) + .foldLeft(Expr(true))((e, t) => '{ $e && $t }) + + case _ => '{ $expr.forallOptimized(c => ${ applyConstraint('c, constraintExpr) }) }*/ + private def checkString[C, Impl <: Constraint[Char, C]](expr: Expr[String], constraintExpr: Expr[Impl])(using Quotes): Expr[Boolean] = val rflUtil = reflectUtil import rflUtil.* diff --git a/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala b/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala index ae1117bc..7b3ad2cc 100644 --- a/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala +++ b/main/src/io/github/iltotore/iron/macros/ReflectUtil.scala @@ -390,5 +390,4 @@ class ReflectUtil[Q <: Quotes & Singleton](using val _quotes: Q): term match case Apply(TypeApply(Select(Ident("Set"), "apply"), _), List(values)) => decodeTerm(values, definitions).as[List[?]].map(_.toSet) - case _ => Left(DecodingFailure.Unknown) - + case _ => Left(DecodingFailure.Unknown) \ No newline at end of file diff --git a/main/src/io/github/iltotore/iron/macros/package.scala b/main/src/io/github/iltotore/iron/macros/package.scala index 2effdd24..0234a677 100644 --- a/main/src/io/github/iltotore/iron/macros/package.scala +++ b/main/src/io/github/iltotore/iron/macros/package.scala @@ -84,4 +84,4 @@ def isIronTypeImpl[T: Type, C: Type](using Quotes): Expr[Boolean] = case _: ImplicitSearchSuccess => Expr(true) case _: ImplicitSearchFailure => Expr(false) - case _ => Expr(false) + case _ => Expr(false) \ No newline at end of file