<commit_before>package ciris.generic.readers

import ciris.{ConfigError, ConfigReader}
import shapeless.{:+:, ::, CNil, Coproduct, Generic, HNil, Inl, Inr, Lazy}

trait GenericConfigReaders {
  implicit val cNilConfigReader: ConfigReader[CNil] =
    ConfigReader.pure { (key, source) =>
      Left(ConfigError(
        s"Could not find any valid coproduct choice while reading ${source.keyType.value} [$key]"))
    }

  implicit def coproductConfigReader[A, B <: Coproduct](
    implicit readA: Lazy[ConfigReader[A]],
    readB: ConfigReader[B]
  ): ConfigReader[A :+: B] = {
    ConfigReader.pure { (key, source) =>
      readA.value.read(key)(source) match {
        case Right(a) => Right(Inl(a))
        case Left(aError) =>
          readB.read(key)(source) match {
            case Right(b) => Right(Inr(b))
            case Left(bError) => Left(aError combine bError)
          }
      }
    }
  }

  implicit def hListArityOneConfigReader[A](
    implicit readA: Lazy[ConfigReader[A]]
  ): ConfigReader[A :: HNil] = {
    readA.value.map(_ :: HNil)
  }

  implicit def genericConfigReader[A, B](
    implicit gen: Generic.Aux[A, B],
    readB: Lazy[ConfigReader[B]]
  ): ConfigReader[A] = {
    readB.value.map(gen.from)
  }
}<commit_msg>Rename hListArityOneConfigReader to unaryHList...<commit_after>package ciris.generic.readers

import ciris.{ConfigError, ConfigReader}
import shapeless.{:+:, ::, CNil, Coproduct, Generic, HNil, Inl, Inr, Lazy}

trait GenericConfigReaders {
  implicit val cNilConfigReader: ConfigReader[CNil] =
    ConfigReader.pure { (key, source) =>
      Left(ConfigError(
        s"Could not find any valid coproduct choice while reading ${source.keyType.value} [$key]"))
    }

  implicit def coproductConfigReader[A, B <: Coproduct](
    implicit readA: Lazy[ConfigReader[A]],
    readB: ConfigReader[B]
  ): ConfigReader[A :+: B] = {
    ConfigReader.pure { (key, source) =>
      readA.value.read(key)(source) match {
        case Right(a) => Right(Inl(a))
        case Left(aError) =>
          readB.read(key)(source) match {
            case Right(b) => Right(Inr(b))
            case Left(bError) => Left(aError combine bError)
          }
      }
    }
  }

  implicit def unaryHListConfigReader[A](
    implicit readA: Lazy[ConfigReader[A]]
  ): ConfigReader[A :: HNil] = {
    readA.value.map(_ :: HNil)
  }

  implicit def genericConfigReader[A, B](
    implicit gen: Generic.Aux[A, B],
    readB: Lazy[ConfigReader[B]]
  ): ConfigReader[A] = {
    readB.value.map(gen.from)
  }
}
{'fraction_non_alphanumeric': 0.14138613861386137, 'fraction_numerical': 0.0, 'mean_word_length': 3.245378151260504, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6651739', 'n_tokens_mistral': 880, 'n_tokens_neox': 882, 'n_words': 181}