All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalafix.internal.config.ReaderUtil.scala Maven / Gradle / Ivy

package scalafix.internal.config

import metaconfig.Conf
import metaconfig.ConfDecoder
import metaconfig.ConfError
import metaconfig.Configured
import scala.reflect.ClassTag

object ReaderUtil {
  def oneOf[T: ClassTag](options: sourcecode.Text[T]*): ConfDecoder[T] = {
    val m = options.map(x => x.source -> x.value).toMap
    fromMap(m)
  }
  // Poor mans coproduct reader
  def fromMap[T: ClassTag](
      m: Map[String, T],
      additionalMessage: PartialFunction[String, String] = PartialFunction.empty
  ): ConfDecoder[T] =
    ConfDecoder.instance[T] {
      case Conf.Str(x) =>
        m.get(x) match {
          case Some(y) =>
            Configured.Ok(y)
          case None =>
            val available = m.keys.mkString(", ")
            val extraMsg = additionalMessage.applyOrElse(x, (_: String) => "")
            val msg =
              s"Unknown input '$x'. Expected one of: $available. $extraMsg"
            Configured.NotOk(ConfError.message(msg))
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy