diff --git a/modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/W.scala b/modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/W.scala new file mode 100644 index 000000000..eb2109d50 --- /dev/null +++ b/modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/W.scala @@ -0,0 +1,87 @@ +package eu.timepit.refined + +import scala.annotation.compileTimeOnly +import scala.compiletime.ops.int +import scala.compiletime.ops.long +import scala.compiletime.ops.string.CharAt +import scala.compiletime.ops.string.Length +import scala.compiletime.ops.string.Substring +import scala.language.dynamics + +object W extends Dynamic { + type StringTail[A <: String] = Substring[A, 1, Length[A]] + + type StringLast[A <: String] = CharAt[A, int.-[Length[A], 1]] + + type Literal[A <: String] = CharAt[A, 0] match { + case '"' => + // String + StringLast[A] match { + case '"' => + Substring[A, 1, int.-[Length[A], 1]] + } + case '\'' => + // Char + CharAt[A, 2] match { + case '\'' => + Length[A] match { + case 3 => + CharAt[A, 1] + } + } + case _ => + // TODO support another types + // e.g. Double, Float + StringLast[A] match { + case 'L' => + // Long + StringToLong[Substring[A, 0, int.-[Length[A], 1]]] + case _ => + // Int + long.ToInt[StringToLong[A]] + } + } + + type StringToLong[Input <: String] <: Long = CharAt[Input, 0] match { + case '-' => + long.Negate[Loop[StringTail[Input], 0L]] + case _ => + Loop[Input, 0L] + } + + type CharToLong[C <: Char] <: Long = C match { + case '0' => 0L + case '1' => 1L + case '2' => 2L + case '3' => 3L + case '4' => 4L + case '5' => 5L + case '6' => 6L + case '7' => 7L + case '8' => 8L + case '9' => 9L + } + + type Loop[Input <: String, Acc <: Long] <: Long = Length[Input] match { + case 0 => + Acc + case _ => + Loop[ + StringTail[Input], + long.+[ + long.*[10L, Acc], + CharToLong[ + CharAt[Input, 0] + ] + ] + ] + } + + @compileTimeOnly("Illegal reference") + def selectDynamic( + selector: String + ): None.type { type T = Literal[selector.type] } = + None.asInstanceOf[ + None.type { type T = Literal[selector.type] } + ] +} diff --git a/modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/types/digests.scala b/modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/types/digests.scala deleted file mode 100644 index 6367c5d7f..000000000 --- a/modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/types/digests.scala +++ /dev/null @@ -1,48 +0,0 @@ -package eu.timepit.refined.types - -import eu.timepit.refined.api.{Refined, RefinedTypeOps} -import eu.timepit.refined.boolean.And -import eu.timepit.refined.collection.Size -import eu.timepit.refined.generic.Equal -import eu.timepit.refined.string.HexStringSpec - -/** Module for type representing message digests. */ -object digests { - type MD5 = String Refined (HexStringSpec And Size[Equal[32]]) - object MD5 extends RefinedTypeOps[MD5, String] - - type SHA1 = String Refined (HexStringSpec And Size[Equal[40]]) - object SHA1 extends RefinedTypeOps[SHA1, String] - - type SHA224 = String Refined (HexStringSpec And Size[Equal[56]]) - object SHA224 extends RefinedTypeOps[SHA224, String] - - type SHA256 = String Refined (HexStringSpec And Size[Equal[64]]) - object SHA256 extends RefinedTypeOps[SHA256, String] - - type SHA384 = String Refined (HexStringSpec And Size[Equal[96]]) - object SHA384 extends RefinedTypeOps[SHA384, String] - - type SHA512 = String Refined (HexStringSpec And Size[Equal[128]]) - object SHA512 extends RefinedTypeOps[SHA512, String] -} - -trait DigestTypes { - final type MD5 = digests.MD5 - final val MD5 = digests.MD5 - - final type SHA1 = digests.SHA1 - final val SHA1 = digests.SHA1 - - final type SHA224 = digests.SHA224 - final val SHA224 = digests.SHA224 - - final type SHA256 = digests.SHA256 - final val SHA256 = digests.SHA256 - - final type SHA384 = digests.SHA384 - final val SHA384 = digests.SHA384 - - final type SHA512 = digests.SHA512 - final val SHA512 = digests.SHA512 -} diff --git a/modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/types/time.scala b/modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/types/time.scala deleted file mode 100644 index a264a46fe..000000000 --- a/modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/types/time.scala +++ /dev/null @@ -1,61 +0,0 @@ -package eu.timepit.refined.types - -import eu.timepit.refined.api.{Refined, RefinedTypeOps} -import eu.timepit.refined.numeric.Interval - -/** Module for date and time related refined types. */ -object time { - - /** An `Int` in the range from 1 to 12 representing the month-of-year. */ - type Month = Int Refined Interval.Closed[1, 12] - - object Month extends RefinedTypeOps[Month, Int] - - /** - * An `Int` in the range from 1 to 31 representing the day-of-month. - * Note that the days from 29 to 31 are not valid for all months. - */ - type Day = Int Refined Interval.Closed[1, 31] - - object Day extends RefinedTypeOps[Day, Int] - - /** An `Int` in the range from 0 to 23 representing the hour-of-day. */ - type Hour = Int Refined Interval.Closed[0, 23] - - object Hour extends RefinedTypeOps[Hour, Int] - - /** An `Int` in the range from 0 to 59 representing the minute-of-hour. */ - type Minute = Int Refined Interval.Closed[0, 59] - - object Minute extends RefinedTypeOps[Minute, Int] - - /** An `Int` in the range from 0 to 59 representing the second-of-minute. */ - type Second = Int Refined Interval.Closed[0, 59] - - object Second extends RefinedTypeOps[Second, Int] - - /** An `Int` in the range from 0 to 999 representing the millisecond-of-second. */ - type Millis = Int Refined Interval.Closed[0, 999] - - object Millis extends RefinedTypeOps[Millis, Int] -} - -trait TimeTypes { - final type Month = time.Month - final val Month = time.Month - - final type Day = time.Day - final val Day = time.Day - - final type Hour = time.Hour - final val Hour = time.Hour - - final type Minute = time.Minute - final val Minute = time.Minute - - final type Second = time.Second - final val Second = time.Second - - final type Millis = time.Millis - final val Millis = time.Millis -} diff --git a/modules/core/shared/src/main/scala-3.0-/eu/timepit/refined/types/digests.scala b/modules/core/shared/src/main/scala/eu/timepit/refined/types/digests.scala similarity index 100% rename from modules/core/shared/src/main/scala-3.0-/eu/timepit/refined/types/digests.scala rename to modules/core/shared/src/main/scala/eu/timepit/refined/types/digests.scala diff --git a/modules/core/shared/src/main/scala-3.0-/eu/timepit/refined/types/time.scala b/modules/core/shared/src/main/scala/eu/timepit/refined/types/time.scala similarity index 100% rename from modules/core/shared/src/main/scala-3.0-/eu/timepit/refined/types/time.scala rename to modules/core/shared/src/main/scala/eu/timepit/refined/types/time.scala