From e239c58d9c6f2871e6f7523d13cba511468de3a9 Mon Sep 17 00:00:00 2001 From: Joan Goyeau Date: Tue, 26 Sep 2023 09:17:21 -0400 Subject: [PATCH] Ability to lift a ScalaVersion from a String --- .../org/typelevel/scalacoptions/ScalaVersion.scala | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/src/main/scala/org/typelevel/scalacoptions/ScalaVersion.scala b/lib/src/main/scala/org/typelevel/scalacoptions/ScalaVersion.scala index 74b577e..a396ca4 100644 --- a/lib/src/main/scala/org/typelevel/scalacoptions/ScalaVersion.scala +++ b/lib/src/main/scala/org/typelevel/scalacoptions/ScalaVersion.scala @@ -44,6 +44,18 @@ object ScalaVersion { val V3_3_0 = ScalaVersion(3, 3, 0) val V3_3_1 = ScalaVersion(3, 3, 1) + def unsafeFromString(version: String): ScalaVersion = + fromString(version).fold(throw _, identity) + + def fromString(version: String): Either[IllegalArgumentException, ScalaVersion] = { + val regex = raw"""(\d+)\.(\d+)\.(\d+).*""".r + version match { + case regex(major, minor, patch) => + Right(ScalaVersion(major.toLong, minor.toLong, patch.toLong)) + case _ => Left(new IllegalArgumentException(s"Scala version $version not recognized")) + } + } + implicit val scalaVersionOrdering: Ordering[ScalaVersion] = Ordering.by(version => (version.major, version.minor, version.patch)) }