|
| 1 | + |
| 2 | +// circe.scala |
| 3 | + |
| 4 | +package io.circe |
| 5 | + |
| 6 | +import scala.compiletime.* |
| 7 | +import scala.deriving.Mirror |
| 8 | +import scala.quoted.* |
| 9 | + |
| 10 | +trait Encoder[A]: |
| 11 | + def encode(value: A): String |
| 12 | + |
| 13 | +object Encoder: |
| 14 | + trait AsObject[A] extends Encoder[A] |
| 15 | + given Encoder[String] = ??? |
| 16 | + |
| 17 | +trait Configuration |
| 18 | +object Configuration: |
| 19 | + val default: Configuration = ??? |
| 20 | + |
| 21 | +object Default: |
| 22 | + given [A]: Default[A] = ??? |
| 23 | +trait Default[T] |
| 24 | + |
| 25 | +trait Codec[A] extends Encoder[A] |
| 26 | +object Codec: |
| 27 | + trait AsObject[A] extends Encoder.AsObject[A] |
| 28 | + object AsObject: |
| 29 | + inline final def derived[A](using inline A: Mirror.Of[A]): Codec.AsObject[A] = |
| 30 | + ConfiguredCodec.derived[A](using Configuration.default) |
| 31 | + inline final def derivedConfigured[A](using |
| 32 | + inline A: Mirror.Of[A], |
| 33 | + inline conf: Configuration |
| 34 | + ): Codec.AsObject[A] = ConfiguredCodec.derived[A] |
| 35 | + |
| 36 | +trait ConfiguredEncoder[A](using conf: Configuration) extends Encoder.AsObject[A] |
| 37 | +trait ConfiguredCodec[A] extends Codec.AsObject[A], ConfiguredEncoder[A] |
| 38 | +object ConfiguredCodec: |
| 39 | + inline final def derive[A: Mirror.Of](): ConfiguredCodec[A] = |
| 40 | + derived[A](using Configuration.default) |
| 41 | + inline final def derived[A](using |
| 42 | + conf: Configuration, |
| 43 | + inline mirror: Mirror.Of[A] |
| 44 | + ): ConfiguredCodec[A] = ${ derivedImpl[A]('conf, 'mirror) } |
| 45 | + def ofProduct[A]( |
| 46 | + encoders: => List[Encoder[?]] |
| 47 | + )(using Configuration, Default[A]): ConfiguredCodec[A] = ??? |
| 48 | + def derivedImpl[A: Type](conf: Expr[Configuration], mirror: Expr[Mirror.Of[A]])(using |
| 49 | + q: Quotes |
| 50 | + ): Expr[ConfiguredCodec[A]] = { |
| 51 | + mirror match { |
| 52 | + case '{ |
| 53 | + ${ _ }: Mirror.ProductOf[A] { |
| 54 | + type MirroredLabel = l |
| 55 | + type MirroredElemLabels = el |
| 56 | + type MirroredElemTypes = et |
| 57 | + } |
| 58 | + } => |
| 59 | + '{ |
| 60 | + ConfiguredCodec.ofProduct[A]( |
| 61 | + derivation.summonEncoders[et & Tuple](false)(using $conf) |
| 62 | + )(using $conf) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | +object derivation: |
| 68 | + sealed trait Inliner[A, Arg]: |
| 69 | + inline def apply[T](inline arg: Arg): A |
| 70 | + |
| 71 | + class EncoderNotDeriveSum(using config: Configuration) extends Inliner[Encoder[?], Unit]: |
| 72 | + inline def apply[T](inline arg: Unit): Encoder[?] = summonEncoder[T](false) |
| 73 | + |
| 74 | + inline final def loopUnrolled[A, Arg, T <: Tuple](f: Inliner[A, Arg], inline arg: Arg): List[A] = |
| 75 | + inline erasedValue[T] match |
| 76 | + case _: EmptyTuple => Nil |
| 77 | + case _: (h *: ts) => f[h](arg) :: loopUnrolled[A, Arg, ts](f, arg) |
| 78 | + |
| 79 | + inline def loopUnrolledNoArg[A, T <: Tuple](f: Inliner[A, Unit]): List[A] = |
| 80 | + loopUnrolled[A, Unit, T](f, ()) |
| 81 | + |
| 82 | + inline final def summonEncoders[T <: Tuple](inline derivingForSum: Boolean)(using |
| 83 | + Configuration |
| 84 | + ): List[Encoder[?]] = |
| 85 | + loopUnrolledNoArg[Encoder[?], T]( |
| 86 | + inline if (derivingForSum) compiletime.error("unreachable") |
| 87 | + else new EncoderNotDeriveSum |
| 88 | + ) |
| 89 | + |
| 90 | + private[circe] inline final def summonEncoder[A]( |
| 91 | + inline derivingForSum: Boolean |
| 92 | + )(using Configuration): Encoder[A] = summonFrom { |
| 93 | + case encodeA: Encoder[A] => encodeA |
| 94 | + case _: Mirror.Of[A] => |
| 95 | + inline if (derivingForSum) compiletime.error("unreachable") |
| 96 | + else error("Failed to find an instance of Encoder[]") |
| 97 | + } |
0 commit comments