Skip to content

Commit

Permalink
Ability to lift a ScalaVersion from a String
Browse files Browse the repository at this point in the history
  • Loading branch information
joan38 committed Oct 1, 2023
1 parent 9953268 commit 254928d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/main/scala/org/typelevel/scalacoptions/ScalaVersion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ object ScalaVersion {
val V3_3_0 = ScalaVersion(3, 3, 0)
val V3_3_1 = ScalaVersion(3, 3, 1)

private val versionRegex = raw"""(\d+)\.(\d+)\.(\d+)""".r
def fromString(version: String): Either[IllegalArgumentException, ScalaVersion] =
version match {
case versionRegex(major, minor, patch) =>
Right(ScalaVersion(major.toLong, minor.toLong, patch.toLong))
case _ => Left(new IllegalArgumentException(s"Scala version $version not recognized"))
}

def unsafeFromString(version: String): ScalaVersion =
fromString(version).fold(throw _, identity)

implicit val scalaVersionOrdering: Ordering[ScalaVersion] =
Ordering.by(version => (version.major, version.minor, version.patch))
}

0 comments on commit 254928d

Please sign in to comment.