Skip to content

Commit 2916a6d

Browse files
committed
Ability to lift a ScalaVersion from a String
1 parent 9953268 commit 2916a6d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/src/main/scala/org/typelevel/scalacoptions/ScalaVersion.scala

+12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ object ScalaVersion {
4444
val V3_3_0 = ScalaVersion(3, 3, 0)
4545
val V3_3_1 = ScalaVersion(3, 3, 1)
4646

47+
def fromString(version: String): Either[IllegalArgumentException, ScalaVersion] = {
48+
val regex = raw"""(\d+)\.(\d+)\.(\d+).*""".r
49+
version match {
50+
case regex(major, minor, patch) =>
51+
Right(ScalaVersion(major.toLong, minor.toLong, patch.toLong))
52+
case _ => Left(new IllegalArgumentException(s"Scala version $version not recognized"))
53+
}
54+
}
55+
56+
def unsafeFromString(version: String): ScalaVersion =
57+
fromString(version).fold(throw _, identity)
58+
4759
implicit val scalaVersionOrdering: Ordering[ScalaVersion] =
4860
Ordering.by(version => (version.major, version.minor, version.patch))
4961
}

0 commit comments

Comments
 (0)