• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pureconfig / pureconfig / 4737452896

pending completion
4737452896

Pull #1387

github

GitHub
Merge 142706a45 into 682df3b9b
Pull Request #1387: Enable coverage for Scala 3

1279 of 1600 relevant lines covered (79.94%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.16
/core/src/main/scala/pureconfig/ConvertHelpers.scala
1
package pureconfig
2

3
import scala.reflect.ClassTag
4
import scala.util.control.NonFatal
5
import scala.util.{Failure, Success, Try}
6

7
import pureconfig.error._
8

9
/** Useful helpers for building `ConfigConvert` instances and dealing with results.
10
  */
11
trait ConvertHelpers {
12

1✔
13
  private[pureconfig] def toResult[A, B](f: A => B): A => Either[FailureReason, B] =
1✔
14
    v => tryToEither(Try(f(v)))
15

1✔
16
  private[pureconfig] def tryToEither[A](t: Try[A]): Either[FailureReason, A] =
17
    t match {
1✔
18
      case Success(v) => Right(v)
×
19
      case Failure(e) => Left(ExceptionThrown(e))
20
    }
21

1✔
22
  private[pureconfig] def ensureNonEmpty[A](implicit ct: ClassTag[A]): String => Either[FailureReason, String] = {
1✔
23
    case "" => Left(EmptyStringFound(ct.toString))
1✔
24
    case x => Right(x)
25
  }
26

1✔
27
  def catchReadError[A](f: String => A)(implicit ct: ClassTag[A]): String => Either[FailureReason, A] = { string =>
×
28
    try Right(f(string))
29
    catch {
1✔
30
      case NonFatal(ex) => Left(CannotConvert(string, ct.toString(), ex.toString))
31
    }
32
  }
33

34
  /** Convert a `String => Try` into a `String => Option[ConfigValueLocation] => Either` such that after application
35
    *   - `Success(t)` becomes `_ => Right(t)`
36
    *   - `Failure(e)` becomes `location => Left(CannotConvert(value, type, e.getMessage, location)`
37
    */
×
38
  def tryF[A](f: String => Try[A])(implicit ct: ClassTag[A]): String => Either[FailureReason, A] = { string =>
×
39
    f(string) match {
×
40
      case Success(t) => Right(t)
×
41
      case Failure(e) => Left(CannotConvert(string, ct.runtimeClass.getName, e.getLocalizedMessage))
42
    }
43
  }
44

45
  /** Convert a `String => Option` into a `String => Option[ConfigValueLocation] => Either` such that after application
46
    *   - `Some(t)` becomes `_ => Right(t)`
47
    *   - `None` becomes `location => Left(CannotConvert(value, type, "", location)`
48
    */
1✔
49
  def optF[A](f: String => Option[A])(implicit ct: ClassTag[A]): String => Either[FailureReason, A] = { string =>
1✔
50
    f(string) match {
1✔
51
      case Some(t) => Right(t)
×
52
      case None => Left(CannotConvert(string, ct.runtimeClass.getName, ""))
53
    }
54
  }
55
}
56

57
object ConvertHelpers extends ConvertHelpers
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc